I am trying to decrypt this certificate. I zip it before encrypting it since it is big. For decrypting, I am decrypting then unzip it. I would really appreciate any help.
This is the output:
[B@3ac42916
[B@5cad8086 while the output should be the certification string
package test11;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class Sample {
public static void main(String [] args) throws Exception {
// generate public and private keys
KeyPair keyPair = buildKeyPair();
PublicKey pubKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
GzipUtil zipp = new GzipUtil();
// encrypt the message
String hour = "00";
String certificate="1"+","+"0336"+","+"RSA"+","+"CA 1552"+","+hour+","+pubKey+","+"RSA";
byte [] cert = GzipUtil.zip(certificate) ;
byte [] encrypted = encrypt(privateKey, cert.toString());
System.out.println(encrypted); // <<encrypted message>>
// decrypt the message
byte[] secret = decrypt(pubKey, encrypted);
String text= GzipUtil.unzip(secret);
System.out.println(text); // This is a secret message
}
public static KeyPair buildKeyPair() throws NoSuchAlgorithmException {
final int keySize = 2048;
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keySize);
return keyPairGenerator.genKeyPair();
}
public static byte[] encrypt(PrivateKey privateKey, String message) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(message.getBytes());
}
public static byte[] decrypt(PublicKey publicKey, byte [] encrypted) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(encrypted);
}
}