AES+BASE64双重加密,解密。私钥,向量解析。

使用AES加密,再使用base64编码加密。

具体细节请看注释。

/**
     * 加密
     *
     * @param sSrc 加密的明文
     * @param sKey 秘钥
     * @param iv 向量  16 bytes
     * @return
     * @throws Exception
     */
    public static String Encrypt(String sSrc, String sKey,String iv) throws Exception {
        if (sKey == null) {
            System.out.print("Key不能为空null");
            return null;
        }
        if (sKey.length() != 16) {
            System.out.print("Key的长度不是16位");
            return null;
        }
        if (iv.length() != 16) {
            System.out.print("iv的长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv1 = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv1);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        return new BASE64Encoder().encode(encrypted);
    }

解密,如加密相似,唯一不同就是解密的参数是待解密文件。

 

/**
     * 解密
     * @param sSrc 接收到的加密过后的字符串(带解密密文)
     * @param sKey 秘钥
     * @return
     * @throws Exception
     */
    public static String Decrypt(String sSrc, String sKey,String iv) throws Exception {
        try {
            if (sKey == null) {
                System.out.print("Key不能为空null");
                return null;
            }
            if (sKey.length() != 16) {
                System.out.print("Key的长度不是16位");
                return null;
            }
            if (iv.length() != 16) {
                System.out.print("iv的长度不是16位");
                return null;
            }
            byte[] byte1 = Base64.decode(sSrc);//先用Base64解码
            IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
            SecretKeySpec key = new SecretKeySpec(sKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
            //与加密时不同MODE:Cipher.DECRYPT_MODE
            byte[] ret = cipher.doFinal(byte1);
        return new String(ret, "utf-8");
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }


上述是我集成的方法,把加密(解密)文件、私钥、向量都作为参数进行的解析。

 

如若您想单独加密解密的话,请继续往下看。

 

/** 
     * AES加密 
     *  
     * @param content 
     * @return 
     * @throws Exception 
     */  
    public static byte[] aesEncryptToBytes(String content) throws Exception {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(LENGTH, new SecureRandom(defaultKey.getBytes()));  
        Cipher cipher = Cipher.getInstance("AES");  
        SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");  
        cipher.init(Cipher.ENCRYPT_MODE, sks);  
        return cipher.doFinal(content.getBytes(ENCODE));  
    }  
/** 
     * AES解密 
     *  
     * @param encryptBytes 
     * @return 
     * @throws Exception 
     */  
    public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {  
        KeyGenerator kgen = KeyGenerator.getInstance("AES");  
        kgen.init(LENGTH, new SecureRandom(defaultKey.getBytes()));  
        Cipher cipher = Cipher.getInstance("AES");  
        SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");  
        cipher.init(Cipher.DECRYPT_MODE, sks);  
        byte[] decryptBytes = cipher.doFinal(encryptBytes);  
        return new String(decryptBytes);  
    }  

 

/** 
     * BASE64 加密 
     *  
     * @param content 
     * @return 
     * @throws Exception 
     */  
    public static String base64Encode(byte[] bytes) {  
        return new BASE64Encoder().encode(bytes);  
    }
/** 
     * BASE64 解密 
     *  
     * @param content 
     * @return 
     * @throws Exception 
     */  
    public static byte[] base64Decode(String base64Code) throws Exception {  
        return isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
    }  
  
   

接下来测试一下:
 

 public static void main(String[] args) throws Exception{
        String pwd="123456";
        String s = MD5Utils.md5LowerCase(pwd);
        //这里使用的是md5加密后的前十六位作为私钥,后十六位作为向量
        //私钥
        String substring = s.substring(0, 16);
        //向量
        String substring1 = s.substring(16, 32);
        System.out.println("私钥 :"+substring);
        System.out.println("向量 :"+substring1);
        String mobile="18621764382";
        String encrypt1 = Encrypt(mobile, substring,substring1);
        System.out.println("加密后的密文:"+encrypt1);
        String decrypt1 = Decrypt(encrypt1, substring,substring1);
        System.out.println("解密后的明文:"+decrypt1);
    }

结果:

私钥 :e10adc3949ba59ab
向量 :be56e057f20f883e
加密后的密文:c85VnIGqV2rxsPT1lNviiA==
解密后的明文:18621764382

 

源码地址

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值