C# Bob和Alice非对称加密和解密的实现
我正在尝试使用 为此我有C# Bob和Alice非对称加密和解密的实现,c#,encryption,cryptography,rsacryptoserviceprovider,C#,Encryption,Cryptography,Rsacryptoserviceprovider,我正在尝试使用 为此我有 控制台应用程序鲍伯(可以考虑作为发送者) 控制台应用程序爱丽丝(可考虑为接收机) 控制台应用程序Bob可以使用其公钥进行加密,然后控制台应用程序Alice可以使用其私钥对其进行解密 这就是Bob控制台应用程序 class Program { static void Main(string[] args) { RSACryptoServiceProvider rsa = new RSACryptoSe
class Program
{
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
EncryptText(publicKey, "Hello from C# Corner", "encryptedData.dat");
}
public static void EncryptText(string publicKey, string text, string fileName)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = byteConverter.GetBytes(text);
byte[] encryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
encryptedData = rsa.Encrypt(dataToEncrypt, false);
}
File.WriteAllBytes(fileName, encryptedData);
Console.WriteLine("Data has been encrypted");
}
}
class Program
{
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
Console.WriteLine("Decrypted message: {0}", DecryptData(privateKey, "encryptedData.dat"));
}
public static string DecryptData(string privateKey, string fileName)
{
byte[] dataToDecrypt = File.ReadAllBytes(fileName);
byte[] decryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
decryptedData = rsa.Decrypt(dataToDecrypt, false);
}
UnicodeEncoding byteConverter = new UnicodeEncoding();
return byteConverter.GetString(decryptedData);
}
}
这是Alice控制台应用程序
class Program
{
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
EncryptText(publicKey, "Hello from C# Corner", "encryptedData.dat");
}
public static void EncryptText(string publicKey, string text, string fileName)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = byteConverter.GetBytes(text);
byte[] encryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
encryptedData = rsa.Encrypt(dataToEncrypt, false);
}
File.WriteAllBytes(fileName, encryptedData);
Console.WriteLine("Data has been encrypted");
}
}
class Program
{
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
Console.WriteLine("Decrypted message: {0}", DecryptData(privateKey, "encryptedData.dat"));
}
public static string DecryptData(string privateKey, string fileName)
{
byte[] dataToDecrypt = File.ReadAllBytes(fileName);
byte[] decryptedData;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
decryptedData = rsa.Decrypt(dataToDecrypt, false);
}
UnicodeEncoding byteConverter = new UnicodeEncoding();
return byteConverter.GetString(decryptedData);
}
}
当它解密时,我得到的错误是
System.Security.Cryptography.CryptographyException:'参数为
不对
请您提供以下建议:)原因是您正在两个应用程序中构建公钥/私钥对 本质上,您试图用Bob的公钥加密某些内容,并试图用Alice的私钥解密这些内容。这行不通 您需要构造1对,然后使用该对中的公钥进行加密,使用同一对中的私钥进行解密 这项工作:
void Main()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
EncryptText(publicKey, "Test", @"d:\temp\test.dat");
Console.WriteLine(DecryptData(privateKey, @"d:\temp\test.dat"));
}
但如果在调用decrypt之前创建一个新对,则会出现以下错误:
WindowsCryptographicException参数不正确
如果Bob是发送者,Alice是接收者,则不能使用Bob的公钥进行加密(目前似乎是这样),但必须使用Alice的公钥。然后Alice可以用她的私钥解密。因此,在加密/解密之前,必须交换公钥。@LasseV.Karlsen,如果这些公钥在一个应用程序下无问题如何在一个应用程序中使用此加密方法,在另一个应用程序中使用解密方法,则您需要将公钥共享给进行加密的应用程序。通过写入文件正确吗?当第一次生成这样的密钥对时,是的。使公钥可用于加密应用程序。