java,C#之间的ASE加密实现

工作中遇到不同导入导出的问题,而且导入导出都需要进行ASE加密和解密,所以整理一下这方面的资料,网上也有类似资料,但是很多可能不是你所需要的,而且有可能是错误的,以下代码经过运行测试,请放心使用

java环境:

  • sdk 1.7

C#代码实现:

        /// <summary>
        ///  AES 加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesEncrypt(string str, string key,string iv)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = System.Text.Encoding.UTF8.GetBytes(key),
                IV = Encoding.UTF8.GetBytes(iv),
                Mode = System.Security.Cryptography.CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        ///  AES 解密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesDecrypt(string str, string key,string iv)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                IV = Encoding.UTF8.GetBytes(iv),
                Mode = System.Security.Cryptography.CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

Java代码实现:


   public static String aesEncrypt(String str, String key,String iv) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");#见注释
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"),new IvParameterSpec(iv.getBytes("utf-8")));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        String res = new BASE64Encoder().encode(bytes);
        return res.replace("\r\n", "");
    }

    public static String aesDecrypt(String str, String key,String iv) throws Exception {
    	try {
    	     if (str == null || key == null) return null;
    	     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    	     cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"),new IvParameterSpec(iv.getBytes("utf-8")));
    	     byte[] bytes = new BASE64Decoder().decodeBuffer(str);
    	     bytes = cipher.doFinal(bytes);
    	     return new String(bytes);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
       
    }
注释:

1、java中PKCS5Padding和PKCS7Padding是一样的,但是使用PKCS7Padding需要特殊处理,很多时候可能会调试不通过,建议直接使用PKCS5Padding

2、sun.misc.BASE64Decoder在使用的时候如果编译不通过,可以按照以下网址进行处理:

      http://jingyan.baidu.com/article/f71d6037ad5b111ab741d166.html

3、第一次处理ASE加密、解密的时候,代码往往会报以下错误:

     java.security.InvalidKeyException: Illegal key size

     这是因为美国对软件出口的控制导致,可以按照以下网址内容进行处理:

    http://blog.csdn.net/wangjunjun2008/article/details/50847426

运行结果测试:



经过确认,java加密后和C#加密后字符串内容一致,解密信息也完全一致



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值