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

测试结果:

在这里插入图片描述

小结:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值