2013-06-13 73 views
3

我已經編寫了用於AES解密的代碼,但沒有取得任何成功。 我的AES算法類在這裏。 http://pastebin.com/QtpFnW84 和實施是:AES算法 - 解密問題

String Masterkey = "eX0XcsF8lkeX0XcsF8lkeX0XcsF8lkeX0XcsF8lkeX0XcsF8lk"; 
    try { 
     String s = AES_Algo 
       .decrypt(
         Masterkey, 
         "LVmDIcmVIuNVPObjLXkVbFc13NCX1Md3DjrvfiioMQHS7QmizT3dlSujgA7NS0zI HEweRWGcwOKpu0wurK495yCTWkJO33X1n+at60xLdJ7ZUreRWN9RatUjRQuLI7Ft kwH7QMTQAYXQizGJ0HrArja8QA/YnkiGpgO0pdmYm9Mb6g/sIXhz1Oeo42uwzTM1 F+t6AM/qrH9ZMozlctU6LQQVIggP8zzmnwvjNCyyYJCsXedOEMcvrpQV100gz+pf cE4RisPgN0IOKzvzepJ88E3VMPCXBv/AV4Z2/fuBcmimzGdvZwKgYM/39TGNBS7t T491knA3ZdMoAnSPFvdM4khfRyM5I9FJpwDxmpykA4VpBUhyd4p+ZS1ZSQ8Zwi3I 5egtoNkSJhI6pjAR7PbzJtJ+VAWCVIdsFP4Kc+KKPBE0HVS5UiQQ+OJjx2r9iMMR OYqeyqMv8xw3Wy7TBMiKnQMCRo5+K1mDabx164+6cfoKk8+6b5WlNfBQVobZpQs2"); 
     Log.e("s", s); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

每次我得到異常:

06-13 05:03:43.013: W/System.err(1757): java.lang.NumberFormatException: unable to parse 'LV' as integer 
06-13 05:03:43.043: W/System.err(1757):  at java.lang.Integer.parse(Integer.java:433) 
06-13 05:03:43.043: W/System.err(1757):  at java.lang.Integer.parseInt(Integer.java:422) 
06-13 05:03:43.043: W/System.err(1757):  at java.lang.Integer.valueOf(Integer.java:704) 
06-13 05:03:43.043: W/System.err(1757):  at com.caddytips.AES_Algo.toByte(AES_Algo.java:76) 
06-13 05:03:43.043: W/System.err(1757):  at com.caddytips.AES_Algo.decrypt(AES_Algo.java:32) 

任何人可以幫助我嗎?

在此先感謝。

+0

您的密碼似乎是無效的。你是如何加密的? – Raghunandan

+0

@Raghunandan:意思是我的加密值是錯誤的。 –

+0

看起來像是錯誤的 – Raghunandan

回答

5

http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html

http://developer.android.com/reference/javax/crypto/SecretKeyFactory.html

檢查上面的鏈接。使用下面的參考。根據您的需要修改以下內容。

使用

try { 
DescEncrypter ec = new DescEncrypter(); 
byte[] cipherText =ec.encrypt("hi", "hello"); 
String enc = new String(cipherText,"UTF-8"); 
String decryp= ec.decrypt("hi", cipherText); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 

DescEncrypter.java

import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.PBEKeySpec; 
import javax.crypto.spec.SecretKeySpec; 

public class DescEncrypter { 

    public static final int SALT_LENGTH = 20; 
    public static final int PBE_ITERATION_COUNT = 200; //1024; 

    private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC"; 

    //algoritmo/modo/relleno 
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; 

    byte[] iv = "1234567890asdfgh".getBytes(); 

    byte[] salt = "dfghjklpoiuytgftgyhj".getBytes(); 

    public byte[] encrypt(String password, String cleartext) { 

     byte[] encryptedText = null; 

     try { 


      PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256); 

      //Factoria para crear la SecretKey, debemos indicar el Algoritmo 
      SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM); 

      SecretKey tmp = factory.generateSecret(pbeKeySpec); 

      //Creamos una llave; 
      SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); 

      //Obtenemos la llave, solo informativo 
      byte[] key = secret.getEncoded(); 

      //La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica 
      Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM); 

      //byte[] iv = generateIv(); 

      IvParameterSpec ivspec = new IvParameterSpec(iv); 

      //Accion, SecretKey, parameter specification for an initialization vector 
      encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec); 

      //Realizamos el cifrado 
      encryptedText = encryptionCipher.doFinal(cleartext.getBytes()); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return encryptedText; 
    } 

    public String decrypt(String password, byte[] encryptedText) { 

     String cleartext = ""; 

     try { 

      PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256); 

      //Factoria para crear la SecretKey, debemos indicar el Algoritmo 
      SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM); 

      SecretKey tmp = factory.generateSecret(pbeKeySpec); 

      //Creamos una llave; 
      SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); 

      //Obtenemos la llave, solo informativo 
      byte[] key = secret.getEncoded(); 

      //La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica 
      Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM); 

      //byte[] iv = generateIv(); 

      IvParameterSpec ivspec = new IvParameterSpec(iv); 

      //Accion, SecretKey, parameter specification for an initialization vector 
      decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec); 

      //Realizamos el descifrado 
      byte[] decryptedText = decryptionCipher.doFinal(encryptedText); 

      cleartext = new String(decryptedText); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return cleartext; 
    }  
} 
+0

儘管比海報代碼好得多,但我沒有看到使用密碼的任何要求? –

+0

它是來自其中一個代碼的複製粘貼。所以在複製粘貼時不要改變 – Raghunandan