I have a method that currently returns a string converted from a byte array:
public static readonly UnicodeEncoding ByteConverter = new UnicodeEncoding();
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 = Convert.FromBase64String(textToDecrypt);
byte[] decryptedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKeyXml);
decryptedBytes = rsa.Decrypt(bytesToDecrypt, FOAEP);
}
return ByteConverter.GetString(decryptedBytes);
}
I'm trying to update this method to instead return a SecureString
, but I'm having trouble converting the return value of RSACryptoServiceProvider.Decrypt
from byte[]
to SecureString
. I tried the following:
var secStr = new SecureString();
foreach (byte b in decryptedBytes)
{
char[] chars = ByteConverter.GetChars(new[] { b });
if (chars.Length != 1)
{
throw new Exception(
"Could not convert a single byte into a single char"
);
}
secStr.AppendChar(chars[0]);
}
return secStr;
However, using this SecureString equality tester, the resulting SecureString
was not equal to the SecureString
constructed from the original, unencrypted text. My Encrypt and Decrypt methods worked before, when I was just using string
everywhere, and I've also tested the SecureString
equality code, so I'm pretty sure the problem here is how I'm trying to convert byte[]
into SecureString
. Is there another route I should take for using RSA encryption that would allow me to get back a SecureString
when I decrypt?
Edit: I didn't want to convert the byte array to a regular string and then stuff that string into a SecureString
, because that seems to defeat the point of using a SecureString
in the first place. However, is it also bad that Decrypt
returns byte[]
and I'm then trying to stuff that byte array into a SecureString
? It's my guess that if Decrypt
returns a byte[]
, then that's a safe way to pass around sensitive information, so converting one secure representation of the data to another secure representation seems okay.
Based on Coding Gorilla's answer, I tried the following in my Decrypt
method:
string decryptedString1 = string.Empty;
foreach (byte b in decryptedBytes)
{
decryptedString1 += (char)b;
}
string decryptedString2 = ByteConverter.GetString(decryptedBytes);
When debugging, decryptedString1
and decryptedString2
were not equal:
decryptedString1 "m\0y\0V\0e\0r\0y\0L\0o\0n\0g\0V\03\0r\0y\05\03\0c\0r\03\07\0p\04\0s\0s\0w\00\0r\0d\0!\0!\0!\0"
decryptedString2 "myVeryLongV3ry53cr37p4ssw0rd!!!"
So it looks like I can just go through the byte[]
array, do a direct cast to char
, and skip \0
characters. Like Coding Gorilla said, though, this does seem to again in part defeat the point of SecureString
, because the sensitive data is floating about in memory in little byte
-size chunks. Any suggestions for getting RSACryptoServiceProvider.Decrypt
to return a SecureString
directly?
Edit: yep, this works:
var secStr = new SecureString();
foreach (byte b in decryptedBytes)
{
var c = (char)b;
if ('\0' == c)
{
continue;
}
secStr.AppendChar(c);
}
return secStr;
Edit: correction: this works with plain old English strings. Encrypting and then attempting to decrypt the string "標準語 明治維新 english やった"
doesn't work as expected because the resulting decrypted string, using this foreach (byte b in decryptedBytes)
technique, does not match the original unencrypted string.
Edit: using the following works for both:
var secStr = new SecureString();
foreach (char c in ByteConverter.GetChars(decryptedBytes))
{
secStr.AppendChar(c);
}
return secStr;
This still leaves a byte array and a char array of the password in memory, which sucks. Maybe I should find another RSA class that returns a SecureString
. :/