RSA密钥格式

public static void generateRSAKey() {
try {
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 注意密钥大小1024
kg.initialize(1024, sr);
KeyPair keyPair = kg.generateKeyPair();
PrivateKey priKey = keyPair.getPrivate();
PublicKey pubKey = keyPair.getPublic();
byte[] publicKey = pubKey.getEncoded();
byte[] privateKey = priKey.getEncoded();
String hexPublicKey = byteArr2HexString(publicKey);
System.out.println("公钥:"+pubKey.getFormat() + " ; " + byteArr2HexString(publicKey));
System.out.println("私钥:"+priKey.getFormat() + " ; " +byteArr2HexString(privateKey));
} catch (Exception e) {
e.printStackTrace();
}
}


公钥:X.509 ; 

30819f300d06092a864886f70d010101050003818d003081890281810090169dd9c5edc8f09c971d8583c342b1059068164458aa0faabf3a66c9755cd4d41b173758b1d1ad839d4ab0384314a49c6766b432aa15b4a009f7767682a180fb7b0f5a2926bbb339d5924e3c86e29713b30c66c4570917e731cd5ebc93712001e496ef3a74ad9db69aa41c68839231f43a0829ce67ec60b3db2d239de2c6f70203010001

私钥:PKCS#8 ; 30820276020100300d06092a864886f70d0101010500048202603082025c0201000281810090169dd9c5edc8f09c971d8583c342b1059068164458aa0faabf3a66c9755cd4d41b173758b1d1ad839d4ab0384314a49c6766b432aa15b4a009f7767682a180fb7b0f5a2926bbb339d5924e3c86e29713b30c66c4570917e731cd5ebc93712001e496ef3a74ad9db69aa41c68839231f43a0829ce67ec60b3db2d239de2c6f70203010001028180798008c55d28c0bb5c5ff50de3e8e030e3af3c862245ddc96e42e85d4a5dc3d4d7b2ac66084d91b0cdfdf1c01d013bc3fefb1658b72c433757886ffa9dbe53ae6af5ca72c9006a4477982ed9ca16be913b887f83d9ada64dca6eca51b9b9ed8e13b6239b5f24126962382581793fa755aa37b3d68974118ccb08f0075c4012f9024100c1c66c186c80408c34a5f93739a17feb5e4617666d641b6325a3e865a3d97333dd6b8a0f85f2ef0509408823de959f6e0ab605f924cb232bd3ba858932602895024100be5b9ea77653340954f64652b2030fe892c0de355555a51aeb8c82af651dc6e878edfe5ce2010f059c0fba6a15a453dd0924d9830d8ee4b5af8ee309129a725b02403efcb7b06cbc77bedb7244032e898fa23b7fbf257d53780f8f71ab1e36d77d3a13b45c29559a3515c6ff2201bedd95f85824f8e6168ead7d1692dfcb4c03b3cd0241008d6d5e93869acc977e5336ee2ba990c1377f29d8b01ce07cfeb23749feb33fdbea34cffdcc40031c24d59ea7408b056fbc626f6429c7976ed10f34e8c1b493ef02403cf5c5515b80976383197e006783a41716b47cacf8604f6874e0cbcec3e5f4cf301e319a1cdc1acf38974fdea4114e30be1a3189c0fd2a28f114cf47f16af0fe


其中公钥的X.509格式删除前44位得到DER格式:3081890281810090169dd9c5edc8f09c971d8583c342b1059068164458aa0faabf3a66c9755cd4d41b173758b1d1ad839d4ab0384314a49c6766b432aa15b4a009f7767682a180fb7b0f5a2926bbb339d5924e3c86e29713b30c66c4570917e731cd5ebc93712001e496ef3a74ad9db69aa41c68839231f43a0829ce67ec60b3db2d239de2c6f70203010001

你可能感兴趣的:(基础)