简体   繁体   English

如何使用BouncyCastle从字符串生成私钥

[英]How to generate Private key from string using BouncyCastle

I have a String stored in a variable: 我有一个存储在变量中的字符串:

-----BEGIN RSA PUBLIC KEY-----
MIGHAoGBANAahj75ZIz9nXqW2H83nGcUao4wNyYZ9Z1kiNTUYQl7ob/RBmDzs5rY
mUahXAg0qyS7+a55eU/csShf5ATGzAXv+DDPcz8HrSTcHMEFpuyYooX6PrIZ07Ma
XtsJ2J4mhlySI5uOZVRDoaFY53MPQx5gud2quDz759IN/0gnDEEVAgED
-----END RSA PUBLIC KEY-----

I generate public key as: 我将公钥生成为:

public static PublicKey getFromString(String keystr) throws Exception
{
  //String S1= asciiToHex(keystr);
   byte[] keyBytes = new sun.misc.BASE64Decoder().decodeBuffer(keystr);
   ASN1InputStream in = new ASN1InputStream(keyBytes);
   DERObject obj = in.readObject();
   RSAPublicKeyStructure pStruct = RSAPublicKeyStructure.getInstance(obj);
   RSAPublicKeySpec spec = new RSAPublicKeySpec(pStrcut.getModulus(), pStruct.getPublicExponent());
   KeyFactory kf = KeyFactory.getInstance("RSA");
   return kf.generatePublic(spec);
}

How to generate the PrivateKey using bouncy castle in android 如何在Android中使用充气城堡生成私钥

{Edit} {编辑}

Without using bouncy castle i am generating private key like this: 在不使用充气城堡的情况下,我将生成如下私钥:

public static PrivateKey getKey(String mKey){
        try{
            // Read in the key into a String
            StringBuilder pkcs8Lines = new StringBuilder();
            BufferedReader rdr = new BufferedReader(new StringReader(mKey));
            String line;
            while ((line = rdr.readLine()) != null) {
                pkcs8Lines.append(line);
            }

            // Remove the "BEGIN" and "END" lines, as well as any whitespace

            String pkcs8Pem = pkcs8Lines.toString();
            pkcs8Pem = pkcs8Pem.replace("-----BEGIN RSA PRIVATE KEY-----", "");
            pkcs8Pem = pkcs8Pem.replace("-----END RSA PRIVATE KEY-----", "");
            pkcs8Pem = pkcs8Pem.replaceAll("\\s+","");

            // Base64 decode the result

            byte [] pkcs8EncodedBytes = Base64.decode(pkcs8Pem, Base64.DEFAULT);

            // extract the private key

            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PrivateKey privKey = kf.generatePrivate(keySpec);
            System.out.println(privKey);

            return privKey;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

I want to achieve the same using Bouncy Castle 我想使用Bouncy Castle实现相同的效果

I'm a little confused on why you insist on using bouncycastle, but if you really want to use bouncycastle then the CMS/PKIX library has a nice helper class called PEMParser that will shorten the code needed, eg: 我对为什么您坚持使用bouncycastle感到有些困惑,但是如果您真的想使用PEMParser ,则CMS / PKIX库提供了一个很好的帮助程序类PEMParser ,它将缩短所需的代码,例如:

public static PrivateKey getPemPrivateKey(String mKey) throws Exception {
    PEMParser pemParser = new PEMParser(new StringReader(mKey));
    final PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
    final byte[] encoded = pemKeyPair.getPrivateKeyInfo().getEncoded();
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM