Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和Java中的AES-128加密,但溢出错误,无符号字节整数小于最小值_Java_Python_Aes - Fatal编程技术网

Python和Java中的AES-128加密,但溢出错误,无符号字节整数小于最小值

Python和Java中的AES-128加密,但溢出错误,无符号字节整数小于最小值,java,python,aes,Java,Python,Aes,我是Python新手,正在将Java编写的AES加密重写为Python,但遇到了异常: 溢出错误或无符号字节整数小于最小值 将32字节的密钥字符串转换为16字节长度的字节数组的算法是固定的,在函数keyToBytes中实现,我不能使用其他方法,如使用sha256转换为哈希密钥字符串 /** * custom algorithm to convert the string of key to bytes * @param key * @return */ public static byt

我是Python新手,正在将Java编写的AES加密重写为Python,但遇到了异常:

溢出错误或无符号字节整数小于最小值

将32字节的密钥字符串转换为16字节长度的字节数组的算法是固定的,在函数
keyToBytes
中实现,我不能使用其他方法,如使用
sha256
转换为哈希密钥字符串

/**
 * custom algorithm to convert the string of key to bytes
 * @param key
 * @return
 */
public static byte[] keyToBytes(String key) {

    int length = key.length();//32 bytes
    byte[] bArr = new byte[(length / 2)]; //16 bytes length
    int a2 = 0;
    int b = 30;
    int c = 2;
    while (true) {
        int i = a2 + 2;
        if (i > length) {
            i = length - 1;
        }
        int i2 = a2 / 2;
        String substring = key.substring(a2, i);

        bArr[i2] = (byte) Integer.parseInt(substring, 16);
        if (a2 == b) {
            break;
        }
        a2 += c;
    }
    return bArr;
}

public static String aesEncrypt(byte[]key, String text) {

    try {
        IvParameterSpec ivParameterSpec = new IvParameterSpec(new byte[16]);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
        instance.init(1, secretKeySpec, ivParameterSpec);
        byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
        String result = Base64.getEncoder().encodeToString(instance.doFinal(bytes));
        return result;
    } catch (NoSuchAlgorithmException e) {
        System.out.println(e);
        return null;
    } catch (InvalidKeyException e) {
        System.out.println(e);
        return null;
    }  catch (IllegalBlockSizeException e) {
        System.out.println(e);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        System.out.println(e);
        return null;
    } catch (BadPaddingException e) {
        System.out.println(e);
        return null;
    } catch (NoSuchPaddingException e) {
        System.out.println(e);
        return null;
    }
}

public static void main(String[] args) {
    String key = "7e585aedb1dd597382cf5aaaabfa221d";
    byte [] keyInBytes = keyToBytes(key);
    System.out.println(Arrays.toString(keyInBytes));
    String text = "hello world";
    String ret = aesEncrypt(keyInBytes,text);
    System.out.println(ret);
}
输出

[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
exception has occurred: OverflowError in  
self.key = array.array('B',key).tobytes()
unsigned byte integer is less than minimum
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
输出

[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
exception has occurred: OverflowError in  
self.key = array.array('B',key).tobytes()
unsigned byte integer is less than minimum
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
最终解决了这个问题 在爪哇

输出

[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
exception has occurred: OverflowError in  
self.key = array.array('B',key).tobytes()
unsigned byte integer is less than minimum
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
对于该数组中的每个值,都是十进制形式的有符号字节,范围为-128到+127 请注意,对于每对两个字符7e 58 5a ed。。。 它通过integer.parseInt(“ed”,16)==int('ed',16)==237进行转换 因此,Python和Java中表示的32位有符号整数是相同的 但在Java中,当整数237转换为字节时,默认情况下它被屏蔽为237-256=-19

 if num > 127:      
    num = num - 256
bArr[i2] = num 
在Python方面,即使减去256并得到屏蔽值-19,它仍然是一个32位单精度整数,而不是有符号字节

将32位带符号整数列表转换为字节格式的步骤

key = [126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
keyInBytes = array.array('b',key).tobytes()
aes加密

def encrypt(self, message):
     message = self._pad(message)
     iv = array.array('b',[0]*16).tobytes()
     cipher = AES.new(self.key, AES.MODE_CBC, iv)
     return base64.b64encode(cipher.encrypt(message)).decode('utf-8')
输出

[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
exception has occurred: OverflowError in  
self.key = array.array('B',key).tobytes()
unsigned byte integer is less than minimum
[126, 88, 90, -19, -79, -35, 89, 115, -126, -49, 90, -86, -85, -6, 34, 29]
2gAIBaXGgTfepnHit0A7sg==