1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > RSA生成公钥私钥

RSA生成公钥私钥

时间:2022-02-05 21:07:53

相关推荐

RSA生成公钥私钥

public static void genKeyPair()

{ // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");

// 初始化密钥对生成器,密钥大小为96-1024位

keyPairGen.initialize(1024,new SecureRandom());

// 生成一个密钥对,保存在keyPair中

KeyPair keyPair = keyPairGen.generateKeyPair();

RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

// 得到私钥

RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

// 得到公钥

String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));

// 得到私钥字符串

String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));

// 将公钥和私钥保存到Map

keyMap.put(0,publicKeyString);

//0表示公钥

keyMap.put(1,privateKeyString);

//1表示私钥

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。