Skip to content

Instantly share code, notes, and snippets.

@ilshad
Last active May 17, 2018 19:46
Show Gist options
  • Save ilshad/d5e9a43bc7dbad9ff3208ac153d1105c to your computer and use it in GitHub Desktop.
Save ilshad/d5e9a43bc7dbad9ff3208ac153d1105c to your computer and use it in GitHub Desktop.
Encrypt / decrypt URL with Blowfish + Base64
package com.company;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import org.apache.commons.codec.binary.Base64;
/**
*
* D2SDataProtection.encrypt("http://example.com/image.png", "SD88-4Si-oVJ") =>
* "ciK954e/FLZJKuEMFsfynyTSGt/xOP4ubn7rU/uUTTg="
*
*/
public class D2SDataProtection {
private static Cipher blowfish(int mode, String secret) throws GeneralSecurityException {
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(mode, keySpec);
return cipher;
}
public static String encrypt(String url, String secret) throws GeneralSecurityException {
Cipher cipher = blowfish(Cipher.ENCRYPT_MODE, secret);
return new String(Base64.encodeBase64(cipher.doFinal(url.getBytes())));
}
public static String decrypt(String encryptedURL, String secret) throws GeneralSecurityException {
Cipher cipher = blowfish(Cipher.DECRYPT_MODE, secret);
return new String(cipher.doFinal(Base64.decodeBase64(encryptedURL)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment