none
Represent RSA keys as simple strings. RRS feed

  • Question

  • I'm not very familiar with RSA details but I did expect it would be straightforward to generate a key pair and be able to see the two keys as "simple" strings. By that I mean not have to see any XML structures.

    Everything I see deals with RSAParameters or the XML version of them.

    But what I ideally want is to see each key as just a long hex (or whatever) string.

    I know RSA is pretty secure too, so I was a bit surprised to see the public key as two values, surely know the key's components makes hacking a bit easier than not knowing those components?

    Thanks

    Tuesday, October 18, 2016 5:07 PM

Answers

  • The problem to your questions is that, the key for RSA has 8 parts when with private key instead of 2, so you need some way to identify the values in order to assign it back for decryption. Feel free to convert the parameters one by one to hex values yourself and put some delimiters such as ";" in between, then read it back yourself when you need to use it again.
    • Marked as answer by Captain Kernel Wednesday, October 19, 2016 4:04 PM
    Wednesday, October 19, 2016 1:54 AM
    Answerer

All replies

  • The problem to your questions is that, the key for RSA has 8 parts when with private key instead of 2, so you need some way to identify the values in order to assign it back for decryption. Feel free to convert the parameters one by one to hex values yourself and put some delimiters such as ";" in between, then read it back yourself when you need to use it again.
    • Marked as answer by Captain Kernel Wednesday, October 19, 2016 4:04 PM
    Wednesday, October 19, 2016 1:54 AM
    Answerer
  • The problem to your questions is that, the key for RSA has 8 parts when with private key instead of 2, so you need some way to identify the values in order to assign it back for decryption. Feel free to convert the parameters one by one to hex values yourself and put some delimiters such as ";" in between, then read it back yourself when you need to use it again.

    OK, so that's pretty much how to this - very helpful.

    I assume the ; can never occur in the key data itself? I can't find any MS documentation that defines the character set which represents each of the XML strings. Presumably any binary character could occur and so it seems that any ASCII character could therefore also occurr...

    Thanks


    Wednesday, October 19, 2016 4:04 PM
  • Oh, I should have explain more clearly.

    You can use something like 

    String.Join("", param.D.Select(x => x.ToString("X").PadLeft(2'0')));
    

    to turn the byte array into Hexdecimal string representation of bytes. Then combine each parts into single string seperated by ";" for storage.

    To use it, split the stored string using ";" as seperator, and then use something the following to convert it back to byte array (assuming key is the  string[] you get from String.Split()):

    param.D = Enumerable.Range(0, key.Length / 2).Select(x => Convert.ToByte(key[0].Substring(2 * x, 2), 16)).ToArray();
    


    Thursday, October 20, 2016 1:31 AM
    Answerer