Reputation:
I have the code:
String Password = PasswordText.getText();
SecretKeySpec SKC = new SecretKeySpec(Password.getBytes(), "DES");
PBEKeySpec PKS = new PBEKeySpec(Password.toCharArray());
SecretKeyFactory SKF = null;
try{
SKF = SecretKeyFactory.getInstance("DES");
} catch(NoSuchAlgorithmException AlgorithmFail) {
return;
}
SecretKey CipherKey = null;
try{
CipherKey = SKF.generateSecret(SKC);
} catch(InvalidKeySpecException KeyFail) {
return;
}
In the last statement I cause the failure: "Inappropriate key specification". That failure caused also I using in the last statement SKC instead of PKS. Code example I using I give from the Internet. Please kick me to the my mistake. In all examples, but statement does work at all:
SecretKey MyKey = SecretKeyFactory.getInstance("DES")
.generateSecret(new PBEKeySpec(Password.toCharArray()));
Can be help me? PLEASE!!!
Upvotes: 0
Views: 198
Reputation: 41997
You cannot just mix and match these keyspecs any way you want. The docs are unfortunately very confusing and unclear on how to do this. The Sun provider's SecretKeyFactory seems to require that DES keys be specified by a DESKeySpec instance: DESKeySpec SKC = new DESKeySpec(Password.getBytes());
Unfortunately I do not understand the remainder of your question.
Upvotes: 1
Reputation: 1783
You can add Bouncy Castle as a provider. Then add:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
to the beginning of your example. And use:
secretKeyFactory = SecretKeyFactory.getInstance("DES", "BC");
You'll need to make your project depend on the bcprov
jar (probably bcprov-jdk15on-147.jar
). Download from here.
Upvotes: 0
Reputation: 7311
Take a look to this tutorial that works fine with DES encryption
http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html
Upvotes: 1