RSA加密方法
/// <summary>
/// RSA加密
/// </summary>
/// <param name="data"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string RSASignCharSet(string data, string privateKey)
{
byte[] signatureBytes = null;
RSACryptoServiceProvider rsaCsp = LoadCertificateString(privateKey, "");
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
signatureBytes = rsaCsp.SignData(dataBytes, "SHA256");
return Convert.ToBase64String(signatureBytes);
}
private static RSACryptoServiceProvider LoadCertificateString(string strKey, string signType)
{
byte[] data = null;
//读取带
//ata = Encoding.Default.GetBytes(strKey);
data = Convert.FromBase64String(strKey);
//data = GetPem("RSA PRIVATE KEY", data);
try
{
RSACryptoServiceProvider rsa = DecodeRSAPrivateKey(data, signType);
return rsa;
}
catch (Exception ex)
{
// throw new AopException("EncryptContent = woshihaoren,zheshiyigeceshi,wanerde", ex);
}
return null;
}
private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey, string signType)
{
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
MemoryStream mem = new MemoryStream(privkey);
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
byte bt = 0;
ushort twobytes = 0;
int elems = 0;
try
{
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
binr.ReadByte(); //advance 1 byte
else if (twobytes == 0x8230)
binr.ReadInt16(); //advance 2 bytes
else
return null;
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102) //version number
return null;
bt = binr.ReadByte();
if (bt != 0x00)
return null;
//------ all private key components are Integer sequences ----
elems = GetIntegerSize(binr);
MODULUS = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
E = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
D = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
P = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
Q = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DP = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
DQ = binr.ReadBytes(elems);
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
CspParameters CspParameters = new CspParameters();
CspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
int bitLen = 1024;
if ("RSA2".Equals(signType))
{
bitLen = 2048;
}
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(bitLen, CspParameters);
RSAParameters RSAparams = new RSAParameters();
RSAparams.Modulus = MODULUS;
RSAparams.Exponent = E;
RSAparams.D = D;
RSAparams.P = P;
RSAparams.Q = Q;
RSAparams.DP = DP;
RSAparams.DQ = DQ;
RSAparams.InverseQ = IQ;
RSA.ImportParameters(RSAparams);
return RSA;
}
catch (Exception ex)
{
return null;
}
finally
{
binr.Close();
}
}
private static int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02) //expect integer
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte(); // data size in next byte
else
if (bt == 0x82)
{
highbyte = binr.ReadByte(); // data size in next 2 bytes
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt; // we already have the data size
}
while (binr.ReadByte() == 0x00)
{ //remove high order zeros in data
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
return count;
}
RSA加密方法的更多相关文章
- openssl RSA加密方法初识
作为非对称加密算法,有两对密钥 一般用法 加密结果=RSA_EN(数据,公钥); 解密结果=RSA_DE(数据,私钥); RSA填充 (RSA_public_encrypt和RSA_private_d ...
- RSA加密方法java工具类
package com.qianmi.weidian.common.util; import java.io.ByteArrayOutputStream; import java.security.K ...
- C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
因为C#的RSA加密解密只有公钥加密,私钥解密,没有私钥加密,公钥解密.在网上查了很久也没有很好的实现.BouncyCastle的文档少之又少.很多人可能会说,C#也是可以的,通过Biginteger ...
- RSA加密、解密、签名、验签的原理及方法
一.RSA加密简介 RSA加密是一种非对称加密.可以在不直接传递密钥的情况下,完成解密.这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险.是由一对密钥来进行加解密的过程,分别称为公钥和私 ...
- vue使用JSEncrypt实现rsa加密及挂载方法
挂载全局方法 使用jsencrypt进行rsa加密 原文链接:Js参数RSA加密传输,jsencrypt.js的使用 - CSDN博客* https://blog.csdn.net/p31201115 ...
- 电信级的RSA加密后的密码的破解方法
一直以来,电信通过HTTP劫持推送广告的方式已经存在了很多年了,这种手段至今并未停止.这种手段月光博客曾经有多次曝光,见<电信级的网络弹出广告>.<获取了电信恶意弹出广告的罪证> ...
- ruby的加密方法整理(des rsa加密 加签)
# coding:utf-8require 'openssl'require 'base64'#des加密并且base64编码def des_encrypt des_key, des_text des ...
- 【密码学】RSA加密 kotlin实现方法(支持任意字节长度)
这个编辑器不支持kotlin,尴尬了···· 算了,就用Java来弄吧 val 定义常量 var 定义变量 具体kotlin的开发手册详见:http://www.runoob.com/kotlin/k ...
- “不给力啊,老湿!”:RSA加密与破解
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 加密和解密是自古就有技术了.经常看到侦探电影的桥段,勇敢又机智的主角,拿着一长串毫 ...
随机推荐
- hook declined to update refs/heads/dev
提交一个项目,push的时候,报错: warning: Large files detected. remote: error: File TaodangpuAuction/TaodangpuAuct ...
- nRF24L01+如何检测信道被占用-RSSI寄存器实战分享
检测信道占用的需求场景 在使用nRF24L01模块做一对多或多对一的组网通信中,大家都会担心一个问题就是在发送的时候,希望不要有其他的模块也进行发送,因为这样就会使无线信号发生碰撞,信道被堵塞,造成通 ...
- [转]关于maven pom.xml中dependency type 为pom的应用
原文地址:http://blog.csdn.net/yao123long/article/details/49925659 dependency为什么会有type为pom,默认的值是什么?depend ...
- laravel 数据库操作之 DB facade & 查询构造器 & Eloquent ORM
<?php namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class S ...
- Oracle 12c报错:ORA-01078和LRM-00109的解决办法
Oracle 12c报错:ORA-01078和LRM-00109的解决办法 2017-12-17 10:25:30 lemon_love1 阅读数 4336 收藏 更多 分类专栏: oracle ...
- Qt在window下的环境变量PATH的配置
Qt在window下的环境变量PATH的配置 路劲: C:\Qt\Qt5.6.0\5.6\mingw49_32\bin C:\Qt\Qt5.6.0\Tools\mingw492_32\bin 发布Qt ...
- requests---requests发送json数据类型
我们都知道post请求中的body有4种数据类型,今天我们来写一篇关于requests请求json这种数据类型. 数据类型 我们都知道post和get有个大的区别就是get没有body而post存在b ...
- 201271050130-滕江南《面向对象程序设计(java)》第十一周学习总结
项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...
- lua 3 循环
while() do ... end i=10 while(i>0) do print(i) i=i-1 end repeat ... until() i=10 repeat print(i) ...
- mask-rcnn环境配置windows
安装pycocotools 这个方法非常简便 但是需要先安装git,并且同时配置一下C++的工具 https://blog.csdn.net/qq_41271957/article/details/8 ...