我在这里有一个简单的代码,该代码使用了 Eclipse Java 7.0 中不可用的 sun.misc.BASE64Encoder 和 sun.misc.BASE64Decoder,我想让代码使用 Apache commons base 64 并且仍然可以完全一样的东西,
变量
private static final String ALGORITHM = "AES";
private static final byte[] keyValue = "2H5a1r5i6s3h8C1h".getBytes();
原始代码
public static String AESencrypt(String valueToEnc) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}
public static String AESdecrypt(String encryptedValue) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
尝试修改为 apache commons 代码
public static String AESencrypt(String valueToEnc) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new Base64().encodeBase64(encValue).toString();
return encryptedValue;
}
public static String AESdecrypt(String encryptedValue) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new Base64().decodeBase64(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
原始代码工作正常,新代码抛出以下异常,
线程“main”javax.crypto.IllegalBlockSizeException 中的异常:在 com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750) 在 com.sun.crypto 使用填充密码解密时,输入长度必须是 16 的倍数。 provider.CipherCore.doFinal(CipherCore.java:676) at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at trial2.encrypt .AESdecrypt(encrypt.java:28) at trial2.encrypt.main(encrypt.java:37) Java 结果:1
如何在不过多更改初始代码的情况下解决这个问题,这里和那里的几行就可以了。是否可以完全消除 Base64encoding 步骤并使其仅使用如下密码:
public static String AESencrypt(String valueToEnc) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
return encValue.toString();
}
public static String AESdecrypt(String encryptedValue) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decValue = c.doFinal(encryptedValue.getBytes());
String decryptedValue = new String(decValue);
return decryptedValue;
}
上面的代码与第二个代码有类似的异常:
线程“main”javax.crypto.IllegalBlockSizeException 中的异常:在 com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750) 在 com.sun.crypto 使用填充密码解密时,输入长度必须是 16 的倍数。 provider.CipherCore.doFinal(CipherCore.java:676) at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at trial2.encrypt .AESdecrypt(encrypt.java:26) at trial2.encrypt.main(encrypt.java:35) Java 结果:1
请帮助和TYVM