C# RSA根据modulus,exponent生成公钥
很多站点在数据提交的时候都会进行RSA加密,我们在js中可以得到n和e
长这样
var n = "D1EC51E7CEA07CB3233ADA6009006EF3EBF89EFD5CF77AAD211051D008077DC7142872B8C36EE971D4B368C79C13A6BBCB89B551A8308C68F71764C1519DEAD90B560E126B365375700CC5A2E6CF81E2A0FEEA31B53C1F8D3F3AE522DF9AB19B5C0C391D997D6DE56807328B9BBD5F6D08EA47614060177E12F65BDB95D5D6E3"; var e = "10001";
public static byte[] Hex2Byte(string byteStr) { try { byteStr = byteStr.ToUpper().Replace(" ", ""); int len = byteStr.Length / 2; byte[] data = new byte[len]; for (int i = 0; i < len; i++) { data[i] = Convert.ToByte(byteStr.Substring(i * 2, 2), 16); } return data; } catch (Exception ex) { return new byte[] { }; } }
n和e经过Hex2Byte
Convert.ToBase64String(n);
Convert.ToBase64String(n);
我们得到新值
n="0exR586gfLMjOtpgCQBu8+v4nv1c93qtIRBR0AgHfccUKHK4w27pcdSzaMecE6a7y4m1UagwjGj3F2TBUZ3q2QtWDhJrNlN1cAzFoubPgeKg/uoxtTwfjT865SLfmrGbXAw5HZl9beVoBzKLm71fbQjqR2FAYBd+EvZb25XV1uM="; e="ABAQ";
自己拼接成xml格式的publickey
<RSAKeyValue> <Modulus> 0exR586gfLMjOtpgCQBu8+v4nv1c93qtIRBR0AgHfccUKHK4w27pcdSzaMecE6a7y4m1UagwjGj3F2TBUZ3q2QtWDhJrNlN1cAzFoubPgeKg/uoxtTwfjT865SLfmrGbXAw5HZl9beVoBzKLm71fbQjqR2FAYBd+EvZb25XV1uM= </Modulus> <Exponent> AQAB </Exponent> </RSAKeyValue>
通过在线格式转换转成pem格式
http://superdry.apphb.com/tools/online-rsa-key-converter
你熟悉的公钥来咯
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDR7FHnzqB8syM62mAJAG7z6/ie /Vz3eq0hEFHQCAd9xxQocrjDbulx1LNox5wTprvLibVRqDCMaPcXZMFRnerZC1YO Ems2U3VwDMWi5s+B4qD+6jG1PB+NPzrlIt+asZtcDDkdmX1t5WgHMoubvV9tCOpH YUBgF34S9lvbldXW4wIDAQAB -----END PUBLIC KEY-----