Skip to content

Instantly share code, notes, and snippets.

@nicoruti
Created January 31, 2015 23:38
Show Gist options
  • Save nicoruti/996995dacb97990d6db8 to your computer and use it in GitHub Desktop.
Save nicoruti/996995dacb97990d6db8 to your computer and use it in GitHub Desktop.
FST Serialize BouncyCastle Keypair
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.RSAKeyGenParameterSpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.nustaq.serialization.FSTConfiguration;
public class TestSerializeKeyFST {
// Fermat F4, largest known fermat prime
private static final BigInteger PUBLIC_EXP = new BigInteger("10001", 16);;
private static final int STRENGTH = 1024;
public static void main(String[] args) throws Exception {
// install BouncyCastle provider
Security.addProvider(new BouncyCastleProvider());
// generate a keypair
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "BC");
RSAKeyGenParameterSpec params = new RSAKeyGenParameterSpec(STRENGTH, PUBLIC_EXP);
gen.initialize(params, new SecureRandom());
KeyPair keyPair = gen.generateKeyPair();
FSTConfiguration fst = FSTConfiguration.createDefaultConfiguration();
// serialize
byte[] serialized = fst.asByteArray(keyPair);
// deserialize --> crash
KeyPair deserialized = (KeyPair) fst.asObject(serialized);
}
}
@RuedigerMoeller
Copy link

Also they made OptionalDataException private, so I can't throw it from my code ... WTF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment