[botan-devel] java pkcs8 encoding -> botan

Timothy Prepscius timprepscius at gmail.com
Tue Jul 24 15:10:05 EDT 2012


Sure:

	public void generate () throws IOException, NoSuchAlgorithmException
	{
		Q.println("Generating 2048 bit RSA Key... ");
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(2048);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		privateKey = keyPair.getPrivate();
		publicKey = keyPair.getPublic();
		Q.println("PrivateKey will be encoded using: " + privateKey.getFormat());
		Q.println("KEY START -----------------------");
		Q.println(new String(Base64.encode(privateKey.getEncoded())));
		Q.println("KEY END -----------------------");
		Q.println("PublicKey will be encoded using: " + publicKey.getFormat());
		Q.println("KEY START -----------------------");
		Q.println(new String(Base64.encode(publicKey.getEncoded())));
		Q.println("KEY END -----------------------");
		
		Q.println("Done");
	}


Generating 2048 bit RSA Key... 
PrivateKey will be encoded using: PKCS#8
KEY START -----------------------
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDlbI+Qa2zUwSRki7iTrcog4Zu0hrrx6jGlrC+xTsMfRj/gGA5npOBziSQMmqR6zhuVTR/9OAk5lX5muGQ2NdsngwE0gl783qjkQFvQNkQDCH8tK6oiC+lVKSLYUa57f9vusCOU1elq3PTCfWAU0myV59Ji2YvX6uxpi7bWeBx1ymJoofRaQlAupdMuXcRaLkA4otdlMLHO6PRH1XDzfMuD/WeDEbbDp3Rrkm2EEl2Ck9TCY7GLwD6+xJUA4Ok2FQgmZQ0uqk/0cGDVJa6rxnlRWNg4Ks8Ut5vqFfEFRGd6XuxbYmv6t4YseOow+e00cd7YDb10y/5eX19X80WYR3uhAgMBAAECggEAaYnbMRB57BU9bJNpwWW9Cgs+2XjxHsAhPplLB0G4OPbUA36Ngdgy+HMqBjJNX8DYp4HnKVFRzQ+cKhzA3L9BpZd6QnGGyMSU5QtGMS4Fc2/BMUJY7XiVSfu2b9SDc4SJ4+9bLuiIjARBBAcw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Y=
KEY END -----------------------
PublicKey will be encoded using: X.509
KEY START -----------------------
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5WyPkGts1MEkZIu4k63KIOGbtIa68eoxpawvsU7DH0Y/4BgOZ6Tgc4kkDJqkes4blU0f/TgJOZV+ZrhkNjXbJ4MBNIJe/N6o5EBb0DZEAwh/LSuqIgvpVSki2FGue3/b7rAjlNXpatz0wn1gFNJslefSYtmL1+rsaYu21ngcdcpiaKH0WkJQLqXTLl3EWi5AOKLXZTCxzuj0R9Vw83zLg/1ngxG2w6d0a5JthBJdgpPUwmOxi8A+vsSVAODpNhUIJmUNLqpP9HBg1SWuq8Z5UVjYOCrPFLeb6hXxBURnel7sW2Jr+reGLHjqMPntNHHe2A29dMv+Xl9fV/NFmEd7oQIDAQAB
KEY END -----------------------
Done

Hmm if this doesn't format, I'll try attaching a file.



I am checking my code over and over for some typo, the public key loads fine.

-tim


On Jul 24, 2012, at 2:58 PM, Jack Lloyd wrote:

> 
> The code looks plausible. Can you post an example key from the Java side?
> 
> On Tue, Jul 24, 2012 at 02:52:45PM -0400, Timothy Prepscius wrote:
>> ok, so I think I have a pkcs8 encoded private key.
>> 
>> The java code looks like this:
>> 
>> new String(Base64.encode(privateKey.getEncoded()));
>> 
>> where privateKey was generated with:
>> 
>> KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
>> keyPairGenerator.initialize(2048);
>> KeyPair keyPair = keyPairGenerator.genKeyPair();
>> 
>> the 
>> 
>> privateKey.getFormat()
>> says: PKCS#8
>> 
>> 
>> ----------------------------------------
>> 
>> 
>> I'm trying to get that private key into botan.
>> It would be best if I could do it without changing the java code.  But that is an option if all else fails :-)
>> 
>> 
>> I was trying code like this:
>> 
>> 	if (!privateKey.empty())
>> 	{
>> 		AutoSeeded_RNG rng;
>> 		Block key = fromBase64(privateKey);
>> 		Botan::DataSource_Memory dataSource((const unsigned char *)key.data(), (Botan::u32bit)key.size());
>> 		Botan::PKCS8_PrivateKey *genericPrivateKey = Botan::PKCS8::load_key(dataSource, rng);
>> 
>> 		Botan::RSA_PrivateKey *rsaPrivateKey = 
>> 			dynamic_cast<Botan::RSA_PrivateKey *> (
>> 				genericPrivateKey
>> 			);
>> 			
>> 		privateKeyImpl = rsaPrivateKey;
>> 	}
>> 
>> 
>> But of course, I think that the format is completely different, not sure, actually.
>> And it throws a decoding exception
>> 
>> 
>> Any thoughts?
>> 
>> -tim
>> 
>> 
>> 
> 
>> _______________________________________________
>> botan-devel mailing list
>> botan-devel at randombit.net
>> http://lists.randombit.net/mailman/listinfo/botan-devel
> 
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://lists.randombit.net/mailman/listinfo/botan-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.randombit.net/pipermail/botan-devel/attachments/20120724/b49c9546/attachment.html>


More information about the botan-devel mailing list