Bobby
Bobby

Reputation: 5

Decryption Exception

I am trying to generate a shared key from Alice and Bob. I used a key generator to get public and private keys for Bob and Alice. Then, I generated a secret key using AES. What I am supposed to do is encrypt the secret key then decrypt it, and it should be the same as the original secret key. However, my code is giving me an exception that says "decryption error". And I cannot figure out why. Can anyone help me? Thank you!

Also, decodeBytes and generateSharedKey are supposed to be equal and I was having trouble with that as well.

import java.security.*;
import java.util.UUID;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class rsa {
    static SecretKey sharedKey = null;
    public static void main(String[] args) throws NoSuchAlgorithmException, 
    NoSuchProviderException, InvalidKeyException, NoSuchPaddingException, 
    IllegalBlockSizeException, BadPaddingException {


        //Generates Key Pair -> a private and public key for both Alice and 
         Bob
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);

        //Alice's key pair
        KeyPair aliceKey = keyGen.genKeyPair();
        PublicKey pubAlice = aliceKey.getPublic(); //alice's public key
        PrivateKey privAlice = aliceKey.getPrivate();//alice's private key

        //Bob's key pair
        KeyPair bobKey = keyGen.genKeyPair();
        PublicKey pubBob = bobKey.getPublic(); //bob's public key
        PrivateKey privBob = bobKey.getPrivate();// bob's private key

        //Generates a random key and encrypt with Bob's public Key
        System.out.println(generateSharedKey());

        byte[] keyEncrypted = encrypt(sharedKey, pubBob);

        byte[] decodeBytes = decrypt(sharedKey, privBob);
        System.out.println(decodeBytes);
        }

    public static SecretKey generateSharedKey() throws 
    NoSuchAlgorithmException{ 
        KeyGenerator sharedKeyGen = KeyGenerator.getInstance("AES"); 
        sharedKey = sharedKeyGen.generateKey();
        return sharedKey;
    }

    public static byte[] encrypt(SecretKey sharedKey, PublicKey pubKey) 
    throws NoSuchAlgorithmException, NoSuchPaddingException, 
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        //System.out.println(inputBytes);

        return cipher.doFinal(generateSharedKey().getEncoded());   
    }

    public static byte[] decrypt(SecretKey sharedKey, PrivateKey privKey) 
  throws NoSuchAlgorithmException, NoSuchPaddingException, 
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        return cipher.doFinal(generateSharedKey().getEncoded());

    }
}

Upvotes: 0

Views: 1028

Answers (1)

Luke Joshua Park
Luke Joshua Park

Reputation: 9806

Please carefully read your code before posting... You are generating a new shared key and then trying to decrypt that in your decrypt method...

You should be passing in a byte[] to decrypt and returning a SecretKey, not the other way around.

Upvotes: 1

Related Questions