在阅读了很多关于 AES、JCE 及其 256 位密钥的无限强度策略文件以及这个Oracle/Sun 指南之后,我已经实现了这一点。
更新
我结合sun提供的代码,做了一个java类AESencrp.java
/**
*
* @author MUDASSIR
*/
import java.security.*;
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, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
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, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decodedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey(byte[] keyValue) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
这工作得很好,但是每当我构建我的项目时,它都会发出警告
“Base64encoder 是专有软件,必须在未来的版本中删除”。
当我移除 base64 编码器并改用 asHex 方法时(由此处的太阳指南提供),它给了我一个 BadPadding 异常。
线程“主”javax.crypto.BadPaddingException 中的异常:给定最终块未正确填充
这是我没有 base64 编码器的代码
/**
*
* @author MUDASSIR
*/
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class AESencrp2 {
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 asHex(byte buf[]) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static String encrypt(String Data, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedValue = c.doFinal(Data.getBytes());
return asHex(encryptedValue);
}
public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
Key key = generateKey(keyValue);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decValue = c.doFinal(encryptedData.getBytes());
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey(byte[] keyValue) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
这是主要的,我在这里尝试代码
public static void main(String args[]) throws Exception {
byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
String password = "This is the data that is going to be encrypted";
String passwordEnc = AESencrp.encrypt(password, keyValue);
//String passwordEnc = AESencrp2.encrypt(password, keyValue);
String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);
//String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);
System.out.println("Plain Text : " + password);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
}
问题,
- 1 - 如果我使用 base64 编码器,它是专有的。
- 2 - 如果使用 sun 提供的 asHex 方法,我会得到错误的填充异常。
请帮忙。