使用AES解密时的Stringindexoutofboundsexception

时间:2013-03-25 07:23:26

标签: java encryption aes

我使用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

0 个答案:

没有答案