公钥 "010001" M是 modulus
A3A69317FB92A534912A0999A7EEE826358C05F434C5E1E
DB61C68E882CE52F7573FA44CE46E858673A8A328E17712F
DAAECF383F13ECC1FD9D1505D2F23C983AD36F951788DEE30F1A
E2A34F2DB13E46C409980A5467E05C7667AAD896464ABB073AA01AAF
E130E28FA4D3D6A57ECA8422A482E22C5E0BA67434160B95A68DF
怎么利用自带的类去实现加密呢
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters para = new RSAParameters();
6 个解决方案
#1
MSDN提供的列子里面 RSAParameters Exponent 是三个字节 搞的非常蛋疼
#2
"010001"是3个字节。exponent和modulus正好提供了一个公钥(只能用来加密)。
class Program
{
static void Main(string[] args)
{
string exponent = "010001";
string modulus =
"A3A69317FB92A534912A0999A7EEE826358C05F434C5E1E" +
"DB61C68E882CE52F7573FA44CE46E858673A8A328E17712F" +
"DAAECF383F13ECC1FD9D1505D2F23C983AD36F951788DEE30F1A" +
"E2A34F2DB13E46C409980A5467E05C7667AAD896464ABB073AA01AAF" +
"E130E28FA4D3D6A57ECA8422A482E22C5E0BA67434160B95A68DF";
RSAParameters rsaParameters = new RSAParameters()
{
Exponent = FromHex(exponent), // new byte[] {01, 00, 01}
Modulus = FromHex(modulus), // new byte[] {A3, A6, ...}
};
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParameters);
byte[] sample = rsa.Encrypt(Encoding.UTF8.GetBytes("hello world"), false);
}
static byte[] FromHex(string hex)
{
if (string.IsNullOrEmpty(hex) || hex.Length % 2 != 0) throw new ArgumentException("not a hexidecimal string");
List<byte> bytes = new List<byte>();
for (int i = 0; i < hex.Length; i += 2)
{
bytes.Add(Convert.ToByte(hex.Substring(i, 2), 16));
}
return bytes.ToArray();
}
}
#3
试试这个:
RSAParameters Exponent 是三个字节,一般固定是0x01, 0, 0x01
byte[] plainData = new byte[117];// 明文数据;
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
RSAParameters rparam = new RSAParameters();
rparam.Modulus = mods;
rparam.Exponent = new byte[] { 1, 0, 1 };
rsaPub.ImportParameters(rparam);
byte[] enData = rsaPub.Encrypt(plainData, false);
RSAParameters Exponent 是三个字节,一般固定是0x01, 0, 0x01
byte[] plainData = new byte[117];// 明文数据;
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
RSAParameters rparam = new RSAParameters();
rparam.Modulus = mods;
rparam.Exponent = new byte[] { 1, 0, 1 };
rsaPub.ImportParameters(rparam);
byte[] enData = rsaPub.Encrypt(plainData, false);
#4
补充下,上面的mods就是你的modulus,把你的那个字符串转换成字节,你的那个字符串应该是两个字符为一个字节。
#5
疑问为什么JS加密后是256位的字符串呀
#6
搞定结贴给分 原来 是要转换成HEX 吼吼 结贴给分
#1
MSDN提供的列子里面 RSAParameters Exponent 是三个字节 搞的非常蛋疼
#2
"010001"是3个字节。exponent和modulus正好提供了一个公钥(只能用来加密)。
class Program
{
static void Main(string[] args)
{
string exponent = "010001";
string modulus =
"A3A69317FB92A534912A0999A7EEE826358C05F434C5E1E" +
"DB61C68E882CE52F7573FA44CE46E858673A8A328E17712F" +
"DAAECF383F13ECC1FD9D1505D2F23C983AD36F951788DEE30F1A" +
"E2A34F2DB13E46C409980A5467E05C7667AAD896464ABB073AA01AAF" +
"E130E28FA4D3D6A57ECA8422A482E22C5E0BA67434160B95A68DF";
RSAParameters rsaParameters = new RSAParameters()
{
Exponent = FromHex(exponent), // new byte[] {01, 00, 01}
Modulus = FromHex(modulus), // new byte[] {A3, A6, ...}
};
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(rsaParameters);
byte[] sample = rsa.Encrypt(Encoding.UTF8.GetBytes("hello world"), false);
}
static byte[] FromHex(string hex)
{
if (string.IsNullOrEmpty(hex) || hex.Length % 2 != 0) throw new ArgumentException("not a hexidecimal string");
List<byte> bytes = new List<byte>();
for (int i = 0; i < hex.Length; i += 2)
{
bytes.Add(Convert.ToByte(hex.Substring(i, 2), 16));
}
return bytes.ToArray();
}
}
#3
试试这个:
RSAParameters Exponent 是三个字节,一般固定是0x01, 0, 0x01
byte[] plainData = new byte[117];// 明文数据;
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
RSAParameters rparam = new RSAParameters();
rparam.Modulus = mods;
rparam.Exponent = new byte[] { 1, 0, 1 };
rsaPub.ImportParameters(rparam);
byte[] enData = rsaPub.Encrypt(plainData, false);
RSAParameters Exponent 是三个字节,一般固定是0x01, 0, 0x01
byte[] plainData = new byte[117];// 明文数据;
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
RSAParameters rparam = new RSAParameters();
rparam.Modulus = mods;
rparam.Exponent = new byte[] { 1, 0, 1 };
rsaPub.ImportParameters(rparam);
byte[] enData = rsaPub.Encrypt(plainData, false);
#4
补充下,上面的mods就是你的modulus,把你的那个字符串转换成字节,你的那个字符串应该是两个字符为一个字节。
#5
疑问为什么JS加密后是256位的字符串呀
#6
搞定结贴给分 原来 是要转换成HEX 吼吼 结贴给分