如何将Java OpenSSLRSAPrivateCrtKey转换为PKCS1格式

时间:2018-12-06 04:50:34

标签: java encryption private-key pkcs#8 pkcs#1

CipherText

i5SvmG2TbtZfkWdwJ5qeaYzvLlQknY3uMvZxSEwhBdRcXKHjgzrRk6XLDCEG9ZtZDGDA7iB3tFhLPMisfqGZvSSrcBfiV8b71+qzWVDNW9EedVShk6kaeEN6rw4UgVi6P5PvrDMn6pmYmLWCjtuFWrmboCvvYgI+FJurhlbsQESkA5oDYirjS8L0wnsQB/TnnQ5UPY2xfOBdY2MJpUSTyIjJPhI40GST8YWjXEMkJeDV/1zuKuK55RHCDF5AdTMEvgvvRnGhN2Fzh+rsDziHqVS9d8FmrtjdU445F6ki0d8DkaeFfrofptxGIncqfuukKSXpSp4cPLvM3LxtRvp+Aw==

要解密的代码

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(2048);
KeyPair kp2 = kpg.generateKeyPair();
            generatedPub = kp2.getPublic();
            generatedPvt = kp2.getPrivate();

public String rsaDecrypt(String encrypted) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, generatedPvt);
            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted.getBytes()));
            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

错误

  

javax.crypto.BadPaddingException:错误:04000089:RSA例程:OPENSSL_internal:PKCS_DECODING_ERROR

此外,如果我尝试将私钥打印为:

generatedPvt.toString()

我得到以下信息:

OpenSSLRSAPrivateCrtKey{modulus=c7544bf521bbdd7db52dd28bd3c6f694214dc2356b905edd2730b631d11be9aea703692c2db690e6725da65737b5ec511c13668d1735bfbbc2519e0d33a67b41b289bae6ea71903af91e4f12c6e8660614ef12cd439293a0a38f564fd8f19a3e38f9e2defa269d0bcf0f53159bba1b4fd539ad934fb691e860113be53901de5a10d0c0e3ceaec3715841bc6e56b7738336e8df95a989b61175b06d70d349dcb4a031acf5b25647a1d77f6e6e11efc66e98bb321430f148a63c103a0a59e94b147a4fb49a9ecb0b23603a6ceed6e6e298650667cd61de71455bd4f95767444d89bcf485cc47a297b5306a60a14f77a3fbc19552c5fddfa5cfa2e68020e245ad91,publicExponent=10001}

我期望打印PKCS#8格式化的密钥。如何获得以PKCS#8格式打印的密钥?

为什么我会收到如上所示的填充错误?

1 个答案:

答案 0 :(得分:-2)

byte[] original = cipher.doFinal(encrypted.getBytes());

从加密和解密方法中删除Base64.decodeBase64

如果要将密钥转换为字符串,请使用

String keyString= Base64.encodeBase64String(key.getEncoded());

RSA实施

 public static void main(String[] args) {
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp2 = kpg.generateKeyPair();
        PublicKey publicKey = kp2.getPublic();
        PrivateKey privateKey = kp2.getPrivate();
        NewClass nc = new NewClass();
        byte[] encrypt=nc.rsaEncrypt("hi",publicKey);
        byte[] decrypt=nc.rsaDecrypt(encrypt,privateKey);
        String decryptString = new String(decrypt);
        System.out.println("decryptString = " + decryptString);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public byte[] rsaDecrypt(byte[] encrypted,PrivateKey privateKey) {
    try {

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] original = cipher.doFinal(encrypted);
        return original;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public byte[] rsaEncrypt(String message,PublicKey publicKey) {
    try {

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] original = cipher.doFinal(message.getBytes());
        return original;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

在完成编码和解码以及加密和解密时,顺序必须是

加密

cipher.doFinal(Base64.encodeBase64(message.getBytes()));

解密

Base64.decodeBase64(cipher.doFinal(cipher.getBytes()));