rsa public key for c# .net

Hi. I have a problem. I try to traslate a Public Key Rsa to a valid format to use in ImportCspBlob. but this code don't work.

I find solucion on internet but don't work. http://www.mcse.ms/archive113-2005-6-1681898.html

I don't know format for public key rsa for .net and i don't find documentacion. I use framework 2.0 visual studio 2005

I don't know what i need to do... help me please!

using System;

using System.Collections.Generic;

using System.Security.Cryptography;

using System.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

string RsaResult = null;

//questa e la loro chiave rsa pubblica

string PublicKeyRsa = "30819D300D06092A864886F70D010101050003818B0030818702818100C6F80A0CA603136DA265397481E6B00A0BFB0A2D928A139D0E179960E382B7DB1F9A39DB7CA65E113C7BE37DCCD7EA731B4C1722A8C0BB3E38675E15CD4B1344732C09770BDFE2C8028B2BE017168FB5E2AF4F67209A8F663EC4D2F59C70F60F75B683063F55C9499ABABAE5A56902B74CEAFD64BB0D416E5B00A448D2DBF8FB020111";

byte[] RsaKeyByteMatrix = ConvertStringHexadecimalToByteArray(PublicKeyRsa);

//creating ecrypting instrument

RSACryptoServiceProvider EncrypterRsa = new RSACryptoServiceProvider();

//error. bad provider

EncrypterRsa.ImportCspBlob(RsaKeyByteMatrix);

}

private static byte[] ConvertStringHexadecimalToByteArray(string StringHexadecimal)

{

byte[] RsaByteMatrix = new byte[StringHexadecimal.Length / 2];

char[] RsaCharMatrix = StringHexadecimal.ToCharArray();

int StringLength = StringHexadecimal.Length / 2;

string temporany;

//convert string rsa hexadecimal to byte array decimal

for (int i = 0; i < StringLength; i++)

{

RsaByteMatrixIdea = Convert.ToByte(StringHexadecimal.Substring(0, 2), 16);

StringHexadecimal = StringHexadecimal.Remove(0, 2);

}

return RsaByteMatrix;

}

}

}




Answer this question

rsa public key for c# .net

  • ahmedWebDev

    I just notices your key, I think it was not out put of Base64String() so it will not probably work with the method I told you, you have done your own encoding which is not required in situation like this. I told you the simplest way posisble. Why dont you adopt this

    If you need any further help please post your question here!

    Best Regards,



  • ron nash

    If you dont know what encoding scheme you used to make this key from bytes to string then you cant get it done.

    I cant understand where is "StringHexadecimal" and where are its functions you called in your code. You need to know what Encoding you need to convert this string otherwise there is no way to get a successful solution.

    I will recomend you a few things:

    1. If its not a commercial application as you told that you are a student then just give up this key and generate new one.
    2. Use the conversion scheme I suggested you, If you have the solution then why you are trimiing, generating substring and all this headache.
    3. If you dont want to adopt above 2 points then share more information that who you generated the key, what was the correct encoding to convert it from bytes to string, only then your problem can be solved.

    Wish you all the best ;-)



  • darynk21

    RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider();

    byte[] rsaKeyPair = Convert.FromBase64String("30819D300D06092A864886F70D010101050003818B0030818702818100C6F80A0CA603136DA265397481E6B00A0BFB0A2D928A139D0E179960E382B7DB1F9A39DB7CA65E113C7BE37DCCD7EA731B4C1722A8C0BB3E38675E15CD4B1344732C09770BDFE2C8028B2BE017168FB5E2AF4F67209A8F663EC4D2F59C70F60F75B683063F55C9499ABABAE5A56902B74CEAFD64BB0D416E5B00A448D2DBF8FB020111");

    rsaCryptoServiceProvider.ImportCspBlob(rsaKeyPair); //excepion, bad provider!

    Yes but it is exactly my problem! the method ImportCspBlob answer me //excepion, bad provider!

    I try to change mi Rsa public key in a byte array of decimal (first post) but I have the same problem!

    I hope that someone have an idea... :-)

    C# university student very sad



  • JimDuncan

    byte[] rsaKeyPair = Convert.FromBase64String("30819D300D06092A864886F70D010101050003818B0030818702818100C6F80A0CA603136DA265397481E6B00A0BFB0A2D928A139D0E179960E382B7DB1F9A39DB7CA65E113C7BE37DCCD7EA731B4C1722A8C0BB3E38675E15CD4B1344732C09770BDFE2C8028B2BE017168FB5E2AF4F67209A8F663EC4D2F59C70F60F75B683063F55C9499ABABAE5A56902B74CEAFD64BB0D416E5B00A448D2DBF8FB020111");

    now import it in RsaCryptoServiceProvider through Import.... method.

    Cheers ;-)



  • Trisk2

    My key is in hexadecimal format. i write this method

    private static byte[] ConvertStringHexadecimalToByteArray(string StringHexadecimal)

    {

    byte[] RsaByteMatrix = new byte[StringHexadecimal.Length / 2];

    char[] RsaCharMatrix = StringHexadecimal.ToCharArray();

    int StringLength = StringHexadecimal.Length / 2;

    string temporany;

    //convert string rsa hexadecimal to byte array decimal

    for (int i = 0; i < StringLength; i++)

    {

    RsaByteMatrix = Convert.ToByte(StringHexadecimal.Substring(0, 2), 16);

    StringHexadecimal = StringHexadecimal.Remove(0, 2);

    }

    return RsaByteMatrix;

    }

    to convert in a byte[] (first post). but this byte[] isn't ok for Blob in C#. I think that .net write something in the blob value to know the rsa key. but i am not sure.

    I can't change the key becausa i need to write a simple method that cript input with this specific rsa public key.



  • Rab Lucas

    I have used it in my project so try this:

    /// <summary>

    /// Constructor for PublicKeyEncryption class

    /// </summary>

    /// <param name="keyLength">Length of Key (To assist running on different Platforms, because not all Platoforms Support 1024 bit Key)</param>

    public PublicKeyCipher(int keyLength)

    {

    if (keyLength != 384 && keyLength != 512 && keyLength != 1024 && keyLength != 2048) // Check if Key length is Valid

    {

    throw new ArgumentOutOfRangeException("keyLength", "Key Lenght is not Valid, Only 384, 512, 1024 and 2048 bit Keys are currently suported");

    }

    this.rsaCryptoServiceProvider = new RSACryptoServiceProvider();

    switch (keyLength)

    {

    case 2048:

    this.rsaKeyPair = Convert.FromBase64String("BwIAAACkAABSU0EyAAgAAAEAAQDNIPLRggKWlQUI77l5A1Sn/1qGEeCPLJSoomDMep7ZziK2Y6js172R8BekXYRSfNzHEd06aWxFaW6Jd9Ie90I1GXbNpAvt0LQK87aeTNGn85OgL/cEzEc4KFAagovP8auWKEz5F8joVgAq2Zf4MBJ4QWhihrcphUWiczAd023cIZ9GcvQ/JAqg86WeKNcKnwXGGNjF7DFeT7XnFv9jps9ouAVrHiRj/TykRYTFOR6N2lTo5UuxauZ9Qc9Qt/5Kl7lH/P1Avfks8tC2vN/+v1Tc0jCIcLd8eMA6xIlYcoXADH3JOHySkIJaMDdI3kr/5JbGQMpd7HwHY0dUZuxR6e2oJT3xg8Rj3gNrHZts93553Mw6y22ZvM7M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lKDLLxFldkJHTfFMbYLrI84maoA2OnXYxeact5l9MUixWxtJbM9FMqWKe6WEGYKz31bsNySBFx6GUzl1NxTfEw=");

    break;

    case 1024:

    this.rsaKeyPair = Convert.FromBase64String("BwIAAACkAABSU0EyAAQAAAEAAQD3/Niww1f/MSTmHsEnhI11C2bSYBbZfAYn+1BtTZPGU3x27FaLS1m1iameRvqLq1MY+vsJ4UM/wZSZ4P7Cqla4qHe/c578nCYnbiJ9ZzrWibjkOfdaAxVh8pa4xipzcxqYgaCK8V44vUOMee1IIFVwnHf8jIKWP1exptuxaODowgGKHldIE0XQ0yyXmUiNDyZbdTKE6lcFAqWbBp92p68x+on4rtnvV6OyGnYm9UJGJRygA9mWKYdjiZGwrCWj9PL31gVp8R+gfsHAP86mMjFiiU/uTS9uJRSCzW71grTTOkQJ99uzxP/4bIsK+gNb5HZtUQkwyF0g2//CaF0z3F/NAfqw0bKeG91KSAfiNBryBhIPEYaYIehIfJ7ZBSVv9glA+zgPT94Pk/1roOwh2EvLaikmIH/vnE2xD5mjMTa0e2942iWbZ2/gHeJ6D/VZkytySIdttAz2iRN2j3BcYXU4CvBuG8asrA8w4kxR3WG+SUJJlUA7IZL+IhbTqI45Uzm4ukPbxtljwJZBGf5dWdVtPVmTjH3nIVkAurUUKZ+nElJO4sJg79+XRKaTJgO5CeCeRRYynJFEmYOs3Kvd1w/BAegdqnIWZXKtZ+ua9aPX1VIZOilMTNACTbuwR10TRK3nhn6/BadnhxmQQAg3MI8X5/uWs6gVorbWiMhno/86PGs7VRaAZB+mzDnsIR/AvV0jJ/C3/PjpmBuxYYt2GEmhM8hQeMlDGwYZfySjWbKrULfTXUnp9yoy795hfBB7m6w=");

    break;

    case 512:

    this.rsaKeyPair = Convert.FromBase64String("BwIAAACkAABSU0EyAAIAAAEAAQAn6+5OoIwY3f0B0ND4BwdjYhRMl4uoZGV2u20FRAJnYZCtuL/W6vNwYF7v8IjRzpBvg6rKsubjx2JLmbDi7V6z0SLUDmw/kVXKqfl6GJD5WyzgKw8zkprM0YvG/ItRs+J3/H0YtGeUK+h/HanJQ+H+lnPXj4R/TMX9XXiMvqWNymGRbCx9klUA56l+qtSBKSmFfmUqlpNA36ozRO3zIn3OB/SvMW+V9ZIeltvAmyb+iKhzMB8BXLEwiM0i8S6cYj4wmOXNsPKb7FTgVfPAcVQdM5p5yKjYlDqQzhU9OLmleoF1yEdUXsbXTee6DKDQhLcgFr+SHqw0gv8dthEVExElL/zVmRXmGFMoOTY7EjQR8P90yZ37rm3WZmYwqXaXLm0=");

    break;

    default:

    this.rsaKeyPair = Convert.FromBase64String("BwIAAACkAABSU0EygAEAAAEAAQDDwLE58+FoKzBRuRqODOm4xIijVGXKl9jOiu8hXgL1ISjHyTv87ToNrqICbDP4StHdWB0D8QyzChca2tMVKvAo6kYa/EFldf4fFvXo1YPLTDbeu4AEY/SZ2siL9GGIj9KptemtRvx2v+vu40REI906Q+EElDFzEogJoSigBjv/fTpBi0cr5WGx7Vj8/4ICbi3n6QeXqlSWdn5V0ksHNq4YbTXnD0/urKeRaGhrOx7Ur6yrNGSSQ3Atal4xW4Lxz+qTqCG5P4E/Pvll2i5uG9BncUj+rsnq3oA=");

    break;

    }

    this.rsaCryptoServiceProvider.ImportCspBlob(this.rsaKeyPair);

    }

    I works like a Charm ;-) Cheers!!!



  • MrMigs

    Firstly, I dont know anything about Hex Encoding. The problem may be arosen you have missed something in Encoding or Decoding data, So you are not getting exact bytes from which you got this String value.

    "I think that .net write something in the blob value to know the rsa key. but i am not sure"

    No, .Net doesnot write anything to know the RSA key but it validates it with its standard algorithm which is failed when you pass wrong in there.

    Best Regards and Best of Luck!!!



  • ancient sord

    Thanks. But i follow to have a problem. I have only this public key

    30819D300D06092A864886F70D010101050003818B0030818702818100C6F80A0CA603136DA265397481E6B00A0BFB0A2D928A139D0E179960E382B7DB1F9A39DB7CA65E113C7BE37DCCD7EA731B4C1722A8C0BB3E38675E15CD4B1344732C09770BDFE2C8028B2BE017168FB5E2AF4F67209A8F663EC4D2F59C70F60F75B683063F55C9499ABABAE5A56902B74CEAFD64BB0D416E5B00A448D2DBF8FB020111

    and i don't know convert this key to a .net rsa valid key... :-)

    I hope that you can help me to idea to traslate this public key.

    thaks :-)



  • rsa public key for c# .net