2013-03-18 104 views
0
try{ 
    String plainData="my name is laksahan",cipherText,decryptedText; 
    KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
    keyGen.init(128); 
    SecretKey secretKey = keyGen.generateKey(); 
    Cipher aesCipher = Cipher.getInstance("AES"); 
    aesCipher.init(Cipher.ENCRYPT_MODE,secretKey); 
    byte[] byteDataToEncrypt = plainData.getBytes(); 
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
    cipherText = new BASE64Encoder().encode(byteCipherText); 
System.out.println(cipherText); 
}catch(Exception e){ 

} 

我也有解密代碼,但我想使用cipherText的輸出解密消息。我如何在這裏解密信息加密?

例如;我的密文輸出是uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

我想解密此:提前uSG1OxJPywzU4JylpqgS6SoB9t21GZ4iN3bY2M6Qf10=

感謝。

+2

行 - 現在我可以閱讀代碼(請閱讀如何使用代碼格式)。對於初學者,將'catch(異常e) { }''更改爲'catch(異常e) {0121} e.printStckTrace(); }' – 2013-03-18 07:34:48

回答

1

試試這個

byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, secretKeyUsed while encrypting); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
+0

我如何提供解密後的代碼? – user2136160 2013-03-18 07:43:06

+0

cipherText = new BASE64Encoder()。decode(decrypted code); – PSR 2013-03-18 07:43:42

+0

仍撲朔迷離先生,你能解釋一下這個... – user2136160 2013-03-18 07:47:38

3

試試這個代碼,它工作正常,在我的pc.Good運氣!

import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 

public class AESExample 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
      String plainData = "my name is laksahan", cipherText, decryptedText; 
      KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
      keyGen.init(128); 
      SecretKey secretKey = keyGen.generateKey(); 
      cipherText = encrypt(plainData, secretKey); 
      System.out.println(cipherText); 
      decryptedText = decrypt(cipherText, secretKey); 
      System.out.println(decryptedText); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    } 

    public static String encrypt(String plainData, SecretKey secretKey) throws Exception 
    { 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.ENCRYPT_MODE, secretKey); 
     byte[] byteDataToEncrypt = plainData.getBytes(); 
     byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
     return new BASE64Encoder().encode(byteCipherText); 
    } 

    public static String decrypt(String cipherData, SecretKey secretKey) throws Exception 
    { 
     byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, secretKey); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
    } 

} 

如果你想使用客戶鍵,試試下面的代碼,只記得密鑰長度爲128位。順便說一下,我更喜歡將密鑰存儲在密鑰庫文件中!

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 

public class AESExample 
{ 

    public static void main(String[] args) 
    { 
     try 
     { 
      byte[]key={-4, -14, 106, -75, -9, 65, -95, 77, -52, 73, -87, -101, 80, 94, -59, -66}; 
      String plainData = "my name is laksahan", cipherText, decryptedText; 
      System.out.println(key.length); 
      cipherText = encrypt(plainData, key); 
      System.out.println(cipherText); 
      decryptedText = decrypt(cipherText, key); 
      System.out.println(decryptedText); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public static String encrypt(String plainData, byte[] key) throws Exception 
    { 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
     aesCipher.init(Cipher.ENCRYPT_MODE, keySpec); 
     byte[] byteDataToEncrypt = plainData.getBytes(); 
     byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
     return new BASE64Encoder().encode(byteCipherText); 
    } 

    public static String decrypt(String cipherData, byte[] key) throws Exception 
    { 
     byte[] data = new BASE64Decoder().decodeBuffer(cipherData); 
     SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
     Cipher aesCipher = Cipher.getInstance("AES"); 
     aesCipher.init(Cipher.DECRYPT_MODE, keySpec); 
     byte[] plainData = aesCipher.doFinal(data); 
     return new String(plainData); 
    } 

} 
+0

這很有幫助,但我想提供我的加密密碼並解密它。如果你可以提供一個帶有例子的代碼,它會對我很有幫助 – user2136160 2013-03-18 08:30:18

+0

@ user2136160你是什麼意思「提供你的加密密碼並解密它」? – 2013-03-18 17:56:31

+0

您的意思是將密碼存儲在某處並使用密碼來加密或解密數據?我用代碼改變了我的答案 – 2013-03-19 01:48:51