The line:
Cipher c = Cipher.getInstance("DES");
throws a NoSuchAlgorithmException. This line is taken right out of the Java documentation, can anyone tell me what I'm doing wrong?
These are the imports I'm using:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
Would appreciate any help you could offer.
I'm just trying to encrypt a sting.
Thanks!
Problems with Java cryptographic extension?
I used it b4 as below ....just have an idea
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
import java.util.*;
public class EncryptTest {
public static void main(String args[]) {
File desFile = new File("out.des");
// Create data to encrypt
Map map = new TreeMap(System.getProperties());
int number = map.size();
try {
// Create Key
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey secretKey = kg.generateKey();
// Create Cipher
Cipher desCipher =
Cipher.getInstance("DES/ECB/PKCS5Padding...
desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
// Create stream
FileOutputStream fos = new FileOutputStream(desFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
CipherOutputStream cos = new CipherOutputStream(bos,
desCipher);
ObjectOutputStream oos = new ObjectOutputStream(cos);
// Write objects
oos.writeObject(map);
oos.writeInt(number);
oos.flush();
oos.close();
// Change cipher mode
desCipher.init(Cipher.DECRYPT_MODE, secretKey);
// Create stream
FileInputStream fis = new FileInputStream(desFile);
BufferedInputStream bis = new BufferedInputStream(fis);
CipherInputStream cis = new CipherInputStream(bis,
desCipher);
ObjectInputStream ois = new ObjectInputStream(cis);
// Read objects
Map map2 = (Map)ois.readObject();
int number2 = ois.readInt();
ois.close();
// Compare original with what was read back
if (map.equals(map2) %26amp;%26amp; (map.size() == number2)) {
System.out.println("Everything read back out okay.");
} else {
System.out.println("Problems during
encryption/decryption process.");
}
} catch (NoSuchPaddingException e) {
System.err.println("Padding problem: " + e);
} catch (NoSuchAlgorithmException e) {
System.err.println("Invalid algorithm: " + e);
} catch (InvalidKeyException e) {
System.err.println("Invalid key: " + e);
} catch (IOException e) {
System.err.println("I/O Problem: " + e);
} catch (ClassNotFoundException e) {
System.err.println("Class loading Problem: " + e);
} finally {
if (desFile.exists()) {
desFile.delete();
}
}
}
}
Hope this will solve your issues
Reply:Since this little program does not throw an exception:
import javax.crypto.*;
public class myprog {
public static void main(String args[]){
try
{
Cipher c = Cipher.getInstance("DES");
}catch (Exception e){
e.printStackTrace();
}
}
}
you are not wrapping the line in a try-catch.
It will throw an exception if I change DES to ES for example, so DES is valid as I'm sure you're aware.
The 2 exceptions you should be catching are
NoSuchAlgorithmException,
NoSuchPaddingException
http://java.sun.com/j2se/1.4.2/docs/api/...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment