使用带有java生成密钥的RSA在C ++中解密来自Java的字节数组

时间:2015-08-07 13:00:59

标签: java c++ encryption cryptography rsa

我需要用C ++解密文件。我所拥有的是使用Java.security中的KeyPairGenerator在Java中生成的字节数组和密钥对;

Java代码:

public void generateKeys() {
    try {
        final KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance(algorithmName);
        pairGenerator.initialize(1024); //1024 - keysize
        final KeyPair keyPair = pairGenerator.generateKeyPair();

        savePublicKeyIntoFile(keyPair);
        savePrivateKeyIntoFile(keyPair);

    } catch (Exception e) {
        System.err.println("Class EncryptionTool.generateKeys() ");
        e.printStackTrace();
    }
}

public static String encrypt() throws Exception {

    // Encrypt the string using the public key
    ObjectInputStream inputStream = null;
    inputStream = new ObjectInputStream(new FileInputStream(publicKeyFilepath));
    final PublicKey publicKey = (PublicKey) inputStream.readObject();

    // get an RSA cipher object and print the provider
    final Cipher cipher = Cipher.getInstance(algorithmName);
    // encrypt the plain text using the public key
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    byte[] cipherText = null;
    **cipherText = cipher.doFinal( loadPassword() );**

    return changeByteArrayToString(cipherText);
}

我已经生成了保存在文件中的密钥和C ++中的cipherText数组。 我该怎么用来解密这个?

0 个答案:

没有答案