简体   繁体   中英

Java public private key decryption issue

I am trying to encrypt and decrypt a message as mentioned in the below code. Basically I want to encrypt a message with a public key and convert that encrypted message from byte array to String. And decrypt this string into original text. Here are the both methods. Here encryption works fine but decryption fails (error is "Data must start with zero"). I think this is causing because I convert encrypted byte array into String.

How do I solve this? (I want to have encrypted byte array as string and use it for decryption) Is there any other approach (with public and private keys)

public static String getEncryptedMessage(String publicKeyFilePath,

    String plainMessage) {
    byte[] encryptedBytes;
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        byte[] publicKeyContentsAsByteArray = getBytesFromFile(publicKeyFilePath);
        PublicKey publicKey = getPublicKey(publicKeyContentsAsByteArray);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        encryptedBytes = cipher.doFinal(plainMessage.getBytes());
        return new String(encryptedBytes);
    } catch (Throwable t) {

    }

}
public static String getDecryptedMessage(
        String privateKeyFilePath, String encryptedMessage)
         {
    byte[] decryptedMessage;
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        byte[] privateKeyContentsAsByteArray = getBytesFromFile(privateKeyFilePath);
        PrivateKey privateKey = getPrivateKey(privateKeyContentsAsByteArray);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        decryptedMessage = cipher.doFinal(encryptedMessage.getBytes());
        return new String(decryptedMessage);
    } catch (Throwable t) {


}

If you look at this page ( http://www.wikijava.org/wiki/Secret_Key_Cryptography_Tutorial ) you will need to do base-64 encoding to turn the bytes into a string, then to decrypt it you would just decode it then decrypt.

Base-64 encoding uses the first 7 bits of a byte, to make something that is printable or emailable, for example.

UPDATE:

I made a mistake, there are 64 characters that it would be encoded in, again, in order to make it easier to use as something printable.

Why don't you treat the message as byte array from encryption to decryption? Why changing it to String in the middle? (I know it seems like a question, but it's actually an answer...)

Using RSA directly on unformatted data may leave your application vulnerable to an adaptive chosen ciphertext attack . For details please see Chapter 8, pages 288-289, of the Handbook of Applied Cryptography , a freely-available book from CRC Press. (It's well worth buying the bound edition , if you're really interested in cryptography -- you'll be stunned at the quality for the price.)

Because of this attack, most protocols that integrate RSA use RSA for encrypting randomly-generated session keys or signing hash functions with outputs that ought to be indistinguishable from random, OR using very carefully formatted messages that will fail to be correctly interpreted. (See Note 8.63 in HAC for details.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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