8.24 加密解密

1、加密解密

public static void main(String[] args) {
  /*  String hualili = Encrypt("hualili", 123);
    System.out.println(hualili);
    String encrypt = Decrypt(hualili, 123);
    System.out.println(encrypt);*/


    String username="hualili";
    String encrypt = Encrypt(username, 1);
    System.out.println(encrypt);
    String decrypt = Decrypt(encrypt, 1);
    System.out.println(decrypt);

}

private static int C1 = 52845;
private static int C2 = 22719;

// 加密函数

public static String Encrypt(String S, int Key) {

    StringBuffer Result = new StringBuffer();
    StringBuffer str;
    int i, j;


    for (i = 0; i < S.length(); i++) {
        // 依次对字符串中各字符进行操作
        Result.append((char) (S.charAt(i) ^ (Key >> 8))); // 将密钥移位后与字符异或
        Key = ((byte) Result.charAt(i) + Key) * C1 + C2; // 产生下一个密钥
    }
    S = Result.toString();
    System.out.println("密文中间值:" + S);
    Result = new StringBuffer();
    for (i = 0; i < S.length(); i++) // 对加密结果进行转换
    {
        j = (int) S.charAt(i); // 提取字符
        // 将字符转换为两个字母保存
        str = new StringBuffer(); // 设置str长度为2
        str.append((char) (65 + j / 26));//这里将65改大点的数例如256,密文就会变乱码,效果更好,相应的,解密处要改为相同的数
        str.append((char) (65 + j % 26));
        Result.append(str);
    }
    return Result.toString();
}

// 解密函数

public static String Decrypt(String S, int Key) {
    StringBuffer Result = new StringBuffer();
    StringBuffer str;
    int i, j;


    for (i = 0; i < S.length() / 2; i++) // 将字符串两个字母一组进行处理
    {
        j = ((int) S.charAt(2 * i) - 65) * 26;//相应的,解密处要改为相同的数

        j += (int) S.charAt(2 * i + 1) - 65;
        Result.append((char) j);
    }
    S = Result.toString(); // 保存中间结果
    System.out.println("原文中间值:" + S);
    Result = new StringBuffer();
    for (i = 0; i < S.length(); i++) // 依次对字符串中各字符进行操作
    {
        Result.append((char) (S.charAt(i) ^ (Key >> 8))); // 将密钥移位后与字符异或
        Key = ((byte) S.charAt(i) + Key) * C1 + C2; // 产生下一个密钥
    }
    return Result.toString();
}

猜你喜欢

转载自blog.csdn.net/zlc2351951436/article/details/100072553