纯JS加密+时间校验(没引用任何框架),Java解密

1、JS加密

1
2
3
4
5
6
function caesarEncrypt(text, shift) {
    let str = "G7bCk1Xf2A9qLmJ0pVrTz5D4oS6wEhN" + Date.now().toString();
    return str.split('')
    .map(char => String.fromCharCode(char.charCodeAt(0) + 3))
    .join('');
}

2、Java校验 verify

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.bak.util;
 
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
 
public class CaesarCipherUtil {
 
    private static  String key = "G7bCk1Xf2A9qLmJ0pVrTz5D4oS6wEhN";
    // 解密函数,类似于加密,只是 shift 应该取反
    public static String caesarDecrypt(String encryptedText, int shift) {
        StringBuilder decryptedText = new StringBuilder();
 
        // 遍历每个字符,进行解密
        for (int i = 0; i < encryptedText.length(); i++) {
            char ch = encryptedText.charAt(i);
            // 通过将字符的 ASCII 码减去 shift 来解密
            decryptedText.append((char) (ch - shift));
        }
 
        return decryptedText.toString();
    }
 
    // 加密函数
    public static String caesarEncrypt(String text, int shift) {
        StringBuilder encryptedText = new StringBuilder();
 
        // 遍历每个字符,进行加密
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            // 通过将字符的 ASCII 码加上 shift 来加密
            encryptedText.append((char) (ch + shift));
        }
 
        return encryptedText.toString();
    }
 
    public static String verify(String token){
        String decryptedText = caesarDecrypt(token,3);
        if(!decryptedText.startsWith(key)){
            return "校验不通过!";
        }else{
            Long timestamp = Long.valueOf(decryptedText.substring(32));
            // 将时间戳转换为 LocalDateTime
            LocalDateTime time = Instant.ofEpochSecond(timestamp)
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            // 计算时间差
            Duration duration = Duration.between(time, now);
            // 判断时间差是否超过5分钟
            if (duration.toMinutes() > 5) {
                return "时间超出5分钟!";
            }
        }
        return null;
    }
    public static void main(String[] args) {
        // 示例:凯撒密码加密和解密
        String originalText = "Hello, World!";
        int shift = 3// 假设加密时使用了 shift 为 3
 
        // 加密文本
        String encryptedText = caesarEncrypt(originalText, shift);
        System.out.println("Encrypted Text: " + encryptedText);
 
        // 解密文本
        String decryptedText = caesarDecrypt(encryptedText, shift);
        System.out.println("Decrypted Text: " + decryptedText);
    }
 
}

  

 

posted @   信铁寒胜  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-12-26 30天自制操作系统光盘
点击右上角即可分享
微信分享提示