android -------- AES加密解密算法

AES加密标准又称为高级加密标准Rijndael加密法,是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准。AES的基本要求是,采用对称分组密码体制,密钥长度可以为128、192或256位,分组长度128位,算法应易在各种硬件和软件上实现。1998年NIST开始AES第一轮分析、测试和征集,共产生了15个候选算法。


AES是一种对称的加密算法,可基于相同的密钥进行加密和解密


这个网上有很多种写法,
我就在网上找了两种作为参考


1:秘钥必须为16位字符串

// 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        //此处使用BASE64做转码功能,同时能起到2次加密的作用。
        //return new Base64().encodeToString(encrypted);
        return Base64.encode(encrypted);
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            //先用base64转码
            //byte[] encrypted1 = new Base64().decode(sSrc);
            byte[] encrypted1 = Base64.decode(sSrc);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

2:加密解密的秘钥没有长度限制

//加密
    public static String encrypt(String content, String password) {  
        try {
            //将秘钥补全为128位
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            //若想改为DES加密,则需要将秘钥位数改为64位
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();  
            byte[] enCodeFormat = secretKey.getEncoded();  
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            byte[] byteContent = content.getBytes("utf-8");
            //初始化  
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //加密 
            byte[] result = cipher.doFinal(byteContent);
            //Base64转码
            return Base64.encode(result); 
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (NoSuchPaddingException e) {  
            e.printStackTrace();  
        } catch (InvalidKeyException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
        } catch (BadPaddingException e) {  
            e.printStackTrace();  
        }  
        return null;
    }

    //解密
    public static String decrypt(String content, String password) throws Exception {  
        try {
            //将秘钥补全为128位
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            //若想改为DES加密,则需要将秘钥位数改为64位
            kgen.init(128, new SecureRandom(password.getBytes()));  
            SecretKey secretKey = kgen.generateKey();  
            byte[] enCodeFormat = secretKey.getEncoded();  
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            //创建密码器             
            Cipher cipher = Cipher.getInstance("AES");
            //初始化
            cipher.init(Cipher.DECRYPT_MODE, key);
            //Base64转码
            byte[] encrypted1 = Base64.decode(content);
            //解密
            byte[] result = cipher.doFinal(encrypted1);
            //二进制转为字符串
            return new String(result, "utf-8");
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (NoSuchPaddingException e) {  
            e.printStackTrace();  
        } catch (InvalidKeyException e) {  
            e.printStackTrace();  
        } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
        } catch (BadPaddingException e) {  
            e.printStackTrace();  
        }  
        return null;  
    }

main方法

 //使用
    public static void main(String[] args) throws Exception {
        //秘钥--16位
        //AES和DES同属对称加密算法
        //1234567812345678
        String cKey = "abcdefghabcdefgh";
        //需要加密的字串
        String cSrc = "Hello 小笨蛋";
        System.out.println(cSrc);
        //加密
        String enString = DESAES.Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是:" + enString);
        //解密
        String DeString = DESAES.Decrypt(enString, cKey);
        System.out.println("解密后的字串是:" + DeString);

        String keyN = "123456789";

        String content = encrypt(cSrc,keyN);
        System.out.println("加密后的字串是:" + content);
        System.out.println("加密后的字串是:" + decrypt(content,keyN));
    }

日志:

猜你喜欢

转载自www.cnblogs.com/zhangqie/p/10979791.html
今日推荐