【原创】C# IKVM加密解密机AES对称加密模式AES-128-ECB,补码方式 PKCS5Padding
作者:C/S框架网|www.cscode.ne  发布日期:2019-12-24 11:37:33
  【原创】C# IKVM加密解密机AES对称加密模式AES-128-ECB,补码方式 PKCS5Padding

最近对接Haier海尔集团某子系统,对方后台接口基于JAVA语言开发的,部署在Apache服务器,而我们对接的客户系统是基于C#语言开发的,WebApi接口的数字签名采用AES对称加密算法,对比提供的JAVA源码,C#的AES加密算法差异巨大,加解密结果不一致!

搞了1天!!!

百度找到对应JAVA语言的AES算法-IKVM包。在NuGet,搜索 IKVM ,安装BuGet包。

贴图图片-ikvm



C# Code:


/// <summary>
/// AES加密
/// </summary>
/// <param name="content">明文</param>
/// <param name="sKey">密钥</param>
/// <param name="urlEncode">加密结果是否需要UrlEncoded</param>
/// <returns></returns>
public static string AES_Encrypt(string content, string sKey, bool urlEncode = false)
{
   KeyGenerator kgen = KeyGenerator.getInstance("AES");
   SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
   random.setSeed(Encoding.UTF8.GetBytes(sKey));
   //random.setSeed(Encoding.ASCII.GetBytes(sKey));
   
   kgen.init(128, random);
   SecretKey secretKey = kgen.generateKey();
   byte[] enCodeFormat = secretKey.getEncoded();
   SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
   Cipher cipher = Cipher.getInstance("AES");// 创建密码器
   byte[] byteContent = Encoding.UTF8.GetBytes(content);
   cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
   
   byte[] encryptResult = cipher.doFinal(byteContent); //加密后接口
   
   //string result = Convert.ToBase64String(encryptResult, 0, encryptResult.Length);
   string result = Convert.ToBase64String(encryptResult);
   if (urlEncode)
   return java.net.URLEncoder.encode(java.net.URLEncoder.encode(result, "utf-8"), "utf-8");//进行url编码防止http传输过程出现异常
   else
   return result;
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



C# Code:


/// <summary>
/// AES解密
/// </summary>
/// <param name="content">AES加密的文本</param>
/// <param name="sKey">密钥</param>
/// <param name="isUrlEncoded">AES加密的文本是否UrlEncoded</param>
/// <returns></returns>
public static string AES_Decrypt(string content, string sKey, bool isUrlEncoded = false)
{
   if (isUrlEncoded)
   content = java.net.URLDecoder.decode(java.net.URLDecoder.decode(content, "utf-8"), "utf-8");//进行url解码
   
   byte[] contentByte = Convert.FromBase64String(content);// 先用base64解密
   KeyGenerator kgen = KeyGenerator.getInstance("AES");
   SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
   random.setSeed(Encoding.UTF8.GetBytes(sKey));
   
   kgen.init(128, random);
   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);// 初始化
   byte[] result = cipher.doFinal(contentByte);
   return System.Text.Encoding.UTF8.GetString(result);
}

//来源:C/S框架网(www.csframework.com) QQ:23404761



部署客户端依赖5个dll文件:

IKVM.OpenJDK.Core.dll
IKVM.OpenJDK.Security.dll
IKVM.OpenJDK.Tools.dll
IKVM.OpenJDK.Util.dll
IKVM.Runtime.dll



我们对接的后台采用WebApi架构:

.NET WebApi开发框架|MVC框架|后端框架|服务端框架


.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0



上一篇 下一篇