1

I was looking for RSACryptoServiceProvider helper and found two different implementations

1) http://www.cnblogs.com/WYB/archive/2008/06/19/1225704.html

2) https://github.com/robvolk/Helpers.Net/blob/master/Src/Helpers.Net/EncryptionExtensions.cs

both of them working

var encryptedBytes = myBytes.RSAEncrypt(publicKey);
System.Text.Encoding.Unicode.GetString(encryptedBytes); 
returns strings like "蹩巷Ӂය馧㾵봽놶徤蕺蓷課Ϝ堲泍썳⁙䃑ക늏...."

myString.EncryptStringUsingXMLFile(publicKey) 
returns strings like "AnvFFT6YpoiAyIFwl+tueZq56Zcb0B7WhBEvz5uWl...."

May be some one can explain why first one producing Chinese strings and how to change that?

What approach is better?

Sreginogemoh
  • 1,309
  • 3
  • 10
  • 12
  • 1
    This question appears to be off-topic because it is about Cryptography. Better answers can be there. – Praveen Sep 18 '13 at 03:40

1 Answers1

0

To answer your first question. While it may look like it is producing Chinese characters what is actually happening is it is turning a byte array into unicode. In c# typically when you want to store a byte array you convert it to base64 which is what your second example appears to return.

Your first example would become this:

var encryptedBytes = myBytes.RSAEncrypt(publicKey);
Convert.ToBase64String(encryptedBytes) // this line changed
returns strings like "AnvFFT6YKpoiAy...."

As for what is recommended, the most common is to use base64. The reasons people use base64 over unicode or UTF-8 for binary data can be found in these answers:

https://stackoverflow.com/a/201491/701062

MSDN - Convert.ToBase64String(byte[])

http://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.100).aspx

MSDN - Convert.FromBase64String(string) - Useful if you need to convert back into a byte array

http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.100).aspx

Community
  • 1
  • 1
Gary.S
  • 7,076
  • 1
  • 26
  • 36