IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> RSA加密 之c# -> 正文阅读

[网络协议]RSA加密 之c#

C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider?
DESCryptoServiceProvider 是用于对称加密 RSACryptoServiceProvider是用于非对称加密?
对称加密的意思:有一个密钥 相当于加密算法,加密用它来加密,解密也需要用到它。因为加密解密都是用同一个密钥所以叫对称加密。 对称加密有一个坏处只要拥有密钥的人都可以解密。?
非对称加密:就是有2个密钥,一个是公钥,一个是私钥,私钥是自己的,不能随便给人,公钥随便给,无所谓。一般是别人用你的公钥加密,然后把密文给你,你用你的私钥解密,这样一样加密和解密不是同一个密钥,所以叫非对称。

1.rsa

/// <summary>
        /// 获取加密所使用的key,RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
        /// </summary>
        public static void GetKey(ref string PublicKey ,fer stringPrivateKey )
        {
            string PublicKey = string.Empty;
            string PrivateKey = string.Empty;
            RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
            PublicKey = rSACryptoServiceProvider.ToXmlString(false); // 获取公钥,用于加密
            PrivateKey = rSACryptoServiceProvider.ToXmlString(true); // 私匙,
用于解密

}

?/// <summary>
? ? ? ? /// RSA加密
? ? ? ? /// </summary>
? ? ? ? /// <param name="publickey">公钥</param>
? ? ? ? /// <param name="content">要加密的内容</param>
? ? ? ? /// <returns>加密后的内容</returns>
? ? ? ? public static string RSAEncrypt(string publickey, string content)
? ? ? ? {
? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
? ? ? ? ? ? byte[] cipherbytes;
? ? ? ? ? ? rsa.FromXmlString(publickey);
? ? ? ? ? ? cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

? ? ? ? ? ? return Convert.ToBase64String(cipherbytes);
? ? ? ? }

?/// <summary>
? ? ? ? /// RSA解密
? ? ? ? /// </summary>
? ? ? ? /// <param name="privatekey">私钥</param>
? ? ? ? /// <param name="content">要解密的内容</param>
? ? ? ? /// <returns>解密后的内容</returns>
? ? ? ? public static string RSADecrypt(string privatekey, string content)
? ? ? ? {
? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
? ? ? ? ? ? byte[] cipherbytes;
? ? ? ? ? ? rsa.FromXmlString(privatekey);
? ? ? ? ? ? cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

? ? ? ? ? ? return Encoding.UTF8.GetString(cipherbytes);
? ? ? ? }

2.DES

 /// <summary>
        /// DES加密/解密
        /// </summary>
        /// <param name="data">加密/解密数据</param>
        /// <param name="key">秘钥</param>
        /// <param name="keyIV">向量</param>
        /// <param name="isEncrypt">true加密,false解密</param>
        /// <returns></returns>
        public static byte[] EncryptOrDecrypt(byte[] data, byte[] key, byte[] keyIV, bool isEncrypt)
        {
            DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
            if (isEncrypt)// 加密
            {
                desP.Key = key;
                desP.IV = keyIV;
                ICryptoTransform desencrypt = desP.CreateEncryptor(key, keyIV);
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return result;
            }
            else // 解密
            {
                desP.Key = key;
                desP.IV = keyIV;
                ICryptoTransform desencrypt = desP.CreateDecryptor(key, keyIV);
                byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
                return result;
            }
        }

        /// <summary>
        /// 创建随机秘钥
        /// </summary>
        /// <returns></returns>
        public static byte[] CreateKey()
        {
            DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
            desP.GenerateKey();
            return desP.Key;
        }

        /// <summary>
        /// 创建随机向量
        /// </summary>
        /// <returns></returns>
        public static byte[] CreateIV()
        {
            DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
            desP.GenerateIV();
            return desP.IV;
        }

注:自己做的项目时? 是自己创建自己的 公钥和私钥? ? ?当对接别人项目时? 或许别人给你的是PEM文件? ?格式? ? ? -----BEGIN PUBLIC KEY-----? ? ?公钥的
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -------------------
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-----END PUBLIC KEY-----? ? ?

需要提取其中的公钥

/// <summary>
?? ??? ?/// 用PEM格式密钥对创建RSA,支持PKCS#1、PKCS#8格式的PEM
?? ??? ?/// 出错将会抛出异常
?? ??? ?/// </summary>
?? ??? ?/// <param name="pem">读出的pen文本</param>
?? ??? ?/// <returns>返回公钥文本</returns>
?? ??? ?static public RSA_PEM FromPEM(string pem)
?? ??? ?{
?? ??? ??? ?RSA_PEM param = new RSA_PEM();

?? ??? ??? ?var base64 = _PEMCode.Replace(pem, "");
?? ??? ??? ?byte[] data = null;
?? ??? ??? ?try { data = Convert.FromBase64String(base64); } catch { }
?? ??? ??? ?if (data == null)
?? ??? ??? ?{
?? ??? ??? ??? ?throw new Exception("PEM内容无效");
?? ??? ??? ?}
?? ??? ??? ?var idx = 0;

?? ??? ??? ?//读取长度
?? ??? ??? ?Func<byte, int> readLen = (first) => {
?? ??? ??? ??? ?if (data[idx] == first)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?idx++;
?? ??? ??? ??? ??? ?if (data[idx] == 0x81)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?idx++;
?? ??? ??? ??? ??? ??? ?return data[idx++];
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (data[idx] == 0x82)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?idx++;
?? ??? ??? ??? ??? ??? ?return (((int)data[idx++]) << 8) + data[idx++];
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (data[idx] < 0x80)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?return data[idx++];
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?throw new Exception("PEM未能提取到数据");
?? ??? ??? ?};
?? ??? ??? ?//读取块数据
?? ??? ??? ?Func<byte[]> readBlock = () => {
?? ??? ??? ??? ?var len = readLen(0x02);
?? ??? ??? ??? ?if (data[idx] == 0x00)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?idx++;
?? ??? ??? ??? ??? ?len--;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?var val = new byte[len];
?? ??? ??? ??? ?for (var i = 0; i < len; i++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?val[i] = data[idx + i];
?? ??? ??? ??? ?}
?? ??? ??? ??? ?idx += len;
?? ??? ??? ??? ?return val;
?? ??? ??? ?};
?? ??? ??? ?//比较data从idx位置开始是否是byts内容
?? ??? ??? ?Func<byte[], bool> eq = (byts) => {
?? ??? ??? ??? ?for (var i = 0; i < byts.Length; i++, idx++)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (idx >= data.Length)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?if (byts[i] != data[idx])
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return true;
?? ??? ??? ?};


?? ??? ??? ?if (pem.Contains("PUBLIC KEY"))
?? ??? ??? ?{
?? ??? ??? ??? ?/****使用公钥****/
?? ??? ??? ??? ?//读取数据总长度
?? ??? ??? ??? ?readLen(0x30);

?? ??? ??? ??? ?//检测PKCS8
?? ??? ??? ??? ?var idx2 = idx;
?? ??? ??? ??? ?if (eq(_SeqOID))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?//读取1长度
?? ??? ??? ??? ??? ?readLen(0x03);
?? ??? ??? ??? ??? ?idx++;//跳过0x00
?? ??? ??? ??? ??? ??? ? ?//读取2长度
?? ??? ??? ??? ??? ?readLen(0x30);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?idx = idx2;
?? ??? ??? ??? ?}

?? ??? ??? ??? ?//Modulus
?? ??? ??? ??? ?param.Key_Modulus = readBlock();

?? ??? ??? ??? ?//Exponent
?? ??? ??? ??? ?param.Key_Exponent = readBlock();
?? ??? ??? ?}
?? ??? ??? ?else if (pem.Contains("PRIVATE KEY"))
?? ??? ??? ?{
?? ??? ??? ??? ?/****使用私钥****/
?? ??? ??? ??? ?//读取数据总长度
?? ??? ??? ??? ?readLen(0x30);

?? ??? ??? ??? ?//读取版本号
?? ??? ??? ??? ?if (!eq(_Ver))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?throw new Exception("PEM未知版本");
?? ??? ??? ??? ?}

?? ??? ??? ??? ?//检测PKCS8
?? ??? ??? ??? ?var idx2 = idx;
?? ??? ??? ??? ?if (eq(_SeqOID))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?//读取1长度
?? ??? ??? ??? ??? ?readLen(0x04);
?? ??? ??? ??? ??? ?//读取2长度
?? ??? ??? ??? ??? ?readLen(0x30);

?? ??? ??? ??? ??? ?//读取版本号
?? ??? ??? ??? ??? ?if (!eq(_Ver))
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?throw new Exception("PEM版本无效");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?idx = idx2;
?? ??? ??? ??? ?}

?? ??? ??? ??? ?//读取数据
?? ??? ??? ??? ?param.Key_Modulus = readBlock();
?? ??? ??? ??? ?param.Key_Exponent = readBlock();
?? ??? ??? ??? ?int keyLen = param.Key_Modulus.Length;
?? ??? ??? ??? ?param.Key_D = BigL(readBlock(), keyLen);
?? ??? ??? ??? ?keyLen = keyLen / 2;
?? ??? ??? ??? ?param.Val_P = BigL(readBlock(), keyLen);
?? ??? ??? ??? ?param.Val_Q = BigL(readBlock(), keyLen);
?? ??? ??? ??? ?param.Val_DP = BigL(readBlock(), keyLen);
?? ??? ??? ??? ?param.Val_DQ = BigL(readBlock(), keyLen);
?? ??? ??? ??? ?param.Val_InverseQ = BigL(readBlock(), keyLen);
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?throw new Exception("pem需要BEGIN END标头");
?? ??? ??? ?}

?? ??? ??? ?return param;
?? ??? ?}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-11-19 17:56:53  更:2021-11-19 17:58:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年7日历 -2024/7/3 21:21:53-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码