Jump to content
Tuts 4 You

[Solved] RSA Crypting in .NET


Recommended Posts

Hi guys,

this is my first attempt to use RSA in a .NET app. I looked for a solution for hours but I did not understand how to use

RSACryptoServiceProvider to import my chosen keys. :verymad:

// Imposta parametri
RSAParameters rsaParams = new RSAParameters();
rsaParams.Modulus = myData.getN;
rsaParams.Exponent = myData.getExponent; // Istanzia il crypter
CspParameters providerParam = new CspParameters(1, "Microsoft Enhanced Cryptographic Provider");
RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(48, providerParam); myRSA.ImportParameters(rsaParams); // ALWAYS BAD-DATA !!!

My Information:

- Modulus : new byte[] { 0x52, 0x42, 0x8E, 0x94, 0x44, 0x39 }

- Exponent: new byte[] { 0x04, 0x45, 0x7A, 0x97, 0x32, 0x0B }

ON the Internet I read almost all I found about but nothing helped me :dunno:

1) I tried to execute an Array.Reverse() ( as someone suggested ) ... but no luck !

2) I tried to pass to ctor key-len ( above I choose 48 bits )

3) I tried also to build the XmlString (base64) ... but fromXmlString gives me "Bad Data" too :verymad:

                StringBuilder sb = new StringBuilder();

4) I looked on MSDN:


URL: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx


The RSACryptoServiceProvider supports key lengths from 384 bits to 16384 bits in increments of 8 bits if you have the Microsoft Enhanced Cryptographic Provider installed. It supports key lengths from 384 bits to 512 bits in increments of 8 bits if you have the Microsoft Base Cryptographic Provider installed.


so I used an Array.Copy() into a 48-byte array either for N or for both N and e.

// parametri: N, e
// Occorre estendere le chiavi ad almeno (384 bit = 384/8 = 48 byte) ?
this.e = new byte[48];
this.N = new byte[48];
Array.Copy(N, 0, this.N, this.N.Length - N.Length, N.Length); this.e = new byte[48];
Array.Copy(e, 0, this.e, this.e.Length - e.Length, e.Length);

(Even here I tried Array.Reverse) but even so that damn "Bad Data" prompts up :verymad: Is it possible that that keySize identifies *real* modulus size (and not just the array one) ?

Microsoft information about this issue is practically inexistent.

Where am I wrong ?

Please, help me :help



Edited by tonyweb
Link to comment

I don't know If I understand you but here is a simple RSA code I used in the past.

Private Function CheckLicense() As Boolean If Text_Name.Text.Trim.Length = 0 OrElse Text_Key.Text.Trim.Length = 0 Then Return False End If
Try '[1] Read Signature and Key Dim RSA As New Security.Cryptography.RSACryptoServiceProvider
RSA.FromXmlString("<RSAKeyValue><Modulus>4SaAqGqvtWlh647/Yr/TI8NdEAw0wqE+E2XvrKZPqV5RRXdS0gOJn3YbN7HwlBU0SYwbYLfOL+tOwH/1JZJccS/L9UrMb4dM7NtYZ8H9W7W9BsILMMNwg3VMr3CA4l2XmQihYGJPTghkA/jn7+mSUxFBtN7jgimlAIKr5KNBiTk=</Modulus><Exponent>AQAB</Exponent><P>/CZa3kfBcwbxrbcXp2aFimZA/t+En7o26XdkMw68X636KqiQl73wVqSwZmkUd/XNmMjo2NQi0BqrR4A2TxT8Hw==</P><Q>5JabmEXLj3FhRe/unERLx72btX2a+UCbSgtZmQUFRbqfvL0HMQxrA0T/kZKKrV/g8xjqAA0Y4Godr+auVMzPpw==</Q><DP>CJRthx698GIOGzEdtu6AIMN19rY4Vw8JC0yqtioMMt1Co+z0bpVh9jDIZ9OvhJ1yj35Wnfop7RbbOSWds3ctVQ==</DP><DQ>sY2OdBuafuPJc8JnK7gcyJwtiy5yJrMZK81fHe15Z+0K0FZfdKi5ooOAo7kYwIxdtVWfxMRnk0C5qHzc4n9ntw==</DQ><InverseQ>FBZjEL/dqXHb8IYGH1ghzsEjoTqFCE/dW+JeEmCIJDqAQH6fxyVzpciRpksg9BMHEOASoVzVIWOw4l/e6QRJxg==</InverseQ><D>xXCdPJOqGD0cuJRC0tcDEgadUphRoCgEJsCqiE2j6BOk03ULNM4TXroUrieloEZj/ucVjyBAePcxrU/Iq7UIyDedH80ChGQViA2NGMDNIzyqMR2j+ruwT8wwim41oI7D78m2AnjWBHcLISVaFo2v3Fp1teCtGB3aKbtAu/m3fI0=</D></RSAKeyValue>") 'We will generate Key from Name_Text.text
'We will generate RSA Signature from Key_Text.text 'Generate RSA Key from name
Dim Hasher As New Security.Cryptography.SHA1Managed
Dim Key() As Byte = Hasher.ComputeHash(System.Text.Encoding.Unicode.GetBytes(Text_Name.Text.Trim))
Dim RSASig() As Byte
RSASig = Convert.FromBase64String(Text_Key.Text.Trim)
'Verify Key
Dim Ret As Boolean = RSA.VerifyData(Key, "SHA1", RSASig) Select Case Ret Case True Return True Case False Return False End Select Catch ex As Exception Return False End Try
End Function

the default Key size is 1028 which is big enough imho.

you can create a new RSA object then save it using the "ToXMLString" Method which returns that XML string you see here.

good luck

Link to comment

Thank you Kurapica for your code ... but that's not what I needed ;)

I needed a way to use RSA with a modulus lesser than 384 bit ( the minimum allowed by Microsoft implementation, it seems ).

I followed high6 suggestion and used "Bouncy Castle .NET" ( Open SSL.NET requires 3 accompany files ... bouncy castle has only one dll ).

RsaEngine myRSA = new RsaEngine();
BigInteger exponent = new BigInteger( getExponent() );
BigInteger modulus = new BigInteger( getN() ); Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters rsaParams = new Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters(false, modulus, exponent);
myRSA.Init(true, rsaParams);
byte[] encodedBytes = myRSA.ProcessBlock(to_encode, 0, to_encode.Length);

Thank you guys. Mission completed ;)



Edited by tonyweb
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...