AES算法中 - 解密问题 [英] AES algo - Decryption Issue

查看:98
本文介绍了AES算法中 - 解密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code的AES解密,但没有取得任何成功。 我的AES算法中类是在这里。 http://pastebin.com/QtpFnW84 与实施是:

I have written code for AES decryption, but did not have any success. My AES algo class is here. http://pastebin.com/QtpFnW84 and implementation is :

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();
    }

每当我得​​到异常:

Each time i am getting exception :

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)

任何人能帮助我吗?

Can anybody help me please?

在此先感谢。

推荐答案

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

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

检查上面的链接。根据您的需要以下使用下面的reference.Modify。

Check the above links. Use the below for reference.Modify the below according to your needs.

用法

 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

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;
    }      
}

这篇关于AES算法中 - 解密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆