Microsoft is useless and I was just skinning some code out to do RSA encryption using this bit of code
that should work on older versions of the DOT.NET framework that don't come with BigINT
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(384);
_privateKey = rsa.ToXmlString(true);
_publicKey = rsa.ToXmlString(false);
string text = "Hello world";
string encPublic = EncodePublic(_publicKey, text);
string decPrivate = DecodePrivate(_privateKey, encPublic);
string encSig = EncodePublic(_privateKey, text);
string decSigPrivate = DecodePrivate(_privateKey, encSig);
string decSigPublic = DecodePrivate(_publicKey, encSig); //GOES BANG HERE
public string EncodePublic(string key,string str)
{
RSACryptoServiceProvider RSA2= new RSACryptoServiceProvider();
RSA2.FromXmlString(key);
byte[] encryptedData = RSA2.Encrypt(Encoding.UTF8.GetBytes(str), false);
string base64Encrypted = Convert.ToBase64String(encryptedData);
return base64Encrypted;
}
public string DecodePrivate(string key, string EncryptedStr)
{
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
RSA2.FromXmlString(key);
var resultBytes = Convert.FromBase64String(EncryptedStr);
var decryptedBytes = RSA2.Decrypt(resultBytes, false);
var decryptedData = Encoding.UTF8.GetString(decryptedBytes);
return decryptedData;
}
I want to use the old framework because it will work on more clients but after hours of
looking around i come across this
RSA Private Key EncryptionUnfortunately, the RSACryptoServiceProvider class does not provide you this option, so I wrote my own implementation of the RSA algorithm using the basics of the RSACryptoServiceProvider in conjunction with Chew Keong TAN's class: BigInteger (
http://www.codeproject.com/KB/cs/biginteger.aspx). At a low level, the RSA algorithm is about implementing mathematical equations on huge (huge) integers, so the BigInteger class is really essential. I couldn't have done it myself.
So thanks Microsoft for not throwing an error like "Private key encryption is not possible" instead of "Key not found"
from the bloated framework.
I thought that I wanted private key encryption for the signature when signing transactions and Bitcoin uses this technique
but then I started to read more about it and this guy says
https://rdist.root.org/2007/05/03/rsa-public-keys-are-not-private-implementation/Finally, I’d like to reiterate what I’ve said before. Public key cryptosystems and RSA in particular are extremely fragile. Do not use them differently than they were designed. Better yet, seek expert review or design assistance when working with any design involving crypto.
What do you guys think ?