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;
        }

    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值