概述:
在Java编程中,我们经常用到如下一段代码来生成RSA公私钥,分别拿到公私钥然后加解密计算:
KeyPairGenerator keyPairGen;
keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
本文讲述如何从RSAPublicKey publicKey和RSAPrivateKey privateKey
分别提取出模和指数
开发运行环境:
编辑器:android studio
sdk:
compileSdkVersion 30
buildToolsVersion "30.0.3"m
inSdkVersion 24
targetSdkVersion 26
运行平台:android13
RSAPublicKey提取模和指数代码:
public String[] GetModuleEFromRsaPubCtx(RSAPublicKey publicKey) {
String publicKeyString = null;
String[] outPubPartKeys = null;
java.security.KeyFactory keyFactory = null;
java.security.spec.X509EncodedKeySpec bobPubKeySpec = null;
java.security.interfaces.RSAPublicKey pubKey = null;
try {
keyFactory = java.security.KeyFactory.getInstance("RSA");
bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(publicKey.getEncoded());
pubKey = (java.security.interfaces.RSAPublicKey) keyFactory.generatePublic(bobPubKeySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (pubKey == null) {
return null;
}
outPubPartKeys = new String[2];
publicKeyString = pubKey.getModulus().toString(16).toUpperCase();
outPubPartKeys[0] = publicKeyString;
publicKeyString = pubKey.getPublicExponent().toString(16).toUpperCase();
outPubPartKeys[1] = publicKeyString;
return outPubPartKeys;
}
RSAPrivateKey提取模和指数代码:
public String[] GetModuleEFromRsaPriCtx(RSAPrivateKey prikey) {
String priKeyString = null;
String[] outPriPartKeys = null;
java.security.KeyFactory keyFactory = null;
java.security.spec.PKCS8EncodedKeySpec bobPriKeySpec = null;
java.security.interfaces.RSAPrivateKey priKey = null;
try {
keyFactory = java.security.KeyFactory.getInstance("RSA");
bobPriKeySpec = new java.security.spec.PKCS8EncodedKeySpec(prikey.getEncoded());
priKey = (java.security.interfaces.RSAPrivateKey) keyFactory.generatePrivate(bobPriKeySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (priKey == null) {
return null;
}
outPriPartKeys = new String[2];
priKeyString = priKey.getModulus().toString(16).toUpperCase();
outPriPartKeys[0] = priKeyString;
priKeyString = priKey.getPrivateExponent().toString(16).toUpperCase();
outPriPartKeys[1] = priKeyString;
return outPriPartKeys;
}
测试代码:
void test() {
KeyPairGenerator keyPairGenerator = null;
String keys[] = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
// 指定密钥长度为2048
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
keys = GetModuleEFromRsaPubCtx((RSAPublicKey)keyPair.getPublic());
Log.i("123456", "pub Get Module = " + keys[0]);
Log.i("123456", "pub Get E = " + keys[1]);
keys = GetModuleEFromRsaPriCtx((RSAPrivateKey) keyPair.getPrivate());
Log.i("123456", "pri Get Module = " + keys[0]);
Log.i("123456", "pri Get E = " + keys[1]);
}
测试结果:
文章来源:https://www.toymoban.com/news/detail-723308.html
小结:
本文描述了提取模和指数,暂时没有做到从RSAPrivateKey获取质数P和Q,质数P和Q肯定能获取到,以后有空研究。文章来源地址https://www.toymoban.com/news/detail-723308.html
到了这里,关于Java RSA密钥从RSAPrivateKey和RSAPublicKey对象中,分别提取模和指数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!