probs with cipher
Hi
I created a keystore that generated a DSA pub/Pvt key of keysize1024.
I then retreived the public key for encryption using the the following code
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
if(type.equals("ENCRYPT"))
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
else
cipher.init(Cipher.DECRYPT_MODE,secretKey);
int cipherlength = cipher.getOutputSize(data.length);
cryptedCipherText=newbyte[cipherlength];
cryptedCipherText = cipher.doFinal(data);
This ciphey uses DES algorithm and when i give my DSA public key i get the foolowing error
java.security.InvalidKeyException: No installed provider supportsthis key: sun.security.provider.DSAPublicKey
at javax.crypto.Cipher.a(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
at com.security.cert.KeyStre.encryptDecrypt(KeyStre.java:120)
at com.security.cert.KeyStre.main(KeyStre.java:66)
com.security.cert.EncryptDecryptException: Invalid Key in encrypt/decrypt
at com.security.cert.KeyStre.encryptDecrypt(KeyStre.java:131)
at com.security.cert.KeyStre.main(KeyStre.java:66)
Ok fine but the keytool doesn't support DES algorithm.
keytool -genkey -alias switch -keyalg DES -sigalg DES -keypass <pass> -storepass <pass> -keystore dd.keystore
gives No such algorithm exception
Could anyone help me around this problem
thnx a lot
[1759 byte] By [
--jubs--a] at [2007-10-2 1:22:52]

First, I'm not sure what you're code fragment is trying to do because I don't know where secretKey comes from.
Second, you might be confusing symmetric keys with asymmetric keys.
Third, the DSA is purely a signature algorithm. The Java classes do not support its use for encryption. You can both sign and encrypt with the RSA algorithm, though typically you would only sign hashes and encrypt other keys.
Unless you are using something other than Sun's keystore implementation, the keytool keyalg and sigalg parameters must specify an aymmetric algorithm: either RSA or DSA.
thks ghstark for the insight.
Ichanged my algorithm to RSA and it works fine... Do u know any site that specifies which algorithm to use in which instance
Here is the code by the way...
KeyStore keystore = KeyStore.getInstance("jks");
char[] password ="******".toCharArray();
keystore.load(new FileInputStream("switch.keystore"),password);
Enumeration<String> enumerate =keystore.aliases();
while(enumerate.hasMoreElements())
System.out.println("aliass -->"+ enumerate.nextElement());
Certificate cert = keystore.getCertificate("switch");
PublicKey key = cert.getPublicKey();
System.out.println("public key-->" + new String(key.getEncoded()));
byte[] encryptedText = new KeyStre().encryptDecrypt("ENCRYPT","God is love".getBytes(),key);
PrivateKey pvtKey = (PrivateKey)keystore.getKey("switch",password);
byte[] decryptedText = new KeyStre().encryptDecrypt("DECRYPT",encryptedText,pvtKey);
System.out.println("data-->" + new String(decryptedText));
and
public byte[] encryptDecrypt(String type,byte[] data,Key secretKey)
{
byte cryptedCipherText[] = null ;
BufferedReader read;
try {
System.out.println("Key-->" + new String(secretKey.getEncoded()));
Cipher cipher = Cipher.getInstance("RSA");
if(type.equals("ENCRYPT"))
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
else
cipher.init(Cipher.DECRYPT_MODE,secretKey);
int cipherlength = cipher.getOutputSize(data.length);
cryptedCipherText= new byte[cipherlength];
cryptedCipherText = cipher.doFinal(data);
}