The unique feature of this class is the doEncrypt method of this class returns a byte array which has the same length of encrypted key.And the doDecrypt method will decrypt the resulted values of doEncrypt method into it's clear values.Try this, it's very simple.
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author Sashika Nimantha Perera
*/
public class Crypto {
public static byte[] doEncrypt(byte[] baKey,byte[] baFormattedDataBlock){
byte encrypted[] = new byte[1];
SecretKey key = null;
Cipher c = null;
// for 8 byte long key
if(baKey.length == 8){
// return that Warning in host command manual "02 : Warning PVK not single length"
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DES/ECB/NoPadding");
DESKeySpec ks = new DESKeySpec(baKey);
key = skf.generateSecret(ks);
c.init(Cipher.ENCRYPT_MODE, key);
encrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println(e.toString());
}
// for 16 byte long key
}else if(baKey.length==16){
byte[] bArrayForDouble = new byte[24];
System.arraycopy(baKey,0,bArrayForDouble,0,16);
System.arraycopy(baKey,0,bArrayForDouble,16,8);
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DESede/ECB/NoPadding");
DESedeKeySpec ks = new DESedeKeySpec(bArrayForDouble);
key = skf.generateSecret(ks);
c.init(Cipher.ENCRYPT_MODE, key);
encrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println(e.toString());
}
//for 24 Byte long key
}else if (baKey.length==24){
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DESede/ECB/NoPadding");
DESedeKeySpec ks = new DESedeKeySpec(baKey);
key = skf.generateSecret(ks);
c.init(Cipher.ENCRYPT_MODE, key);
encrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println(e.toString());
}
}
return encrypted;
}
public static byte[] doDecrypt(byte[] baKey, byte[] baFormattedDataBlock){
byte decrypted[] = new byte[1];
SecretKey key = null;
Cipher c = null;
// for 8 byte long key
if(baKey.length == 8){
// return that Warning in host command manual "02 : Warning PVK not single length"
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DES/ECB/NoPadding");
DESKeySpec ks = new DESKeySpec(baKey);
key = skf.generateSecret(ks);
c.init(Cipher.DECRYPT_MODE, key);
decrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println("Exception in De8 " + e.toString());
}
// for 16 byte long key
}else if(baKey.length==16){
byte[] bArrayForDouble = new byte[24];
System.arraycopy(baKey,0,bArrayForDouble,0,16);
System.arraycopy(baKey,0,bArrayForDouble,16,8);
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DESede/ECB/NoPadding");
DESedeKeySpec ks = new DESedeKeySpec(bArrayForDouble);
key = skf.generateSecret(ks);
c.init(Cipher.DECRYPT_MODE, key);
decrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println( e.toString());
}
//for 24 Byte long key
}else if (baKey.length==24){
try{
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
// as parameters we can give only algorithm and the provider
c = Cipher.getInstance("DESede/ECB/NoPadding");
DESedeKeySpec ks = new DESedeKeySpec(baKey);
key = skf.generateSecret(ks);
c.init(Cipher.DECRYPT_MODE, key);
decrypted = c.doFinal(baFormattedDataBlock);
}catch(Exception e){
System.out.println( e.toString());
}
}
return decrypted;
}
}