dcsimg
Encryption of the password using your own key
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sachin_thukral
Posted On:   Wednesday, September 17, 2003 01:17 AM

I want to Encrypt the password field.Also,I want to use my Key instead of creating the key again and again. Also,I'm using DESede algorithm for this purpose. I have pasted down the code. public static String setEncryptedPassword(String password){ byte[] encryptedPwd = null; try { FileInputStream keyfis = new FileInputStream("C:\check.txt"); byte[] encKey = new byte[keyfis.available()]; keyfis.read(encKey); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("DESede"); PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Cipher objCipher = Cipher.getIn   More>>

I want to Encrypt the password field.Also,I want to use my Key instead of creating the key again and again.

Also,I'm using DESede algorithm for this purpose.

I have pasted down the code.

public static String setEncryptedPassword(String password){

byte[] encryptedPwd = null;
try
{
FileInputStream keyfis = new FileInputStream("C:\check.txt");
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey);

X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
KeyFactory keyFactory = KeyFactory.getInstance("DESede");
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

Cipher objCipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
objCipher.init(Cipher.ENCRYPT_MODE,pubKey);
encryptedPwd = objCipher.doFinal(password.getBytes());

keyfis.close();
}
catch(Exception e){
}
return encryptedPwd.toString();

}

But I'm getting Exception when a new Instance is being created for the KeyFactory,saying Exception:java.security.NoSuchAlgorithmException: DESEDE KeyFactory not available

My Objective is to encrypt a password which is input to me with the key which I need to read it from the File.
If my approach is wrong,then please let me know.

If anybody can see the approach,it'll really a great help.

   <<Less

Re: Encryption of the password using your own key

Posted By:   pankaj_kumar  
Posted On:   Saturday, September 20, 2003 02:49 PM

Unfortunately, you cannot use KeyFactory with "DESede" algorithm as J2SE v1.4.x SDK supports KeyFactory only for "DSA", "RSA" and "DiffieHellman" algorithms.



The Javadoc documentation is not very clear on what algorithms are supported for a particular Cryptographic Service (KeyFactory is a cryptographic service). What is clear, though, based on method names and their semantics, is that it supports only public-private key cryptography. whereas, "DESede" is a secret key encryption algorithm.



You can use your own secret key by serialising the key, writing it into file and then later on, reading this file to populate the Key object. However, this method sufferes from a limitation: the serailized key can be used only by Java programs. If you want it to be language independent then you can store the encoded key, which you get by calling getEncoded() method.



Pankaj Kumar.


Home  WebLog  J2EE Security
About | Sitemap | Contact