zoukankan      html  css  js  c++  java
  • AES与RSA加密

    AES


    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    
    namespace winfAESEncryptDecrypt
    {
        public class AES
        {
            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="data">待加密的字符数据</param>
            /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
            /// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
            /// <returns>加密后的字符</returns>
            public string EnAES(string data, string key, string iv)
            {
                if (string.IsNullOrEmpty(data)) return data;
                Aes aes = Aes.Create();
                byte[] tmp = null;
    
                ICryptoTransform encryptor = aes.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv));
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        StreamWriter writer = new StreamWriter(cs);
                        writer.Write(data);
                        writer.Flush();
                        writer.Close();
                    }
                    tmp = ms.ToArray();
                }
                return Convert.ToBase64String(tmp);
            }
    
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="data">待加密的字符数据</param>
            /// <param name="key">密匙,长度可以为:128位(byte[16]),192位(byte[24]),256位(byte[32])</param>
            /// <param name="iv">iv向量,长度必须为128位(byte[16])</param>
            /// <returns>加密后的字符</returns>
            public string DeAES(string data, string key, string iv)
            {
                if (string.IsNullOrEmpty(data)) return data;
                string result = string.Empty;
                Aes aes = Aes.Create();
    
                ICryptoTransform edcryptor = aes.CreateDecryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv));
                using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data)))
                {
                    using (CryptoStream cs = new CryptoStream(ms, edcryptor, CryptoStreamMode.Read))
                    {
                        StreamReader reader = new StreamReader(cs);
                        result = reader.ReadLine();
                        reader.Close();
                    }
                }
                aes.Clear();
                return result;
            }
        }
    
    }
    
    

    RSA


     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                string privateKey = rsa.ToXmlString(true);
                string publicKey = rsa.ToXmlString(false);
    using System;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace winfAESEncryptDecrypt
    {
        public class RSA
        {
            /// <summary>
            /// RSA加密
            /// </summary>
            /// <param name="sdata"></param>
            /// <param name="sPublicKey"></param>
            /// <returns></returns>
            public string EnRSA(string sdata, string sPublicKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(sPublicKey);
                byte[] mybyte = Encoding.UTF8.GetBytes(sdata);
                string sEnRSA = Convert.ToBase64String(rsa.Encrypt(mybyte, false));
                return sEnRSA;
            }
    
            /// <summary>
            /// RSA解密
            /// </summary>
            /// <param name="sdata"></param>
            /// <param name="sPrivateKey"></param>
            /// <returns></returns>
            public string DeRAS(string sdata, string sPrivateKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(sPrivateKey);
                byte[] mybyte = Convert.FromBase64String(sdata);
                string sDeRAS = Encoding.UTF8.GetString(rsa.Decrypt(mybyte, false));
                return sDeRAS;
            }
    
        }
    }
    



  • 相关阅读:
    springboot项目在IDEA根据不同的开发人员读取不同的配置文件
    Idea中一个服务按多个端口同时启动
    修改feign解析器替换json
    Intellij IDEA中启动多个微服务--开启Run Dashboard管理
    解决springboot乱码和window cmd乱码
    调用远程linux服务器shell脚本
    cp复制命令详解
    ftp列出具体目录的所有目录,和目录按照文件类型列出
    Linux下Redis开机自启(Centos)
    vsftpd 配置上传失败553
  • 原文地址:https://www.cnblogs.com/cxd1008/p/6395607.html
Copyright © 2011-2022 走看看