java - 简单的加密/解密似乎不起作用,帮我找出我做错了什么

标签 java encryption

尝试进行简单的加密/解密以在简单的通信协议(protocol)中使用它。 看来我无法使我的解密方法起作用,或者可能是加密方法,输出文本不是原始文本。 我做错了什么?

String s1 = encrypt("Hello World!");
String s2 = decrypt(s1);

public static String encrypt(String decStr) {
        byte[] key = new byte[]{
                92, -33, 70, 90, 42, -22, -76, 38,
                37, 109, 26, -113, 125, 105, 66, 81,
                17, 22, 21, -30, 87, -124, -85, 58,
                40, -116, -100, 28, 37, 127, 51, 36
        };

        byte[] encBytes = decStr.getBytes();

        int n = encBytes.length + 5;
        int k = key[encBytes.length % key.length] & 255;

        for (int i = 0; i < encBytes.length; i++) {
            encBytes[i] ^= key[(n + i) % key.length];
            encBytes[i] ^= key[(k + i) % key.length];
        }

        return new BigInteger(encBytes).toString(36);
}

public static String decrypt(String encStr) {
        byte[] key = new byte[]{
                92, -33, 70, 90, 42, -22, -76, 38,
                37, 109, 26, -113, 125, 105, 66, 81,
                17, 22, 21, -30, 87, -124, -85, 58,
                40, -116, -100, 28, 37, 127, 51, 36
        };

        byte[] encBytes = new BigInteger(encStr, 36).toByteArray();
        byte[] decBytes = Arrays.copyOfRange(encBytes, 1, encBytes.length);

        int n = decBytes.length + 5;
        int k = key[decBytes.length % key.length] & 255;

        for (int i = 0; i < decBytes.length; i++) {
            decBytes[i] ^= key[(n + i) % key.length];
            decBytes[i] ^= key[(k + i) % key.length];
        }

        return new String(decBytes);
}

最佳答案

byte[] decBytes = Arrays.copyOfRange(encBytes, 1, encBytes.length);

您从第二个角色开始。将 1 更改为 0

byte[] decBytes = Arrays.copyOfRange(encBytes, 0, encBytes.length);

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case (byte)0 is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange(byte[],%20int,%20int)

此外,您正在创建一个新的字节数组,然后立即将其复制到第二个数组中,但永远不要使用原始数组。看来一开始就没有必要执行这个副本

测试:

Hello World!
zxj9kxukhtsdmoll41
Hello World!

关于java - 简单的加密/解密似乎不起作用,帮我找出我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769089/

相关文章:

Java : Automatically set array dimensions based on number of columns in CSV file

php - MySQL 和存储加密的用户数据

java和正则表达式: how to match a string with lithreal parenthesis?

java - 支柱 1 : how do i get session variable in DAO layer

Java - 圆形玩家转身

java - Objective-C 中的等效 Java javax.crypto 库?

iphone - iPhone SQLite 数据库中包含的安全信息

iphone - 在 Objective C 中复制 Java 加密

java - PBE 相对于 DES、3DES 和 AES 的优势

java - servlet 中的可配置值