AES算法 - 解密问题

时间:2013-06-13 05:04:34

标签: android aes

我已经为AES解密编写了代码,但没有任何成功。 我的AES算法课就在这里。 http://pastebin.com/QtpFnW84 和实施是:

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)
是的,有人可以帮帮我吗?

提前致谢。

1 个答案:

答案 0 :(得分: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;
    }      
}