rsa-2048-ecc

公钥属性

 public static void main(String[] args) throws Exception {



        KeyPairGenerator keyPairGenerator;
        keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        SubjectPublicKeyInfo convert = SubjectPublicKeyInfoBuilder.convert(publicKey);
        System.out.println("convert = " + convert.toString());
    }

rsa-2048-ecc_第1张图片

oid: 1.2.840.113549.1.1.1

rsa-2048-ecc_第2张图片

签名算法 

sha256withrsa

1.2.840.113549.1.1.11

rsa-2048-ecc_第3张图片

 

sha256wthecdsa

http://oid-info.com/get/1.2.840.10045.4.3.2

rsa-2048-ecc_第4张图片

 

公钥 - x509格式:

2048 的 rsa 为例

30820122 300D06092A864886F70D0101010500 0382010F 003082010A 0282010100 || 512个字符的N || 02 03 010001

比如N (modules) = ab46a231bd39c475c15060d2cbceb6f26ee1b72399186177ef20b20a74fa57a1adcbbeab13d1173dfff70d73306cd987bd18ac00c437e90d4aae5f2f40c2f4ba56d8cc2fbb659871f3ec4db03105238e43a36ac9ca604c651612a27a67249edbcb407cc451bc77f3f7eac4528e0abb1462116e09c78502d519af01006361318d9079eab8e500608f9a27074db4b1cb925805d466f13b69ec9cd604928276bf886e15cad54c0f4f13d4ce5a6f4b201dcfa9f8e8db3e01b4cdb6d343221e8680e50379710083d5be9623b2e65997636dceafb4f84d433eff7504e57a420d178c1ce43d29e358c293dff2ecf85a114e021940ccf82169c34eda9a7426e49f4d8d63

转换成 x509格式的rsa 2048公钥后是:

30820122300D06092A864886F70D01010105000382010F003082010A0282010100ab46a231bd39c475c15060d2cbceb6f26ee1b72399186177ef20b20a74fa57a1adcbbeab13d1173dfff70d73306cd987bd18ac00c437e90d4aae5f2f40c2f4ba56d8cc2fbb659871f3ec4db03105238e43a36ac9ca604c651612a27a67249edbcb407cc451bc77f3f7eac4528e0abb1462116e09c78502d519af01006361318d9079eab8e500608f9a27074db4b1cb925805d466f13b69ec9cd604928276bf886e15cad54c0f4f13d4ce5a6f4b201dcfa9f8e8db3e01b4cdb6d343221e8680e50379710083d5be9623b2e65997636dceafb4f84d433eff7504e57a420d178c1ce43d29e358c293dff2ecf85a114e021940ccf82169c34eda9a7426e49f4d8d630203010001
   /**
     * 
     *
     * @param publicKey 公钥
     * @return
     * @throws Exception
     */
    public static SubjectPublicKeyInfo convert(byte[] publicKey) throws Exception {
        // JCE中公钥中的getEncoded()方法生成一个对其中之一进行编码的DER。
        ASN1InputStream asn1InputStream = new ASN1InputStream(publicKey);
        try {
            SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(asn1InputStream.readObject());
            return publicKeyInfo;
        } finally {
            asn1InputStream.close();
        }
    }
 /**
     *  ok
     * @throws Exception
     */
    @Test
    public void test_rsa_2048_x509_2() throws Exception {
        byte[] rsaPKX509Format = HexUtils.toByteArray("30820122300D06092A864886F70D01010105000382010F003082010A0282010100ab46a231bd39c475c15060d2cbceb6f26ee1b72399186177ef20b20a74fa57a1adcbbeab13d1173dfff70d73306cd987bd18ac00c437e90d4aae5f2f40c2f4ba56d8cc2fbb659871f3ec4db03105238e43a36ac9ca604c651612a27a67249edbcb407cc451bc77f3f7eac4528e0abb1462116e09c78502d519af01006361318d9079eab8e500608f9a27074db4b1cb925805d466f13b69ec9cd604928276bf886e15cad54c0f4f13d4ce5a6f4b201dcfa9f8e8db3e01b4cdb6d343221e8680e50379710083d5be9623b2e65997636dceafb4f84d433eff7504e57a420d178c1ce43d29e358c293dff2ecf85a114e021940ccf82169c34eda9a7426e49f4d8d630203010001");
        SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoBuilder.convert(rsaPKX509Format);
        AlgorithmIdentifier algorithm = subjectPublicKeyInfo.getAlgorithm();

        String s = algorithm.toASN1Primitive().toString();
        System.out.println("algorithm ASN1Primitive = " + s);
        byte[] encoded1 = algorithm.getEncoded();
        System.out.println("algorithm encoded = " +  HexUtils.toHexString( encoded1 ));

        DERBitString publicKeyData = subjectPublicKeyInfo.getPublicKeyData();
        byte[] bytes = publicKeyData.getBytes();
        System.out.println("publicKeyDatabytes = " + HexUtils.toHexString(bytes) );


    }

algorithm ASN1Primitive = [1.2.840.113549.1.1.1, NULL]
algorithm encoded = 300D06092A864886F70D0101010500
publicKeyDatabytes = 3082010A0282010100AB46A231BD39C475C15060D2CBCEB6F26EE1B72399186177EF20B20A74FA57A1ADCBBEAB13D1173DFFF70D73306CD987BD18AC00C437E90D4AAE5F2F40C2F4BA56D8CC2FBB659871F3EC4DB03105238E43A36AC9CA604C651612A27A67249EDBCB407CC451BC77F3F7EAC4528E0ABB1462116E09C78502D519AF01006361318D9079EAB8E500608F9A27074DB4B1CB925805D466F13B69EC9CD604928276BF886E15CAD54C0F4F13D4CE5A6F4B201DCFA9F8E8DB3E01B4CDB6D343221E8680E50379710083D5BE9623B2E65997636DCEAFB4F84D433EFF7504E57A420D178C1CE43D29E358C293DFF2ECF85A114E021940CCF82169C34EDA9A7426E49F4D8D630203010001
 

你可能感兴趣的:(算法,java,算法)