Sto tentando di crittografare e decrittografare i dati utilizzando RSA in C#. Ho il seguente test dell'unità MSTest:CryptographicException si verifica in modo intermittente durante la crittografia/decrittografia con RSA
const string rawPassword = "mypass";
// Encrypt
string publicKey, privateKey;
string encryptedPassword = RSAUtils.Encrypt(rawPassword, out publicKey, out privateKey);
Assert.AreNotEqual(rawPassword, encryptedPassword,
"Raw password and encrypted password should not be equal");
// Decrypt
string decryptedPassword = RSAUtils.Decrypt(encryptedPassword, privateKey);
Assert.AreEqual(rawPassword, decryptedPassword,
"Did not get expected decrypted password");
Non riesce durante la decodifica, ma solo qualche volta. Sembra che ogni volta che imposto i breakpoint e passo attraverso il test, passi. Questo mi ha fatto pensare che forse qualcosa non si stava concludendo in tempo per la decifrazione e che ho rallentato l'esecuzione mentre il debug ha dato il tempo necessario per completarlo. Quando non riesce, la linea sembra di non riuscire a è decryptedBytes = rsa.Decrypt(bytesToDecrypt, false);
nel seguente metodo:
public static string Decrypt(string textToDecrypt, string privateKeyXml)
{
if (string.IsNullOrEmpty(textToDecrypt))
{
throw new ArgumentException(
"Cannot decrypt null or blank string"
);
}
if (string.IsNullOrEmpty(privateKeyXml))
{
throw new ArgumentException("Invalid private key XML given");
}
byte[] bytesToDecrypt = ByteConverter.GetBytes(textToDecrypt);
byte[] decryptedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKeyXml);
decryptedBytes = rsa.Decrypt(bytesToDecrypt, false); // fail here
}
return ByteConverter.GetString(decryptedBytes);
}
non riesce con questa eccezione:
System.Security.Cryptography.CryptographicException: Bad dati
Il mio metodo Encrypt
è il seguente:
public static string Encrypt(string textToEncrypt, out string publicKey,
out string privateKey)
{
byte[] bytesToEncrypt = ByteConverter.GetBytes(textToEncrypt);
byte[] encryptedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
encryptedBytes = rsa.Encrypt(bytesToEncrypt, false);
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
return ByteConverter.GetString(encryptedBytes);
}
Il ByteConverter
utilizzato in tutto è proprio la seguente:
public static readonly UnicodeEncoding ByteConverter = new UnicodeEncoding();
Ho visto alcune domande su StackOverflow su crittografia RSA e la decrittografia con .NET. This one era dovuto alla crittografia con la chiave privata e al tentativo di decifrare con la chiave pubblica, ma non credo che lo stia facendo. This question ha la stessa eccezione di me, ma la risposta selezionata era usare OpenSSL.NET, che preferirei non fare.
Cosa sto sbagliando?
Hm, provando questo mi dà un'eccezione diversa: "System.FormatException: lunghezza non valida per un array di char Base-64". Questo si è verificato sulla prima riga di 'Encrypt':' byte [] bytesToEncrypt = Convert.FromBase64String (textToEncrypt); '. –
@Sarah - Ok, ho aggiornato il tuo esempio. L'ho provato e sembra liek funziona. – SwDevMan81
Funziona! Grazie. Non avrei mai pensato di usare quel mix di 'Convert' /' UnicodeEncoding'. –