Cipher Encryption Decryption probelm due to private key.
843811Aug 21 2007 — edited Aug 27 2007I have cipher encryption and decrytion problem in using certificate private key
but at the same time i am able to sign the document using same certificate private key.
can any one guide me what is problem.
import javax.security.cert.X509Certificate;
import javax.security.cert.Certificate;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import static java.lang.System.*;
public class EncryptDecrypt
{
public EncryptDecrypt()
{
out.println("EncryptDecrypt intilize..");
}
public static void main(String[]args)
{
EncryptDecrypt ed=new EncryptDecrypt();
ed.pp();
}
public void pp()
{
String message = null;
byte[] messageBytes=null;
byte [] tempPub = null;
//String sPub = null;
byte[] encKey=null;
byte[] ciphertextBytes = null;
byte[] textBytes = null;
KeyStore store=null;
java.security.cert.Certificate cert=null;
try
{
// Obtain a RSA Cipher Object
Cipher cipher = Cipher.getInstance("RSA");
//Cipher cipher = Cipher.getInstance("RSA");
String alias = "My Certificate";
store = KeyStore.getInstance("Windows-MY");
store.load(null,null);
cert = store.getCertificate(alias);
PublicKey pubkey=(PublicKey)cert.getPublicKey();
// Set plain message
message = "This is my secret message.";
messageBytes = message.getBytes();
System.out.println("Plain message:\n" + message + "\n" );
// Initialize the cipher for encryption
//cipher.init(Cipher.ENCRYPT_MODE, pubkey, secureRandom);
cipher.init(Cipher.ENCRYPT_MODE, pubkey);
// Encrypt the message
Key recvKey=store.getKey(alias, null);
PrivateKey privKey = (PrivateKey) recvKey;
//RSAPrivateKey privKey= (RSAPrivateKey) recvPrivateKey;
ciphertextBytes = cipher.doFinal(messageBytes);
System.out.println("Message encrypted with certificate file public key:\n" + new String(ciphertextBytes) + "\n");
// Initialize the cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, privKey);
// Decrypt the message
textBytes = cipher.doFinal(ciphertextBytes);
System.out.println("Message decrypted with file private key:\n" + new String(textBytes) + "\n");
}catch(Exception ex)
{
System.out.println("Exception ex: "+ex);
ex.printStackTrace();
}
}
}
///Exception here
EncryptDecrypt intilize..
Plain message:
This is my secret message.
This is encryted string
)�└Fk�!┤╬☺f�]♦mr:6╖G&?
Ω1╡πe╨╡#��╥N─⌠-X╡W?�↓█╙≤g��τlp≈�D▐∩�f░?╕�╞▲│⌂♦O� M‼��*%⌐o▐Θ>╗$f�╩╣┴��?►(ⁿφ‼e6.�░
╓◄,�Φ���∩�qCiδ◄∟Fg6�K
This is exception i am geting here
java.security.InvalidKeyException: Private keys must be instance of RSAPrivate(Crt)Key or have PKCS#8 encoding
java.security.InvalidKeyException: Private keys must be instance of RSAPrivate(Crt)Key or have PKCS#8 encoding
at sun.security.rsa.RSAKeyFactory.translatePrivateKey(Unknown Source)
at sun.security.rsa.RSAKeyFactory.engineTranslateKey(Unknown Source)
at sun.security.rsa.RSAKeyFactory.toRSAKey(Unknown Source)
at com.sun.crypto.provider.RSACipher.engineGetKeySize(DashoA13*..)
at javax.crypto.Cipher.b(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at EncryptDecrypt.pp(EncryptDecrypt.java:78)
at EncryptDecrypt.main(EncryptDecrypt.java:28)