在阅读了很多关于AES,JCE及其256位密钥的无限强度策略文件之后,我已经实现了这个Oracle/Sun guide。
更新
我结合了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方法时(由sun guide here提供),它给了我一个BadPadding异常。
线程“main”中的异常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);
}
问题,
请帮助。
答案 0 :(得分:2)
您可以使用自己的Base64编码/解码例程来解决问题。