我試圖創建一個簡單的加密和解密類。兩種方法都給出了相同的字符串。我有點困惑。請需要幫助。創建一個簡單的AES加密和解密
這裏是我的代碼片段:
public class UltimateEncryptor {
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String RANDOM_GENERATOR_ALGORITHM = "AES";
// Encrypts string and encodes in Base64
public String encrypt(String password, String data) throws Exception
{
byte[] secretKey = password.getBytes();
byte[] clear = data.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, CIPHER_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encrypted = cipher.doFinal(clear);
String encryptedString = Base64.encodeToString(encrypted, Base64.DEFAULT);
return encryptedString;
}
// Decrypts string encoded in Base64
public String decrypt(String password, String encryptedData) throws Exception
{
byte[] secretKey = password.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, CIPHER_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] encrypted = Base64.decode(encryptedData, Base64.DEFAULT);
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted);
}
}
whaat我正在這裏做的是加密/解密字符串日期occording給定的密碼。 – 2013-04-21 10:18:31
'我創建的兩個方法都給出相同的字符串。 '你是什麼意思 – stinepike 2013-04-21 10:20:53
我的意思是兩種方法,即加密和解密接收兩個字符串一個是關鍵字符串,另一個是一些字符串數據。謝謝, – 2013-04-21 16:53:12