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.

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;
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(2 votes, average: 5.00 out of 5)
 Loading ...

Pet Porte Microchip Smart Flap

Technology has evolved in a lot of ways and a lot of things. One of them is a 2nd generation microcip cat flap. Where cat claps of before were not as effective at keeping other uninvited guests in your home , this second generation cat flap does just that. All the while keeping your cat safe and giving it a broader sense of freedom, security and easiness. The Pet Porte Smart flap can keep other cats out of your home whilst letting only your cat in and out anytime. How is this possible? With a microchip implanted in your pet cat, it can easily pass through the cat flap anytime and without problems. The size of cat flaps also vary depending on the breed of cat that you own. Pet Porte has cat flaps for large cats like Main Coon and Norwegian Forest cats.. Hey, it can even be used by little dogs and puppies. The Pet Porte microship cat flap’s unique design makes it impossible from outside intrudes to get the door open. Its material does not consist of rubber or foam which can enable it to be pulled towards any intruding animal. The best news of all. They deliver to any part in the world. Take advantage of this great revolutionary technology and let your cat(s) enjoy the freedom and security that they deserve.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

Convert Hexadecimal To Binary Using Java

Here is a useful method to convert a hexadecimal to binary using Java. If you may wonder why the HEX_String object contains 0 to 9 and A to F, it is because a hexadecimal or base-16 notation uses 16 different digits: 0 up to 9 and then the letters A, B, C, D, E, F to represent 10, 11, 12, 13, 14 and 15.

String HEX_STRING  = "0123456789ABCDEF";
 
public static byte[] convertHexadecimal2Binary(byte[] hex) {
  int block = 0;
  byte[] data = new byte[hex.length / 2];
  int index = 0;
  boolean next = false;
 
  for (int i=0; i<hex.length; i++) {
    block <<= 4;
    int pos = HEX_STRING.indexOf(Character.toUpperCase((char) hex[i]));
    if (pos > -1) block += pos;
 
    if (next) {
      data[index] = (byte)(block & 0xff );
      index++;
      next = false;
    } else next = true;
  }
 
  return data;
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(2 votes, average: 5.00 out of 5)
 Loading ...

Convert Binary To Hexadecimal Using Java

Here is a useful method to convert binary to hexadecimal using Java.

String HEX_STRING  = "0123456789ABCDEF";
 
private static String convertBinary2Hexadecimal(byte[] binary) {
  byte[] data = new byte[binary.length * 2];
  StringBuffer buf = new StringBuffer();
  boolean second = false;
  int block = 0;
 
  for (int i=0; i<binary.length; i++) {
    block = binary[i] & 0xFF;
    buf.append(HEX_STRING.charAt(block >> 4));
    buf.append(HEX_STRING.charAt(binary[i] & 0x0F));
  }
 
  return buf.toString();
}

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(2 votes, average: 5.00 out of 5)
 Loading ...