Based on the Gorilla response Encoding , I tried the following in the 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, make a direct tide to char and skip the \0 characters. As was the case with encoding, Gorilla said that it seemed to again partially lose the SecureString point, because sensitive data was dropped in memory in small byte fragments. Any suggestions for getting RSACryptoServiceProvider.Decrypt for direct return of SecureString ?
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. The encryption and attempt to decrypt the string "ζ¨ζΊθͺ ζζ²»ηΆζ° english γγ£γ" does not work as expected, because the received decrypted string using this foreach (byte b in decryptedBytes) method foreach (byte b in decryptedBytes) 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 an array of bytes and an array of char passwords in memory, which sucks. Maybe I should find another RSA class that returns SecureString .: /
source share