Encrypt And Decrypt Using Blowfish In Java


Java Cryptography classes offer a variety of ways for you to encrypt and decrypt. One of them is using Blowfish. The methods below encrypt and decrypt Strings using a String key as its secret key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static String encryptBlowfish(String to_encrypt, String strkey) {
  try {
    SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.ENCRYPT_MODE, key);
     return new String(cipher.doFinal(to_encrypt.getBytes()));
  } catch (Exception e) { return null; }
}
 
public static String decryptBlowfish(String to_decrypt, String strkey) {
  try {
     SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.DECRYPT_MODE, key);
     byte[] decrypted = cipher.doFinal(to_decrypt.getBytes());
     return new String(decrypted);
  } catch (Exception e) { return null; }
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Encrypt Using Blowflish And Java


Blowfish is a symmetric block cipher that can be used as a drop-in replacement for DES or IDEA. It takes a variable-length key, from 32 bits to 448 bits, making it an ideal way for encryption purposes. Java supports Blowflish encryption in its API. Here is a method that will encrypt using Blowfish. Part of the code converts the string to be encrypted to byte[] array so this method uses a secondary method called convertBinary2Hexadecimal() which you can find here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static String encrypt(String key, String plain) {
  Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 3);
  byte[] plainText = plain.getBytes();
  String cipher = null;
 
  try {
    SecretKeySpec blowfishKey = new SecretKeySpec(key.getBytes(), "Blowfish");
    Cipher blowfishCipher = Cipher.getInstance("Blowfish", "BC");
    blowfishCipher.init(Cipher.ENCRYPT_MODE, (Key)blowfishKey);
    byte[] cipherText = blowfishCipher.doFinal(plainText);
    cipher = convertBinary2Hexadecimal(cipherText);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return cipher;
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

PHP Encrypt Decrypt using Base64


Here are two methods to encrypt and decrypt using Base64. I forgot where I got this from but these 2 methods are pretty handy. Make sure you remember your key as your string will be encrypted and decrypted according to what you specify as key. Key is a string here.

Usage is as follows:
$encrypted = encrypt("to encrypt string", "chitgoks");
$decrypted = decrypt($encrypted, "chitgoks");

$decrypted will return to encrypt string.

function encrypt($string, $key) {
  $result = '';
  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)+ord($keychar));
    $result.=$char;
  }

  return base64_encode($result);
}

function decrypt($string, $key) {
  $result = '';
  $string = base64_decode($string);

  for($i=0; $i<strlen($string); $i++) {
    $char = substr($string, $i, 1);
    $keychar = substr($key, ($i % strlen($key))-1, 1);
    $char = chr(ord($char)-ord($keychar));
    $result.=$char;
  }

  return $result;
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails