Java RSA密钥从RSAPrivateKey和RSAPublicKey对象中,分别提取模和指数

概述:

在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]);
    }

测试结果:

Java RSA密钥从RSAPrivateKey和RSAPublicKey对象中,分别提取模和指数_第1张图片

小结:

本文描述了提取模和指数,暂时没有做到从RSAPrivateKey获取质数P和Q,质数P和Q肯定能获取到,以后有空研究。

你可能感兴趣的:(java,开发语言)