首页 > 编程语言 >C#.NET与JAVA互通之AES加密解密V2024

C#.NET与JAVA互通之AES加密解密V2024

时间:2024-06-10 23:43:35浏览次数:28  
标签:aes string V2024 C# AES System aesKey byte String

C#.NET与JAVA互通之AES加密解密V2024

 

视频:

<iframe frameborder="no" height="600" scrolling="no" src="//player.bilibili.com/player.html?isOutside=true&aid=112592987097040&bvid=BV1k1GseaEqT&cid=500001577741900&p=1" width="800"></iframe>

 

 

 

注意点:

1. KEY 和 IV 从字符串转byte数组时,双方要约定好编码,一般是UTF8。

2.明文从字符串转byte数组时,双方要约定好编码,一般是UTF8,也可以GB2312,但不能Encoding.Default。

3.加密后的结果,从byte数组转字符串时,双方要约定好编码,一般是Base64字符串。

4.NET 的PKCS7Padding 对应 JAVA 的:PKCS5Padding
5. AES128指的是密钥长度为16个字符串(16Byte * 8 = 128 bit),AES256指的是密钥长度为32个字符串(32Byte * 8 = 256 bit)。

6.AES KEY 的变种(MD5 HASH 后,正好是AES256,或截取前16位作为AES128)

 

一、C#.NET:

AesUtil:

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// AES 工具类,2024-06-07,runliuv。
    /// </summary>
    public static class AesUtil
    {

        /// <summary>
        /// AES CBC 解密
        /// </summary>
        /// <param name="decryptStr">要解密的串,base64字符串</param>
        /// <param name="aesKey">密钥</param>
        /// <param name="aesIV">IV</param>
        /// <returns></returns>
        public static string AesDecryptCBC(string decryptStr, string aesKey, string aesIV)
        {

            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
            byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);

            byte[] byteDecrypt = Convert.FromBase64String(decryptStr);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            _aes.Key = byteKEY;
            _aes.IV = byteIV;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteDecrypt, 0, byteDecrypt.Length);

            _crypto.Dispose();

            return Encoding.UTF8.GetString(decrypted);
        }

        /// <summary>
        /// AES CBC 解密,输入输出都是byte[],方便用各种编码转换成字符串
        /// </summary>
        /// <param name="byteDecrypt"></param>
        /// <param name="byteKEY"></param>
        /// <param name="byteIV"></param>
        /// <returns></returns>
        public static byte[] AesDecryptCBC(byte[] byteDecrypt, byte[] byteKEY, byte[] byteIV)
        {

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            _aes.Key = byteKEY;
            _aes.IV = byteIV;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteDecrypt, 0, byteDecrypt.Length);

            _crypto.Dispose();

            return decrypted;
        }

        /// <summary>
        /// AES CBC 加密,输出base64字符串。
        /// </summary>
        /// <param name="content"></param>
        /// <param name="aesKey"></param>
        /// <param name="aesIV"></param>
        /// <returns></returns>
        public static string AesEncryptCBC(string content, string aesKey, string aesIV)
        {

            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
            byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);

            byte[] byteContnet = Encoding.UTF8.GetBytes(content);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            _aes.Key = byteKEY;
            _aes.IV = byteIV;

            var _crypto = _aes.CreateEncryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteContnet, 0, byteContnet.Length);

            _crypto.Dispose();

            return Convert.ToBase64String(decrypted);
        }

        /// <summary>
        /// AES CBC 加密,输入输出都是byte[],方便用各种编码转换成字符串
        /// </summary>
        /// <param name="byteContnet"></param>
        /// <param name="byteKEY"></param>
        /// <param name="byteIV"></param>
        /// <returns></returns>
        public static byte[] AesEncryptCBC(byte[] byteContnet, byte[] byteKEY, byte[] byteIV)
        {

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.CBC;

            _aes.Key = byteKEY;
            _aes.IV = byteIV;

            var _crypto = _aes.CreateEncryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteContnet, 0, byteContnet.Length);

            _crypto.Dispose();

            return decrypted;
        }


        public static string AesEncryptECB(string content, string aesKey)
        {

            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);

            byte[] byteContnet = Encoding.UTF8.GetBytes(content);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;

            _aes.Key = byteKEY;
            

            var _crypto = _aes.CreateEncryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteContnet, 0, byteContnet.Length);

            _crypto.Dispose();

            return Convert.ToBase64String(decrypted);
        }

        public static string AesDecryptECB(string decryptStr, string aesKey)
        {

            byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);

            byte[] byteDecrypt = Convert.FromBase64String(decryptStr);

            var _aes = new RijndaelManaged();
            _aes.Padding = PaddingMode.PKCS7;
            _aes.Mode = CipherMode.ECB;

            _aes.Key = byteKEY;

            var _crypto = _aes.CreateDecryptor();
            byte[] decrypted = _crypto.TransformFinalBlock(
                byteDecrypt, 0, byteDecrypt.Length);

            _crypto.Dispose();

            return Encoding.UTF8.GetString(decrypted);
        }

    }
}

 

 

HashUtil

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CommonUtils
{
    public static class HashUtil
    {

        public static string GetMd5(string src)
        {
            MD5 md =   MD5.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(src);
            byte[] buffer2 = md.ComputeHash(bytes);
            string str = "";
            for (int i = 0; i < buffer2.Length; i++)
            {
                str = str + buffer2[i].ToString("x2");
            }
            return str;

        }

        public static IDictionary<string, string> ModelToDic<T1>(T1 cfgItem)
        {
            IDictionary<string, string> sdCfgItem = new Dictionary<string, string>();

            System.Reflection.PropertyInfo[] cfgItemProperties = cfgItem.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            foreach (System.Reflection.PropertyInfo item in cfgItemProperties)
            {
                string name = item.Name;
                object value = item.GetValue(cfgItem, null);
                if (value != null && (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    sdCfgItem.Add(name, value.ToString());
                }
            }

            return sdCfgItem;
        }

        public static IDictionary<string, string> AsciiDictionary(IDictionary<string, string> sArray)
        {
            IDictionary<string, string> asciiDic = new Dictionary<string, string>();
            string[] arrKeys = sArray.Keys.ToArray();
            Array.Sort(arrKeys, string.CompareOrdinal);
            foreach (var key in arrKeys)
            {
                string value = sArray[key];
                asciiDic.Add(key, value);
            }
            return asciiDic;
        }

        public static string BuildQueryString(IDictionary<string, string> sArray)
        {

            //拼接 K=V&A=B&c=1 这种URL

            StringBuilder sc = new StringBuilder();

            foreach (var item in sArray)
            {
                string name = item.Key;
                string value = item.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    sc.AppendFormat("{0}={1}&", name, value);
                }

            }

            string fnlStr = sc.ToString();
            fnlStr = fnlStr.TrimEnd('&');

            return fnlStr;
        }

    }
}

 

 

 

.NET 使用:

 

using CommonUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //.NET 的PKCS7 对应 JAVA 的:PKCS5Padding
                TestAesCbc();

                TestAesECB();

                //JAVA加密,.NET 解密
                string javaStr = "FfRdTUVCFSknpYNXoMHQOAQNEqkHY1uU19u/3wWH6Js=";
                Console.WriteLine("JAVA加密后的字符串:" + javaStr);
                string aesKey = "1234567890123456";//AES KEY 与 JAVA 保持一致
                string decryptedStr = AesUtil.AesDecryptECB(javaStr, aesKey);
                Console.WriteLine("自加,自解:" + decryptedStr);


                //有的AES加密,不是直接把字符串当KEY使用,而是把原密钥 MD5 HASH后,作为AES 的KEY。
                TestAesKeyTrap();
            }
            catch (Exception ex)
            {
                Console.WriteLine("ex!" + ex.Message);
            }
            Console.WriteLine("结束!");
            Console.ReadKey();
        }

        static void TestAesCbc()
        {
            Console.WriteLine("-- TestAesCbc --" );
            string aesKey = "1234567890123456";//16位或32位
            string aesIV = "abcdefghABCDEFGH";

            string aa = "这边是 .net aes 加密";
            Console.WriteLine("待加密字符串:" + aa);
            string encryptedStr = AesUtil.AesEncryptCBC(aa, aesKey, aesIV);
            Console.WriteLine("加密字符串:" + encryptedStr);
            //自加,自解
            string decryptedStr = AesUtil.AesDecryptCBC(encryptedStr, aesKey, aesIV);
            Console.WriteLine("自加,自解:" + decryptedStr);
        }

        static void TestAesECB()
        {
            Console.WriteLine("-- TestAesECB --");

            string aesKey = "1234567890123456";//16位或32位

            string aa = ".net aes 加密";
            Console.WriteLine("待加密字符串:" + aa);
            string encryptedStr = AesUtil.AesEncryptECB(aa, aesKey);
            Console.WriteLine("加密字符串:" + encryptedStr);
            //自加,自解
            string decryptedStr = AesUtil.AesDecryptECB(encryptedStr, aesKey);
            Console.WriteLine("自加,自解:" + decryptedStr);
        }

        /// <summary>
        /// AES KEY 的变种(MD5 HASH 后,正好是AES256,或截取前16位作为AES128)
        /// </summary>
        static void TestAesKeyTrap()
        {
            Console.WriteLine("-- TestAesECB --");

            string orgKey = "HelloWorld";//不够16位或32位
            //string aesKey = "HelloWorld";//不够16位或32位

            string aesKey = HashUtil.GetMd5(orgKey).ToLower().Substring(16);
            Console.WriteLine("GetMd5后的长度:" + aesKey.Length.ToString());

            string aa = ".net aes 加密";
            Console.WriteLine("待加密字符串:" + aa);
            string encryptedStr = AesUtil.AesEncryptECB(aa, aesKey);
            Console.WriteLine("加密字符串:" + encryptedStr);
            //自加,自解
            string decryptedStr = AesUtil.AesDecryptECB(encryptedStr, aesKey);
            Console.WriteLine("自加,自解:" + decryptedStr);
        }
    }
}

 

 

 

二、JAVA:

AesUtil:

 

 

package org.runliuv;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class AesUtil {

    private static final String charset = "UTF-8";

    /**
     * 加密
     * @param content
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String AesEncryptCBC(String content, String key, String iv)
            throws Exception {

        //明文
        byte[] contentBytes = content.getBytes(charset);

        //AES KEY
        byte[] keyBytes = key.getBytes(charset);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

        //AES IV
        byte[] initParam = iv.getBytes(charset);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
        byte[] byEnd = cipher.doFinal(contentBytes);

        //加密后的byte数组转BASE64字符串
        String strEnd = Base64.getEncoder().encodeToString(byEnd);
        return strEnd;
    }

    /**
     * 解密
     * @param content
     * @param key
     * @param iv
     * @return
     * @throws Exception
     */
    public static String AesDecryptCBC(String content, String key, String iv)
            throws Exception {
        //反向解析BASE64字符串为byte数组
        byte[] encryptedBytes = Base64.getDecoder().decode(content);

        //AES KEY
        byte[] keyBytes = key.getBytes(charset);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

        //AES IV
        byte[] initParam = iv.getBytes(charset);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
        byte[] byEnd = cipher.doFinal(encryptedBytes);

        //加密后的byte数组直接转字符串
        String strEnd = new String(byEnd, charset);
        return strEnd;
    }

    public static String AesEncryptECB(String content, String key)
            throws Exception {

        //明文
        byte[] contentBytes = content.getBytes(charset);

        //AES KEY
        byte[] keyBytes = key.getBytes(charset);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] byEnd = cipher.doFinal(contentBytes);

        //加密后的byte数组转BASE64字符串
        String strEnd = Base64.getEncoder().encodeToString(byEnd);
        return strEnd;
    }

    public static String AesDecryptECB(String content, String key)
            throws Exception {
        //反向解析BASE64字符串为byte数组
        byte[] encryptedBytes = Base64.getDecoder().decode(content);

        //AES KEY
        byte[] keyBytes = key.getBytes(charset);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] byEnd = cipher.doFinal(encryptedBytes);

        //加密后的byte数组直接转字符串
        String strEnd = new String(byEnd, charset);
        return strEnd;
    }

}

 

 

java使用:

 

package org.runliuv;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {

        try
        {
            TestAesCbc();

            TestAesECB();

            //.net aes cbc 加,JAVA 解密
            String netStr="Mrs6Ufo2Y4ulWiksFSWFnUNKVSO5COruAlgQ5ws7FL0=";
            System.out.println("-- .net 加密后的内容:" + netStr);
            String aesKey = "1234567890123456";//与.net 保持一致
            String aesIV = "abcdefghABCDEFGH";//与.net 保持一致

            String decryptedStr = AesUtil.AesDecryptCBC(netStr, aesKey, aesIV);
            System.out.println(".net aes cbc 加,JAVA 解密:" + decryptedStr);

        }catch (Exception ex){

            System.out.println( "ex!"+ex.getMessage() );
        }
        System.out.println( "结束!" );
    }

    static void  TestAesCbc(){

        try {
            System.out.println("-- TestAesCbc --" );

            //16位 (32位,jdk8 老版本,需要放JAR包才支持32位的)
            String aesKey = "1234567890123456";
            String aesIV = "abcdefghABCDEFGH";

            String aa = "java aes 加密,2024-06-07。";
            System.out.println("待加密字符串:" + aa);
            String encryptedStr = AesUtil.AesEncryptCBC(aa, aesKey, aesIV);
            System.out.println("加密字符串:" + encryptedStr);
            //自加,自解
            String decryptedStr = AesUtil.AesDecryptCBC(encryptedStr, aesKey, aesIV);
            System.out.println("自加,自解:" + decryptedStr);

        }catch (Exception ex){

            System.out.println( "ex!"+ex.getMessage() );
        }

    }

    static void  TestAesECB(){

        try {
            System.out.println("-- TestAesECB --" );

            //16位 (32位,jdk8 老版本,需要放JAR包才支持32位的)
            String aesKey = "1234567890123456";

            String aa = "java aes 加密,2024-06-07。";
            System.out.println("待加密字符串:" + aa);
            String encryptedStr = AesUtil.AesEncryptECB(aa, aesKey);
            System.out.println("加密字符串:" + encryptedStr);
            //自加,自解
            String decryptedStr = AesUtil.AesDecryptECB(encryptedStr, aesKey);
            System.out.println("自加,自解:" + decryptedStr);

        }catch (Exception ex){

            System.out.println( "ex!"+ex.getMessage() );
        }

    }
}

 

 

 

-

标签:aes,string,V2024,C#,AES,System,aesKey,byte,String
From: https://www.cnblogs.com/runliuv/p/18237209

相关文章

  • 服务器IO多路复用的select和poll的区别以及监听套接字select函数的四个宏操作
    目录知识补给站对文件描述符集合操作的四个宏操作服务器IO多路复用中的select和poll的区别知识补给站对文件描述符集合操作的四个宏操作对文件描述符集合操作的四个宏操作在select函数中起着关键的作用,它们用于初始化、添加、删除和检查文件描述符集合中的元素。这四个宏为:FD_......
  • DoNet Core的启动过程-WebApplicationBuilder
     1.前言 在NET6开始做ASP.NETCore的开发,我们首先要看的是启动过程,而WebApplication和WebApplicationBuilder类是启动过程好不开的类,WebApplicationBuilder来引导启动,这和前面Core的版本引导启动分为Program和Startup是不同的,.NET6及以上版本直接使用Program中引导启动,而We......
  • LeetCode 算法:缺失的第一个正数c++
    原题链接......
  • 如何解决系统时间同步失败:Unable to sync time with NTP server
    如何解决系统时间同步失败:UnabletosynctimewithNTPserver......
  • 【讲解下Chrome DevTools,什么是Chrome DevTools?】
    ......
  • 大学体育(二)(华中科技大学) 中国大学MOOC答案2024版100分完整版
    大学体育(二)(华中科技大学)中国大学MOOC答案2024版100分完整版有氧运动有氧运动单元测验1、世界卫生组织对18-64岁年龄组成年人的运动建议是:每周至少()分钟的中等强度有氧身体活动,或者每周至少()分钟的较高强度有氧身体活动,或中等和较高强度两种活动相当量的组合。A:150......
  • C++笔记
    c++一、基础(一)C++初识1.注释//1.单行注释,上方或末尾/*2.多行注释,上方*//* 3.main是一个程序的入口 每个程序都有必须有这么一个函数 有且仅有一个 默认return0,程序状态正常*/// ;是语句结束// ;;;;多个空语句// l+(r-1)/2比(l+r)/2比l+r>>1更安全......
  • C语言详解(文件操作)2
    Hi~!这里是奋斗的小羊,很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎~~......
  • redis-benchmark 基准测试
    我们可以通过redis自带工具redis-benchmark来对redis服务器进行性能测试。我们可以通过简单的redis-benchmark命令直接对本地部署的redis进行性能测试,不用输入任何的参数。默认情况下,redis-benchmark会向redis服务器使用50个并发连接发送共100000个请求。......
  • scoop中使用aria2加速下载
    scoop中使用aria2加速下载scoop为了加快下载速度,支持使用aria2加速下载。下载aria2scoopinstallmain/aria2scoopinstallextras/aria-ng-gui安装一个使用aria2加锁并带有GUI界面的下载器aria-ng(该软件内部集成了aria,不需要再额外安装)下载之后,在scoop中开启aria2加速下......