Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bouncy Castle, RSA: transforming keys into a String format

I'm using RSA (Bouncy Castle API) in my C# project. I generated the keypair with this method:

RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();

AsymmetricKeyParameter private_key = keys.Private;
AsymmetricKeyParameter public_key = keys.Public;

Now I want to save them in a txt file but the problem is that I can't convert them to a string format. I read in another post that keys must be serialized using:

PrivateKeyInfo k = PrivateKeyInfoFactory.CreatePrivateKeyInfo(private_key);
byte[] serializedKey = k.ToAsn1Object().GetDerEncoded();

Is it the right way? If yes, what should I do after this? Just convert them from byte[] to String?

like image 482
Skeenor Avatar asked Feb 23 '11 12:02

Skeenor


4 Answers

You could also use PemWriter to store them in PEM format:

TextWriter textWriter = new StringWriter(); PemWriter pemWriter = new PemWriter(textWriter); pemWriter.WriteObject(keys.Private); pemWriter.Writer.Flush();  string privateKey = textWriter.ToString(); 

Now privateKey contain something like this:

-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQDFhB3xI1AzSMsKvt7rZ7gp2o/vd49zON89iL1ENvKkph7oFXa2 ew/hjzbAV33lHnFFlA/vA5SDCbggRyU1/SmjfuPJFEzFbgcPLuO1Sw6z+bWXiIFp QNCOTIw11c0fbwk+kB2y4E1OkLv5f9atlNlekb4wBn8bMbFYtu6LGWjkFQIDAQAB AoGANPoMteLM3hSGMSmkzOsS4Fb5CDe/SB5W8kA805ERX8wWuhUXtDYpukwZWVme MNgLdagS5f7F1KfEtROgDW0Lv4/T/FWAbpgh8O2CPKYDY4ZXl8tmRH9dtYn/824l veLxdgNjHwo5OMvuTSDMjC3tbg2UA3kmV4VAm5QotlRinUECQQDo4zvI5e81zWnS kNrUARX80t432MOZyr0RvAaVJfBNQpJl0H0xp5LKP74hvPVO9WdJvJ0M4Z4QrmGO bm2Hsfz5AkEA2R469YXxgPLDg/LvUQqfrl8Ji9+rip7eQP7iS/dt66NMS31/HymT +HscEZ3qGlnQuyyyRR2rGQdhdjU42HNy/QJBAKbPTF1DxSmGbovyUauU84jaCW17 B++6dd6kDRr7x7mvO2lOAr50RwIy0h8cV6EsycTZIqy9VhigryP0GOQfKxECQA8T uVZpab7hnNAh45qGGVabhOcwrhHfPGHZEU/jK7/sRBUN7vD0CzF7IxTaGXKhAAyv auW/zKzdRVhXE858HeUCQQCGaaAg8GwM0qIS0nHRTLldu4hIGjKn7Sk0Z46Plfwr oqPCtuP4ehX85EIhqCcoFnG6Ttr6AxSgNMZvErVxDBiD -----END RSA PRIVATE KEY----- 
like image 178
JoDG Avatar answered Sep 30 '22 00:09

JoDG


Well, I don't know about the RSA-specific side, but once you've got an opaque binary string (i.e. it could contain any arbitrary data) the best bet for text conversion is Convert.ToBase64String(byte[]) which you can reverse with Convert.FromBase64String(string).

Do not use Encoding.GetString(byte[]) and Encoding.GetBytes(string) for this - the binary data isn't text in a particular encoding, and shouldn't be treated as such. You're almost bound to lose data if you try this.

like image 32
Jon Skeet Avatar answered Sep 29 '22 22:09

Jon Skeet


This might be what you are looking out for: http://www.rahulsingla.com/blog/2011/04/serializing-deserializing-rsa-public-private-keys-generated-using-bounty-castle-library

like image 31
r_honey Avatar answered Sep 29 '22 23:09

r_honey


Try the following

        RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
        rsaKeyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), XXX));

        AsymmetricCipherKeyPair keys = rsaKeyPairGenerator.GenerateKeyPair();

        PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keys.Private);
        // Write out an RSA private key with it's asscociated information as described in PKCS8.
        byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetDerEncoded();
        // Convert to Base64 ..
        string serializedPrivateString = Convert.ToBase64String(serializedPrivateBytes);

        SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keys.Public);
        byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
        string serializedPublicString = Convert.ToBase64String(serializedPublicBytes);
like image 38
Venkata Varagani Avatar answered Sep 29 '22 23:09

Venkata Varagani