• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to encrypt & decrypt Random DES key with DES?

 
lalitha aa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my requirement,

i have constant string say "PASS123"
I need to
- Generate Random DES Key
- Generate a secret key using PASS123
- Encrypt the Random DES key using the secret key generated in second step.

On the decryption side what are the exact steps i need to follow to get the Random DES key that was generated from the client. I would have the secret key that is generated using "PASS123".

Please provide any helpful tips on this.

I was able to acheive the encryption steps mentioned above. But unable to decrypt!

Links i have referred
=============
http://www.java2s.com/Code/Java/Security/EncryptingwithDESUsingaPassPhrase.htm
http://sangchin.byus.net/JavaHelps/Javax_crypto/PassKey.html
http://download.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html


Source code
=========
import java.security.SecureRandom;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


public class DESEnc {
private static final String UNICODE_FORMAT = "UTF8";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private KeySpec myKeySpec;
private SecretKeyFactory mySecretKeyFactory;
private Cipher cipher;



SecretKey key, randomDESKey;


DESEnc(){
}


byte[] encrypt () throws Exception {
byte[] keyAsBytes;
String myEncryptionKey;
//generate a key with Password
myEncryptionKey = "Some 16 bit Key";
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec = new DESKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(DES_ENCRYPTION_SCHEME);
cipher = Cipher.getInstance(DES_ENCRYPTION_SCHEME);
key = mySecretKeyFactory.generateSecret(myKeySpec);


System.out.println("Secret key" + new String(key.getEncoded()));


//generate Random DES Key
KeyGenerator generator;
generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
randomDESKey = generator.generateKey();
System.out.println("RDK " + new String(randomDESKey.getEncoded()));


Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,randomDESKey);
byte[] encryptedText = cipher.doFinal(key.getEncoded());
System.out.println("Encrypted " + new String(encryptedText));
return encryptedText;


}


void decrypt(byte[] encStr) throws Exception {
cipher = Cipher.getInstance(DES_ENCRYPTION_SCHEME);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decText = cipher.doFinal(encStr);
System.out.println("Decrypted Text " + new String(decText));
}


public static void main(String[] args) throws Exception {
DESEnc enc = new DESEnc();
enc.decrypt(enc.encrypt());
}

}




 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you are using this approach but you are encrypting with the random key and then decrypting with the key generated from the password! You must must must use the same key for decryption as encryption.

P.S. Why are you not using Password Based Encryption (PBE)?
PP.S. A DES key is 56 bits normally packaged as 8 bytes with the least significant bit of each byte being a parity bit. This means that your code

is flawed since the key is neither 16 bits or even 16 bytes.

 
lalitha aa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply.

the requirement I have is not to use PBE, i have checked this option

I will change the key to 56 bits. and any tips on getting the decryption right will be of great help to me - Thanks
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lalitha anand wrote:Thank you for the reply.

the requirement I have is not to use PBE, i have checked this option

I will change the key to 56 bits. and any tips on getting the decryption right will be of great help to me - Thanks


But that will still leave you using the wrong key for decryption! Your code seems to me to be nonsense and it is not obvious to me what you are trying to do so I can't help any further. Bye.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic