RSA加密解密源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace MyRSA
{
public class MyRSA
{
private static string publicKey =
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH" +
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L" +
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx" +
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE" +
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP" +
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private static string privateKey =
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH" +
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L" +
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx" +
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE" +
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP" +
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent>" +
"<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d" +
"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd" +
"VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle" +
"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG" +
"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>" +
"<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU" +
"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te" +
"zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY" +
"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA" +
"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>" +
"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn" +
"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4" +
"aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D" +
"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ" +
"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40" +
"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry" +
"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";
static public string 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();
RSA.FromXmlString(privateKey);
byte[] encryptedData;
byte[] decryptedData;
encryptedData = Convert.FromBase64String(base64code);
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(
encryptedData, RSA.ExportParameters(true), false);
//Display the decrypted plaintext to the console.
return ByteConverter.GetString(decryptedData);
}
catch (Exception exc)
{
//Exceptions.LogException(exc);
Console.WriteLine(exc.Message);
return "";
}
}
static public string 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 =
ByteConverter.GetBytes(toEncryptString);
byte[] encryptedData;
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(privateKey);
//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(
dataToEncrypt, RSA.ExportParameters(false), false);
string base64code = Convert.ToBase64String(encryptedData);
return base64code;
}
catch (Exception exc)
{
//Catch this exception in case the encryption did
//not succeed.
//Exceptions.LogException(exc);
Console.WriteLine(exc.Message);
return "";
}
}
static private byte[] 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.
RSA.ImportParameters(RSAKeyInfo);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
//Exceptions.LogException(e);
Console.WriteLine(e.Message);
return null;
}
}
static private byte[] 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.
RSA.ImportParameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
//Exceptions.LogException(e);
Console.WriteLine(e.Message);
return null;
}
}
}
}
测试代码:
static void Main(string[] args)
{
string encodeString = MyRSA.Encrypt("1234567");
Console.WriteLine(encodeString);
string decode = MyRSA.Decrypt(encodeString);
Console.WriteLine(decode);
Console.ReadLine();
}
{
string encodeString = MyRSA.Encrypt("1234567");
Console.WriteLine(encodeString);
string decode = MyRSA.Decrypt(encodeString);
Console.WriteLine(decode);
Console.ReadLine();
}
相关推荐
RSA加密解密C#实现调用实例 public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { try { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; System.Security....
C#做的RSA加密解密, 带winform界面, 测试通过, 代码完整。
主要介绍了C# RSA分段加解密实现方法,结合具体实例形式分析了C# RSA加密解密的原理与具体实现技巧,需要的朋友可以参考下
C#RSA加密与JAVA解密,实现相互通信,对字符进行加密,java解密,对密文字节进行编码
利用System.Numerics.BigInteger实现,私钥加密,公钥解密。兼容PKCS1填充方式。有效文件只有MyRSA.cs一个,其它文件是因为带了一个演示窗口。
1.随机搜索大素数,随机生成公钥和私钥。2.用公钥对任意长度的明文(字符)加密。3.用私钥对密文解密。4.界面简洁、友好便于操作。
* RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥解密,公钥加密。 * RSA数字签名-俗称加签验签:私钥加签,公钥验签。 * RSA加密解密:私钥...
通常C#使用xml格式的密钥文件,不能使用Linux下的公钥和私钥pem文件。...本源码直接使用linux下的PEM文件来做RSA加密和解密,可避免xml格式文件的移植性问题。完整的源码,Vs2008工程,以编译和测试通过。
亲测可用,可参考:https://blog.csdn.net/qq_37835111/article/details/87358779
C#实现RSA加密,JAVA进行解密,实现JAVA与C#的相互通信
unity工具类RSA加密和解密
RSA加密算法实现以及C#与java互通加解密,解决RSA算法在java与C#相互通用
C# RSA私钥加密,公钥解密源码
易语言调用JSEncrypt实现RSA加密解密。Tags:JSEncryptRSA加密解密。
结合具体实例形式分析了js使用jsencrypt.js插件,前端字符数据处理传输及C#后台数据转换与RSA加密相关操作技巧,并附带jsencrypt.js
RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学
C#Android互通RSA加密解密
/// RSA加密解密及RSA签名和验证 /// </summary> public class RSACryption { public RSACryption() { } #region RSA 加密解密 #region RSA 的密钥产生 /// /// RSA 的密钥...
RSA非对称 C#解密、js加密实现登陆密文,传输RSA非对称 C#解密、js加密实现登陆密文传输,RSA非对称 C#解密、js加密实现登陆密文传输
C#RSA加密解密汇总