当前位置: 移动技术网 > IT编程>开发语言>c# > RSA 登陆加密与解密

RSA 登陆加密与解密

2019年10月18日  | 移动技术网IT编程  | 我要评论

  最近公司项目验收后,客户请来的信息安全技术人员对我们的网站进行了各种安全测试与排查问题,其中就有一个登陆时的加密问题。本来如果只是单纯的加密,可以直接在前台用md5加密,将加密的值添加到数据库即可。但是现在的项目里有很多的用户,密码也是后台md5加密了的。这样就不能单纯在前台用md5加密,可能是本人能力有限,使用的前台md5加密与后台的md5加密的值不一致,用户登陆在密码比对的时候就会失败登陆不了。只能使用非对称加密,前台加密后台解密后使用md5加密再作对比,这种做法才能使用改动最少。网上查资料后就使用了rsa非对称加密。

前端代码

 <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>

 <script src="http://passport.cnblogs.com/scripts/jsencrypt.min.js"></script>

<script>

   var encrypt = new jsencrypt();

    encrypt.setpublickey("公钥可以用工具生成");
    var userpwd = encrypt.encrypt(输入的密码);     //加密后的密码

</script>

后端代码

var publickey = "公钥可以用工具生成"

var privatekey = "私钥可以用工具生成“

rsacrypto rsacrypto = new rsacrypto(privatekey, publickey);
userpwd = rsacrypto.decrypt(userpwd);    //解密后的密码,就是输入密码的值

 

/// <summary>
/// rsa非对称加密
/// </summary>
public class rsacrypto
{
private rsacryptoserviceprovider _privatekeyrsaprovider;
private rsacryptoserviceprovider _publickeyrsaprovider;

public rsacrypto(string privatekey, string publickey = null)
{
if (!string.isnullorempty(privatekey))
{
_privatekeyrsaprovider = creatersaproviderfromprivatekey(privatekey);
}

if (!string.isnullorempty(publickey))
{
_publickeyrsaprovider = creatersaproviderfrompublickey(publickey);
}
}

public string decrypt(string ciphertext)
{
if (_privatekeyrsaprovider == null)
{
throw new exception("_privatekeyrsaprovider is null");
}
return encoding.utf8.getstring(_privatekeyrsaprovider.decrypt(system.convert.frombase64string(ciphertext), false));
}

public string encrypt(string text)
{
if (_publickeyrsaprovider == null)
{
throw new exception("_publickeyrsaprovider is null");
}
return convert.tobase64string(_publickeyrsaprovider.encrypt(encoding.utf8.getbytes(text), false));
}

private rsacryptoserviceprovider creatersaproviderfromprivatekey(string privatekey)
{
var privatekeybits = system.convert.frombase64string(privatekey);

var rsa = new rsacryptoserviceprovider();
var rsaparams = new rsaparameters();

using (binaryreader binr = new binaryreader(new memorystream(privatekeybits)))
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.readuint16();
if (twobytes == 0x8130)
binr.readbyte();
else if (twobytes == 0x8230)
binr.readint16();
else
throw new exception("unexpected value read binr.readuint16()");

twobytes = binr.readuint16();
if (twobytes != 0x0102)
throw new exception("unexpected version");

bt = binr.readbyte();
if (bt != 0x00)
throw new exception("unexpected value read binr.readbyte()");

rsaparams.modulus = binr.readbytes(getintegersize(binr));
rsaparams.exponent = binr.readbytes(getintegersize(binr));
rsaparams.d = binr.readbytes(getintegersize(binr));
rsaparams.p = binr.readbytes(getintegersize(binr));
rsaparams.q = binr.readbytes(getintegersize(binr));
rsaparams.dp = binr.readbytes(getintegersize(binr));
rsaparams.dq = binr.readbytes(getintegersize(binr));
rsaparams.inverseq = binr.readbytes(getintegersize(binr));
}

rsa.importparameters(rsaparams);
return rsa;
}

private int getintegersize(binaryreader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
int count = 0;
bt = binr.readbyte();
if (bt != 0x02)
return 0;
bt = binr.readbyte();

if (bt == 0x81)
count = binr.readbyte();
else
if (bt == 0x82)
{
highbyte = binr.readbyte();
lowbyte = binr.readbyte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = bitconverter.toint32(modint, 0);
}
else
{
count = bt;
}

while (binr.readbyte() == 0x00)
{
count -= 1;
}
binr.basestream.seek(-1, seekorigin.current);
return count;
}

private rsacryptoserviceprovider creatersaproviderfrompublickey(string publickeystring)
{
// encoded oid sequence for pkcs #1 rsaencryption szoid_rsa_rsa = "1.2.840.113549.1.1.1"
byte[] seqoid = { 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00 };
byte[] x509key;
byte[] seq = new byte[15];
int x509size;

x509key = convert.frombase64string(publickeystring);
x509size = x509key.length;

// --------- set up stream to read the asn.1 encoded subjectpublickeyinfo blob ------
using (memorystream mem = new memorystream(x509key))
{
using (binaryreader binr = new binaryreader(mem)) //wrap memory stream with binaryreader for easy reading
{
byte bt = 0;
ushort twobytes = 0;

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;

seq = binr.readbytes(15); //read the sequence oid
if (!comparebytearrays(seq, seqoid)) //make sure sequence for oid is correct
return null;

twobytes = binr.readuint16();
if (twobytes == 0x8103) //data read as little endian order (actual data order for bit string is 03 81)
binr.readbyte(); //advance 1 byte
else if (twobytes == 0x8203)
binr.readint16(); //advance 2 bytes
else
return null;

bt = binr.readbyte();
if (bt != 0x00) //expect null byte next
return null;

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();
byte lowbyte = 0x00;
byte highbyte = 0x00;

if (twobytes == 0x8102) //data read as little endian order (actual data order for integer is 02 81)
lowbyte = binr.readbyte(); // read next bytes which is bytes in modulus
else if (twobytes == 0x8202)
{
highbyte = binr.readbyte(); //advance 2 bytes
lowbyte = binr.readbyte();
}
else
return null;
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
int modsize = bitconverter.toint32(modint, 0);

int firstbyte = binr.peekchar();
if (firstbyte == 0x00)
{ //if first byte (highest order) of modulus is zero, don't include it
binr.readbyte(); //skip this null byte
modsize -= 1; //reduce modulus buffer size by 1
}

byte[] modulus = binr.readbytes(modsize); //read the modulus bytes

if (binr.readbyte() != 0x02) //expect an integer for the exponent data
return null;
int expbytes = (int)binr.readbyte(); // should only need one byte for actual exponent data (for all useful values)
byte[] exponent = binr.readbytes(expbytes);

// ------- create rsacryptoserviceprovider instance and initialize with public key -----
rsacryptoserviceprovider rsa = new rsacryptoserviceprovider();
rsaparameters rsakeyinfo = new rsaparameters();
rsakeyinfo.modulus = modulus;
rsakeyinfo.exponent = exponent;
rsa.importparameters(rsakeyinfo);

return rsa;
}

}
}

private bool comparebytearrays(byte[] a, byte[] b)
{
if (a.length != b.length)
return false;
int i = 0;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
}
}

rsa密钥对生成工具---------

请选用pkcs#1来生成公钥与私钥

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网