Skip to content

Instantly share code, notes, and snippets.

@pentium10
Created July 20, 2011 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pentium10/38b43ab0490a0dc9836f to your computer and use it in GitHub Desktop.
Save pentium10/38b43ab0490a0dc9836f to your computer and use it in GitHub Desktop.
class Test {
public static byte[] decrypt(byte[] encrypted, byte[] key)
throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
String iv = "00000000";
IvParameterSpec ivs = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivs);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static byte[] encrypt(byte[] messageBytes, byte[] key)
throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
String iv = "00000000";
IvParameterSpec ivs = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivs);
byte[] encrypted = cipher.doFinal(messageBytes);
return encrypted;
}
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
// encrypting
try {
String c = new String(Test.encrypt((new String("thevalue")).getBytes(),
(new String("mykey")).getBytes()));
System.out.println("Encrypting \"thevalue\": " + c);
System.out.println("Base64 encoded String:"+ new sun.misc.BASE64Encoder().encode(c.getBytes()));
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
}
// Base64 encoded String:wTHzxfxLHdMm/JMFnoh0hciS/D8DvFFg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment