欢迎来到天天文库
浏览记录
ID:41266746
大小:21.72 KB
页数:5页
时间:2019-08-20
《不限制长度Rsa加密算法超出117解决方案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Rsa加密算法加密长度限制是117字节,下面展示的是不限制长度rsa改造方法包含的有加密解密加密的内容与原类型比较是否所有值都相等以及一个单元测试publicclassRsaHelper{publicstaticstringPubKey=”自己用下面生成的”;//公钥publicstaticstringPriKey=”自己用下面生成的”;//私钥publicstructRsaKey{publicstringPublicKey{get;set;}publicstringPrivateKey{get;set;}}//////创建公钥私钥//////2、urns>staticpublicRsaKeyGEN_Key(){varrsa=newRSACryptoServiceProvider();returnnewRsaKey(){//写死加快执行速度//PublicKey=rsa.ToXmlString(false),//生成公钥自己生成填到上面//PrivateKey=rsa.ToXmlString(true)//生成私钥自己生成填到上面};}//////解密//////要解密的字符串///3、"privateKey">私钥///publicstaticstringRsaDecrypt(stringencryptedInput,stringprivateKey){if(string.IsNullOrEmpty(encryptedInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(privateKey)){thrownewArgumentException("InvalidPrivateKey");}using(varrsaProvider=newRSACryptoS4、erviceProvider()){varinputBytes=Convert.FromBase64String(encryptedInput);rsaProvider.FromXmlString(privateKey);intbufferSize=rsaProvider.KeySize/8;varbuffer=newbyte[bufferSize];using(MemoryStreaminputStream=newMemoryStream(inputBytes),outputStream=newMemoryStream()){while(true){intreadSize=inputSt5、ream.Read(buffer,0,bufferSize);if(readSize<=0){break;}vartemp=newbyte[readSize];Array.Copy(buffer,0,temp,0,readSize);varrawBytes=rsaProvider.Decrypt(temp,false);outputStream.Write(rawBytes,0,rawBytes.Length);}returnEncoding.UTF8.GetString(outputStream.ToArray());}}}//////加密/////6、/要加密的字符串///公钥///publicstaticstringRsaEncrypt(stringrawInput,stringpublicKey){if(string.IsNullOrEmpty(rawInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(publicKey)){thrownewArgumentException("InvalidPublicKey")7、;}using(varrsaProvider=newRSACryptoServiceProvider()){varinputBytes=Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流rsaProvider.FromXmlString(publicKey);//载入公钥intbufferSize=(rsaProvider.KeySize/8)-11;//单块最大长度varb
2、urns>staticpublicRsaKeyGEN_Key(){varrsa=newRSACryptoServiceProvider();returnnewRsaKey(){//写死加快执行速度//PublicKey=rsa.ToXmlString(false),//生成公钥自己生成填到上面//PrivateKey=rsa.ToXmlString(true)//生成私钥自己生成填到上面};}//////解密//////要解密的字符串///3、"privateKey">私钥///publicstaticstringRsaDecrypt(stringencryptedInput,stringprivateKey){if(string.IsNullOrEmpty(encryptedInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(privateKey)){thrownewArgumentException("InvalidPrivateKey");}using(varrsaProvider=newRSACryptoS4、erviceProvider()){varinputBytes=Convert.FromBase64String(encryptedInput);rsaProvider.FromXmlString(privateKey);intbufferSize=rsaProvider.KeySize/8;varbuffer=newbyte[bufferSize];using(MemoryStreaminputStream=newMemoryStream(inputBytes),outputStream=newMemoryStream()){while(true){intreadSize=inputSt5、ream.Read(buffer,0,bufferSize);if(readSize<=0){break;}vartemp=newbyte[readSize];Array.Copy(buffer,0,temp,0,readSize);varrawBytes=rsaProvider.Decrypt(temp,false);outputStream.Write(rawBytes,0,rawBytes.Length);}returnEncoding.UTF8.GetString(outputStream.ToArray());}}}//////加密/////6、/要加密的字符串///公钥///publicstaticstringRsaEncrypt(stringrawInput,stringpublicKey){if(string.IsNullOrEmpty(rawInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(publicKey)){thrownewArgumentException("InvalidPublicKey")7、;}using(varrsaProvider=newRSACryptoServiceProvider()){varinputBytes=Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流rsaProvider.FromXmlString(publicKey);//载入公钥intbufferSize=(rsaProvider.KeySize/8)-11;//单块最大长度varb
3、"privateKey">私钥///publicstaticstringRsaDecrypt(stringencryptedInput,stringprivateKey){if(string.IsNullOrEmpty(encryptedInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(privateKey)){thrownewArgumentException("InvalidPrivateKey");}using(varrsaProvider=newRSACryptoS
4、erviceProvider()){varinputBytes=Convert.FromBase64String(encryptedInput);rsaProvider.FromXmlString(privateKey);intbufferSize=rsaProvider.KeySize/8;varbuffer=newbyte[bufferSize];using(MemoryStreaminputStream=newMemoryStream(inputBytes),outputStream=newMemoryStream()){while(true){intreadSize=inputSt
5、ream.Read(buffer,0,bufferSize);if(readSize<=0){break;}vartemp=newbyte[readSize];Array.Copy(buffer,0,temp,0,readSize);varrawBytes=rsaProvider.Decrypt(temp,false);outputStream.Write(rawBytes,0,rawBytes.Length);}returnEncoding.UTF8.GetString(outputStream.ToArray());}}}//////加密/////
6、/要加密的字符串///公钥///publicstaticstringRsaEncrypt(stringrawInput,stringpublicKey){if(string.IsNullOrEmpty(rawInput)){returnstring.Empty;}if(string.IsNullOrWhiteSpace(publicKey)){thrownewArgumentException("InvalidPublicKey")
7、;}using(varrsaProvider=newRSACryptoServiceProvider()){varinputBytes=Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流rsaProvider.FromXmlString(publicKey);//载入公钥intbufferSize=(rsaProvider.KeySize/8)-11;//单块最大长度varb
此文档下载收益归作者所有