Created
January 31, 2015 23:38
-
-
Save nicoruti/996995dacb97990d6db8 to your computer and use it in GitHub Desktop.
FST Serialize BouncyCastle Keypair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also they made OptionalDataException private, so I can't throw it from my code ... WTF