• 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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    本文讲述如何从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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    测试代码:

    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]);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试结果:

    在这里插入图片描述

    小结:

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

  • 相关阅读:
    Github 2024-07-11 开源项目日报 Top10
    一键安装上新版本的QQ
    axios入门
    用户管理命令
    harbor v1.7.1镜像仓库无法访问,并提示502 Bad Gateway
    采用jieba库的posseg函数对剩余有效文本数据进行词性分析
    Java——》JVM对原生的锁做了哪些优化
    Centos7 安装keepalived
    CSS3新特性
    0元真的能做游戏代理吗?
  • 原文地址:https://blog.csdn.net/shenweihong/article/details/133878849