1

我使用 AES 作为加密技术。在解密我的加密字符串时,我面临着这个StringIndexOutOfBoundsException String index out of range : -3。你能告诉我为什么我会面对这个吗?是我使用的相关BASE64编码吗?

解密代码

Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
String salt = "some salt";
String dValue = null;
String valueToDecrypt = encryptedData;
for (int i = 0; i < 2; i++) {
    byte[] decordedValue = Base64.decodeBase64(valueToDecrypt);
    byte[] decValue = c.doFinal(decordedValue);
    dValue = new String(decValue).substring(salt.length());
    valueToDecrypt = dValue;
}

加密代码

    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    String salt = "some salt";
    String valueToEnc = null;
    String eValue = Data;
    for (int i = 0; i < 2; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes(UNICODE_FORMAT));
        eValue =new String(Base64.encodeBase64(encValue));
    }

参考http ://www.digizol.com/2009/10/java-encrypt-decrypt-jce-salt.html

4

0 回答 0