Skip to content

Instantly share code, notes, and snippets.

@xymor
Last active May 25, 2018 19:54
Show Gist options
  • Save xymor/4549291 to your computer and use it in GitHub Desktop.
Save xymor/4549291 to your computer and use it in GitHub Desktop.
Encrypt/decrypt with Blowfish/ECB/NoPadding
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import org.apache.commons.codec.binary.Base64
def encrypt(encrypt,en_key) {
if(encrypt.size() % 8 != 0){ //not a multiple of 8
byte[] padded = new byte[encrypt.length + 8 - (encrypt.length % 8)];
//copy the old array into it
System.arraycopy(encrypt, 0, padded, 0, encrypt.length);
encrypt = padded;
}
def result
try {
SecretKeySpec key = new SecretKeySpec(en_key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
result = cipher.doFinal(encrypt);
} catch (Exception e) {
e.printStackTrace();
result = null;
}
new String(Base64.encodeBase64(result))
}
def decrypt(encrypt,en_key) {
encrypt = Base64.decodeBase64(encrypt)
try {
SecretKeySpec key = new SecretKeySpec(en_key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key);
result = cipher.doFinal(encrypt);
} catch (Exception e) {
e.printStackTrace();
result = null;
}
new String(result)
}
def encd = encrypt("4444333322221111" as byte[],"12345" as byte[])
println "4444333322221111"
println encd // 2D7maQOJ8FtfUCJTrHnqyQ==
def ded = decrypt(encd,"12345" as byte[])
println ded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment