• AES256加密解密


    加密解密方法:

     1 public class AES256Cipher
     2     {
     3         public String AES_encrypt(String Input, String key)
     4         {
     5             RijndaelManaged aes = new RijndaelManaged();
     6             aes.KeySize = 256;
     7             aes.BlockSize = 128;
     8             aes.Mode = CipherMode.CBC;
     9             aes.Padding = PaddingMode.PKCS7;
    10             //MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
    11             //byte[] tdesKey = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
    12             aes.Key = Encoding.UTF8.GetBytes(key); //tdesKey;
    13             aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    14 
    15             var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
    16             byte[] xBuff = null;
    17             using (var ms = new MemoryStream())
    18             {
    19                 using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
    20                 {
    21                     byte[] xXml = Encoding.UTF8.GetBytes(Input);
    22                     cs.Write(xXml, 0, xXml.Length);
    23                 }
    24 
    25                 xBuff = ms.ToArray();
    26             }
    27 
    28             String Output = Convert.ToBase64String(xBuff);
    29             return Output;
    30         }
    31 
    32         public String AES_decrypt(String Input, String key)
    33         {
    34             RijndaelManaged aes = new RijndaelManaged();
    35             aes.KeySize = 256;
    36             aes.BlockSize = 128;
    37             aes.Mode = CipherMode.CBC;
    38             aes.Padding = PaddingMode.PKCS7;
    39             //MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
    40             //byte[] tdesKey = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
    41             aes.Key = Encoding.UTF8.GetBytes(key);
    42             aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    43 
    44             var decrypt = aes.CreateDecryptor();
    45             byte[] xBuff = null;
    46             using (var ms = new MemoryStream())
    47             {
    48                 using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
    49                 {
    50                     byte[] xXml = Convert.FromBase64String(Input);
    51                     cs.Write(xXml, 0, xXml.Length);
    52                 }
    53 
    54                 xBuff = ms.ToArray();
    55             }
    56 
    57             String Output = Encoding.UTF8.GetString(xBuff);
    58             return Output;
    59         }
    60     }

    后台调用:

    public JsonResult AES_encrypt(string inputStr)
            {
    
                AES256Cipher aes = new AES256Cipher();
                string encode = aes.AES_encrypt(inputStr, key);
                return Json(encode);
            }
    
            public JsonResult AES_decrypt(string inputStr)
            {
                AES256Cipher aes = new AES256Cipher();
                string decode = aes.AES_decrypt(inputStr, key);
                return Json(decode);
            }

    前端页面:

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>AES256</title>
        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    </head>
    <body>
        <div> 
            输入字符:<input type="text" value="" id="inputStr" />
            <input type="button" value="加密" id="encrypt" />
            <input type="button" value="解密" id="decrypt" />
            <textarea id="txtarea"></textarea>
        </div>
        <script type="text/javascript">
            $(function () {
                $("#encrypt").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "/Home/AES_encrypt",   
                        data: { "inputStr": $("#inputStr").val() },  
                        dataType: "json",
                        success: function (data) {
                            $("#txtarea").text(data);
                        },
                        error: function (json) {
                            alert("数据错误");
                        }
                    });  
                });
    
                $("#decrypt").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "/Home/AES_decrypt",
                        data: { "inputStr": $("#inputStr").val() },  
                        dataType: "json",
                        //async: false,
                        success: function (data) {
                            $("#txtarea").text(data);
                        }
                    });
                });
            });
        </script>
    </body>
    </html>

    加密界面如图:

    解密界面如图:

  • 相关阅读:
    JDK内置工具使用(jps、jstack、jmap、jstat)
    解决mybatis嵌套查询使用PageHelper分页不准确
    MySQL变量的使用
    java lambda求和最值
    解决ROS中PLUGINLIB_DECLARE_CLASS错误
    由crt和key文件生成keystore文件
    转载从java进程里dump出类的class文件的小工具--dumpclass
    oracle数据库创建实例
    广告 Spring Boot整合Jasypt增强应用安全
    PostgreSQL10基础(4)插件安装(转载)
  • 原文地址:https://www.cnblogs.com/yinzhou/p/7510611.html
Copyright © 2020-2023  润新知