新浪微博、qq等javascript rsa密码加密c#实现。
2013-03-23 13:27
573 查看
可用在新浪微博最新版的登录、qq注册、qq微博注册过程中的密码加密。
按js实现翻译过来的,为了对比方便,保留着js的命名。大数计算类BigInteger是c# 4.0才提供的,如果不想用这么高的版本,这里有个开源的实现:http://www.codeproject.com/csharp/biginteger.asp
转自:http://pwg17.github.com/2012/12/16/weibo-qq-rsa-csharp/
按js实现翻译过来的,为了对比方便,保留着js的命名。大数计算类BigInteger是c# 4.0才提供的,如果不想用这么高的版本,这里有个开源的实现:http://www.codeproject.com/csharp/biginteger.asp
public class JSRSAUtil { private static Random rand = new Random(); private BigInteger n = null; private BigInteger e = null; private BigInteger pkcs1pad2(string a, int b) { if (b < a.Length + 11) { throw new Exception("Message too long for RSA"); } byte[] c = new byte[b]; int d = a.Length - 1; while (d >= 0 && b > 0) { int e = (int)a[d--]; if (e < 128) { c[--b] = Convert.ToByte(e); } else if ((e > 127) && (e < 2048)) { c[--b] = Convert.ToByte(((e & 63) | 128)); c[--b] = Convert.ToByte((e >> 6) | 192); } else { c[--b] = Convert.ToByte((e & 63) | 128); c[--b] = Convert.ToByte(((e >> 6) & 63) | 128); c[--b] = Convert.ToByte(((e >> 12) | 224)); } } c[--b] = Convert.ToByte(0); byte[] temp = new byte[1]; while (b > 2) { temp[0] = Convert.ToByte(0); while (temp[0] == 0) rand.NextBytes(temp); c[--b] = temp[0]; } c[--b] = 2; c[--b] = 0; return new BigInteger(c); } public void RSASetPublic(string a, string b) { if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)) { throw new Exception("Message too long for RSA"); } n = new BigInteger(a, 16); e = new BigInteger(b, 16); } private BigInteger RSADoPublic(BigInteger x) { return x.modPow(e, n); } public string RSAEncrypt(string a) { BigInteger tmp = pkcs1pad2(a, (n.bitCount() + 7) >> 3); tmp = RSADoPublic(tmp); string result = tmp.ToHexString(); return 0 == (result.Length & 1) ? result : "0" + result; } }
转自:http://pwg17.github.com/2012/12/16/weibo-qq-rsa-csharp/
相关文章推荐
- 新浪微博、qq rsa密码加密c#实现
- c#与JavaScript实现对用户名、密码进行RSA非对称加密
- 新浪微博登录,QQ登录网页javascript脚本实现RSA加密的原理
- c#与JavaScript实现对用户名、密码进行RSA非对称加密
- c#实现RSA加密解密
- 用RSA加密实现Web登录密码加密传输
- 用delphi实现qq在Web登录的密码客户端加密功能.
- C#中RSA加密解密和签名与验证的实现
- 用RSA加密实现Silverlight登录密码加密传输
- javascript实现base64 md5 sha1 密码加密
- 注册时候使用 RSA 实现前台对密码加密和后台解密
- C#实现京东登录密码加密POST
- 非对称加密RSA的应用及在C#中的实现
- 非对称加密RSA的应用及在C#中的实现
- 用RSA加密实现Web登录密码加密传输
- C#中RSA加密解密和签名与验证的实现 (转)
- 转载:C#中RSA加密解密和签名与验证的实现
- C#中RSA加密解密和签名与验证的实现
- 用RSA加密实现Web登录密码加密传输