admin 管理员组文章数量: 1086019
2024年4月25日发(作者:交流异步发电机)
///
/// RSA Decrypt
///
/// Source string
/// Private Key
///
public static string DecryptString(String sSource, string sPrivateKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
lString(sPrivateKey);
byte[] byteEn = t(es("a"), false);
string[] sBytes = (',');
for (int j = 0; j < ; j++)
{
if (sBytes[j] != "")
{
byteEn[j] = (sBytes[j]);
}
}
byte[] plaintbytes = t(byteEn, false);
return ing(plaintbytes);
}
return ng();
}
--------------------------------------------------------------------
RSA加密解密源码:
using System;
using c;
using ;
using ;
using graphy;
namespace MyRSA
namespace MyRSA
{
publicclass MyRSA
{
privatestaticstring publicKey =
"
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
"w9YRXiac=
privatestaticstring privateKey =
"
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
"w9YRXiac=
"
/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+
"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+
"VgzvvAxXD7ESw==
6kqclrEunX/fmOle"+"+"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+
"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==
"
"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+
"zc3d/oW40YqJ2Q==
"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+
"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==
"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+
"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+
"aPjSfWdA==
"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+
"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+
"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+
"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=";
staticpublicstring Decrypt(string base64code)
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter =new UnicodeEncoding();
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
lString(privateKey);
byte[] encryptedData;
byte[] decryptedData;
encryptedData = se64String(base64code);
//Pass the data to DECRYPT, the private key information
//(using Parameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(
encryptedData, Parameters(true), false);
//Display the decrypted plaintext to the console.
return ing(decryptedData);
}
catch (Exception exc)
{
//eption(exc);
ine(e);
return"";
}
}
staticpublicstring Encrypt(string toEncryptString)
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter =new UnicodeEncoding();
//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt =
es(toEncryptString);
byte[] encryptedData;
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
lString(privateKey);
//Pass the data to ENCRYPT, the public key information
//(using Parameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(
dataToEncrypt, Parameters(false), false);
string base64code = 64String(encryptedData);
return base64code;
}
catch (Exception exc)
{
//Catch this exception in case the encryption did
//not succeed.
//eption(exc);
ine(e);
return"";
}
}
staticprivatebyte[] RSAEncrypt(
byte[] DataToEncrypt,
RSAParameters RSAKeyInfo,
bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
//Import the RSA Key information. This only needs
//toinclude the public key information.
Parameters(RSAKeyInfo);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return t(DataToEncrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
//eption(e);
ine(e);
returnnull;
}
}
staticprivatebyte[] RSADecrypt(
byte[] DataToDecrypt,
RSAParameters RSAKeyInfo,
bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
//Import the RSA Key information. This needs
//to include the private key information.
Parameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return t(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
//eption(e);
ine(e);
returnnull;
}
}
}
}
测试代码:
static void Main(string[] args)
{
string encodeString = t("1234567");
ine(encodeString);
string decode = t(encodeString);
ine(decode);
ne();
}
版权声明:本文标题:C#实现RSA加密和解密详解 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1714036966a662615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论