Double encryption decryption (Blowfish+Base64) in java
Double Encryption and decryption using Blowfish and Base64
--------------------------------------------------------------
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class BlowfishCipher {
public static void main(String[] args) throws Exception {
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey = keygenerator.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
// get the text to encrypt
String inputText = "User_ID=Test12&User_Timestamp=20121030040002";
System.out.println("Double Encryption..................for :"+inputText);
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
byte[] firstEncrypt = cipher.doFinal(inputText.getBytes());
System.out.println("blowfish encryption...firstEncrypted::"+new String(firstEncrypt));
byte[] encodedBytes = Base64.encodeBase64(firstEncrypt);
System.out.println("Base64 decoded encryption: encodedBytes::"+new String(encodedBytes));
System.out.println("Double decryption starts now..................");
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("Base64 decryptions........decodedBytes:: " +new String(decodedBytes));
cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretkey);
byte[] decryptedFinal = cipher.doFinal(decodedBytes);
System.out.println("Finally...blowfish decryption....decryptedFinal::"+new String(decryptedFinal));
System.exit(0);
}
}
--------------------------------------------------------------
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class BlowfishCipher {
public static void main(String[] args) throws Exception {
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey = keygenerator.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
// get the text to encrypt
String inputText = "User_ID=Test12&User_Timestamp=20121030040002";
System.out.println("Double Encryption..................for :"+inputText);
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
byte[] firstEncrypt = cipher.doFinal(inputText.getBytes());
System.out.println("blowfish encryption...firstEncrypted::"+new String(firstEncrypt));
byte[] encodedBytes = Base64.encodeBase64(firstEncrypt);
System.out.println("Base64 decoded encryption: encodedBytes::"+new String(encodedBytes));
System.out.println("Double decryption starts now..................");
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("Base64 decryptions........decodedBytes:: " +new String(decodedBytes));
cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretkey);
byte[] decryptedFinal = cipher.doFinal(decodedBytes);
System.out.println("Finally...blowfish decryption....decryptedFinal::"+new String(decryptedFinal));
System.exit(0);
}
}
Comments
Post a Comment