Last active
May 17, 2018 19:46
-
-
Save ilshad/d5e9a43bc7dbad9ff3208ac153d1105c to your computer and use it in GitHub Desktop.
Encrypt / decrypt URL with Blowfish + Base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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