我有一段使用AES算法加密和解密的代码,该算法使用的是sun.misc。*包。
后来我才知道使用那些使我遵循使用Apache的Commons Codec有效的建议的包是错误的。
上一段代码如下:
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class AESencrp {
private static final String ALGO = "AES";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
正如所建议的,我已经删除了sun.misc并进行了以下更改。
在Apache的commons编解码器中用Base64替换BASE64Encoder类后:
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
byte[] encryptedValue = new Base64().encode(encVal);
return new String(encryptedValue);
}
我无法找到一个合适的解密替代品,因为我在线:
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
我找不到任何执行decodeBuffer(String encryptedData)工作的方法,并返回一个decodeValue的字节数组。
答案 0 :(得分:4)
好的,那又怎么样?
使用apache Base64 util类与sun.misc.Base64Encoder类似。 Apache Base64类提供了许多重载方法,可以减轻字节和字符串之间的转换。
byte[] encodedBytes = Base64.encodeBase64(testString.getBytes());
String decodedString = new String(Base64.decodeBase64(encodedBytes));
答案 1 :(得分:0)
public class EncryptionDecrption {
private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[]{'T', 'h', 'e', 'R', 'o', 'o', 'K', 'n', 'a', 't','E','n', 'i', 'r','i','n'};
public EncryptionDecrption(){
}
public static String setEncryptedString(String data) throws Exception {
Key key = getKey();
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedValue = cipher.doFinal(data.getBytes("UTF-8"));
return Base64.encodeToString(encryptedValue, Base64.DEFAULT);
}
public static String getDecryptedValue(String data) throws Exception {
if(data != null) {
Key key = getKey();
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodebyte = Base64.decode(data.getBytes("UTF-8"), Base64.DEFAULT);
byte[] decValue = cipher.doFinal(decodebyte);
return new String(decValue);
}
return null;
}
private static Key getKey() throws Exception {
return new SecretKeySpec(keyValue, ALGO);
}
}
它适用于Android。