久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從字符串創(chuàng)建 RSA 公鑰

Creating RSA Public Key From String(從字符串創(chuàng)建 RSA 公鑰)
本文介紹了從字符串創(chuàng)建 RSA 公鑰的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我使用 1024 RSA 生成了這個(gè)測(cè)試公鑰,然后在另一個(gè)編碼平臺(tái)中將其編碼為 DER 和 Base64.我將密鑰復(fù)制到 Android/Eclipse 中的字符串中,并嘗試使用 KeyFactory 將其轉(zhuǎn)換為公鑰.無論我嘗試什么,它都會(huì)一直給我一個(gè) InvalidKeySpecException .任何建議都將不勝感激.

I've generated this test public key using 1024 RSA and then encoded it to DER and Base64 in another coding platform. I copied the key into a string in Android/Eclipse and I am trying to turn it into a public key using KeyFactory. It just keeps giving me an InvalidKeySpecException no matter what I try. Any advice at all would be appreciated.

     private void prepKeys() {
         String AppKeyPub = "MIGHAoGBAOX+TFdFVIKYyCVxWlnbGYbmgkkmHmEv2qStZzAFt6NVqKPLK989Ow0RcqcDTZaZBfO5" +
"5JSVHNIKoqULELruACfqtGoATfgwBp4Owfww8M891gKNSlI/M0yzDQHns5CKwPE01jD6qGZ8/2IZ" +
"OjLJNH6qC9At8iMCbPe9GeXIPFWRAgER";

        // create the key factory          
            try {
                KeyFactory kFactory = KeyFactory.getInstance("RSA");  
                // decode base64 of your key
                byte yourKey[] =  Base64.decode(AppKeyPub,0);
                // generate the public key
                X509EncodedKeySpec spec =  new X509EncodedKeySpec(yourKey);
                PublicKey publicKey = (PublicKey) kFactory.generatePublic(spec);

            System.out.println("Public Key: " + publicKey);  

            } catch (Exception e) {
                // TODO Auto-generated catch block  
                e.printStackTrace(); 
            }

         }

推薦答案

您擁有的密鑰是 PKCS#1 格式,而不是 Java 接受的 SubjectPublicKeyInfo 結(jié)構(gòu).PKCS#1 只是 RSA 參數(shù)的編碼,缺少諸如算法標(biāo)識(shí)符之類的東西.SubjectPublicKeyInfo 在內(nèi)部使用 PKCS#1 - 無論如何都用于 RSA 公鑰.

The key you have is in PKCS#1 format instead of SubjectPublicKeyInfo structure that Java accepts. PKCS#1 is the encoding of the RSA parameters only and lacks things such as an algorithm identifier. SubjectPublicKeyInfo uses PKCS#1 internally - for RSA public keys anyway.

由于 PKCS#1 公鑰位于 SubjectPublicKeyInfo 結(jié)構(gòu)的末尾,因此可以簡(jiǎn)單地為字節(jié)添加前綴,以便它們成為 RSA SubjectPublicKeyInfo.該解決方案更易于執(zhí)行,無需額外的庫,例如 Bouncy Castle.因此,如果您需要不使用外部庫,那么您可以查看我的答案此處.

As the PKCS#1 public key is at the end of the SubjectPublicKeyInfo structure it is possible to simply prefix the bytes so that they become an RSA SubjectPublicKeyInfo. That solution is easier to perform without additional libraries such as Bouncy Castle. So if you need to go without an external library then you may have a look at my answer here.

或者,可以編寫一個(gè)簡(jiǎn)單的 BER 解碼器,將結(jié)構(gòu)解碼為兩個(gè) BigInteger 值.結(jié)構(gòu)本身并不復(fù)雜但BER/DER長(zhǎng)度編碼需要一些時(shí)間來適應(yīng).

Alternatively a simple BER decoder could be written to decode the structure into the two BigInteger values. The structure itself is not that complicated but the BER/DER length encoding takes some getting used to.

不過,您也可以使用 Bouncy Castle(輕量級(jí) API)來解決您的問題:

However, you can also use Bouncy Castle (lightweight API) to solve your issues:

String publicKeyB64 = "MIGHAoGBAOX+TFdFVIKYyCVxWlnbGYbmgkkmHmEv2qStZzAFt6NVqKPLK989Ow0RcqcDTZaZBfO5"
        + "5JSVHNIKoqULELruACfqtGoATfgwBp4Owfww8M891gKNSlI/M0yzDQHns5CKwPE01jD6qGZ8/2IZ"
        + "OjLJNH6qC9At8iMCbPe9GeXIPFWRAgER";
// ok, you may need to use the Base64 decoder of bouncy or Android instead
byte[] decoded = Base64.getDecoder().decode(publicKeyB64);
org.bouncycastle.asn1.pkcs.RSAPublicKey pkcs1PublicKey = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(decoded);
BigInteger modulus = pkcs1PublicKey.getModulus();
BigInteger publicExponent = pkcs1PublicKey.getPublicExponent();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey generatedPublic = kf.generatePublic(keySpec);
System.out.printf("Modulus: %X%n", modulus);
System.out.printf("Public exponent: %d ... 17? Why?%n", publicExponent); // 17? OK.
System.out.printf("See, Java class result: %s, is RSAPublicKey: %b%n", generatedPublic.getClass().getName(), generatedPublic instanceof RSAPublicKey);

正如您所見,它實(shí)際上只需要一個(gè)類作為接口,盡管這當(dāng)然得到了 Bouncy Castle 中整個(gè) ASN.1/BER 解碼器功能的支持.

As you can see it actually only requires a single class as interface, although that is of course backed up with the entire ASN.1/BER decoder functionality within Bouncy Castle.

請(qǐng)注意,可能需要將 Base 64 解碼器更改為 Android 特定的一個(gè)(android.util.Base64).此代碼已在等效的 Java 運(yùn)行時(shí)測(cè)試.

Note that it may be required to change the Base 64 decoder to the Android specific one (android.util.Base64). This code was tested on an equivalent Java runtime.

這篇關(guān)于從字符串創(chuàng)建 RSA 公鑰的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久精品亚洲精品国产欧美 | 中文字幕99 | 亚洲国产精品激情在线观看 | 岛国av一区二区 | 国产色网 | 久久综合久 | 狠狠婷婷综合久久久久久妖精 | 在线 丝袜 欧美 日韩 制服 | 欧美xxxx日本| 成年人免费网站 | 久久国产精品网 | 成人午夜精品 | 午夜精品久久久久久久99黑人 | 中文字幕亚洲欧美 | 日本高清视频在线播放 | 欧美日韩国产一区二区三区 | 国产一区在线免费观看视频 | 国产乱码精品一区二区三区五月婷 | 亚洲免费婷婷 | 免费观看一级特黄欧美大片 | 妞干网福利视频 | 国产欧美日韩久久久 | 在线视频成人 | 欧美激情综合 | 成人午夜视频在线观看 | 日日日色 | 日韩高清一区 | 6996成人影院网在线播放 | 国产精品国产精品国产专区不卡 | 日韩高清成人 | 精品成人69xx.xyz | 日韩一区在线视频 | 亚洲国产精品久久久久 | 久久国产精品无码网站 | 免费精品视频一区 | 在线免费黄色小视频 | 色婷婷综合久久久中字幕精品久久 | 久久久久免费精品国产小说色大师 | 国产一级免费视频 | 欧美亚洲一级 | 国产精品一区二区久久久久 |