代码之家  ›  专栏  ›  技术社区  ›  InDaPond

使用十六进制字符串密钥格式生成RSA私钥

  •  1
  • InDaPond  · 技术社区  · 7 年前

    我有钥匙。存储以下信息的json数据:

    {
      "privateExponent":"0x74326408a9392dc21ab4297391a8c2152c165ca71a3f2282ded681f2cbf8c999eb27bb17524edf431004fd5c5c4eae82ee9138d5bebd61b80f497f762a8bace0baaee6a374f94b27ee4824ebacbfed15d568f9cc17b369af3f0ad879c1d442a2401c01687d7ea51e64f5e8ca67437d4c591a699604af0adca695761561844527002ae51dd0c5a93217a1c6022c97091761837fb8341a6f85254a29bc2d3e48791a6347701a7743760546530fbe236c9bf90f9994ea428777b065c92dfd1bfa86796f43e1e2a1b47299e52c61620ad4ebe26b9bacec78ca73e66efa9404628a550c29ea59eb9826de5342da7b84bba6bcd50aa0fe267eaa8d113fab76262d4fe9",
      "publicExponent": "0x100010",
      "modulus": "0x00e1de9b838b4b2026b29f03d8fecb916622b25dd89d317d5e79ba2a3e148b2d73278cb1944ba4be4bf87f9ab03f612cb28944bc45086a00a9f87ea489ff0ea866e5d6cf62654065d12967d05836b286d9d55d0fe67faa7b77d8c66346b76b0716946e5a96c64f180e1bc71881534d79eba75582bba448ad648cf93d59c8eeb738ea6bb9a94ffada4ecee846b3aa666ba0fb68c10b39ed65aa067046e970cf19d2a92b787643e54ce09e1c7459475aa6d4b89eb5032dcf7b8b80833f12a5c86cce46f3d2583feccc6243653f75c0412499a2edafddad31f70811bcf81343a49933c992d25efc1e522220ea9da6c5cf80d9ed63ff5c21a1cc7fb537b414e6708957"
    }
    

    从pem文件中检索privateExponent、public Exponent和Modular的信息,其中密钥由openssl创建:

    openssl genrsa 2048>钥匙pem公司

    openssl rsa-文本;钥匙pem公司

    因此,首先我删除起始“0x”,因为十六进制字符串在Java中存储时没有0x。然后,我想把它们转换成一个字节数组,并从中生成一个键。这是我的代码:

    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import javax.xml.bind.DatatypeConverter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    
    
    
    /* Json Fomrats:
    key.json:
    privateExponent: hex-String,
    public Exponent: hex-String,
    modulus: hex-String
    
    
    payload.json:
    message: String,
    sig {
        modulus: hex String,
        publicExponent: hex String,
        signature: String
    }
    
     */
    
    public class Main {
    
        public static String toHexString(byte[] array) {
            return DatatypeConverter.printHexBinary(array);
        }
    
        public static byte[] toByteArray(String s) {
            return DatatypeConverter.parseHexBinary(s);
        }
    
        public static void main(String[] args) {
    
    
            JSONParser parser = new JSONParser();
            try {
    
                Object obj = parser.parse(new FileReader(new File((System.getProperty("user.dir") + "\\src\\com\\company\\key.json"))));
                JSONObject jObj = (JSONObject) obj;
                System.out.println(jObj.get("privateExponent").toString());
                String privKeyS = jObj.get("privateExponent").toString().replace("0x", "");
                System.out.println(privKeyS);
    
    
                byte[] privKeyBytes = toByteArray(privKeyS);
                System.out.println(privKeyBytes);
                PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PrivateKey key = keyFactory.generatePrivate(privKeySpec);
    
    
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
    
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    运行该问题时,我收到以下错误消息:

    java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
        at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
        at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
        at com.company.Main.main(Main.java:70)
    Caused by: java.security.InvalidKeyException: invalid key format
    

    为什么呢?我不明白为什么这种格式不起作用。

    1 回复  |  直到 7 年前
        1
  •  2
  •   pedrofb    7 年前

    您试图加载PKCS#8编码的密钥,但需要从privateExponent和Modular创建密钥

    //Convert hex strings to BigInteger
    BigInteger privateExponent = new BigInteger(privateExponentHex, 16);
    BigInteger modulus = new BigInteger(modulusHex, 16);
    
    //Build the private key
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(modulus, privateExponent);
    PrivateKey privateKey = keyFactory.generatePrivate(privateSpec);