Created
March 17, 2011 14:37
-
-
Save scotttam/874426 to your computer and use it in GitHub Desktop.
encrypt and decrypt with PBKDF2/SHA1 and AES
This file contains hidden or 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
import javax.crypto.Cipher; | |
import java.security.spec.KeySpec; | |
import javax.crypto.spec.PBEKeySpec; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.crypto.SecretKeyFactory; | |
import java.security.AlgorithmParameters; | |
import javax.crypto.spec.IvParameterSpec; | |
public class Decrypter { | |
Cipher dcipher; | |
byte[] salt = new String("12345678").getBytes(); | |
int iterationCount = 1024; | |
int keyStrength = 256; | |
SecretKey key; | |
byte[] iv; | |
Decrypter(String passPhrase) throws Exception { | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, keyStrength); | |
SecretKey tmp = factory.generateSecret(spec); | |
key = new SecretKeySpec(tmp.getEncoded(), "AES"); | |
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
} | |
public String encrypt(String data) throws Exception { | |
dcipher.init(Cipher.ENCRYPT_MODE, key); | |
AlgorithmParameters params = dcipher.getParameters(); | |
iv = params.getParameterSpec(IvParameterSpec.class).getIV(); | |
byte[] utf8EncryptedData = dcipher.doFinal(data.getBytes()); | |
String base64EncryptedData = new sun.misc.BASE64Encoder().encodeBuffer(utf8EncryptedData); | |
System.out.println("IV " + new sun.misc.BASE64Encoder().encodeBuffer(iv)); | |
System.out.println("Encrypted Data " + base64EncryptedData); | |
return base64EncryptedData; | |
} | |
public String decrypt(String base64EncryptedData) throws Exception { | |
dcipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); | |
byte[] decryptedData = new sun.misc.BASE64Decoder().decodeBuffer(base64EncryptedData); | |
byte[] utf8 = dcipher.doFinal(decryptedData); | |
return new String(utf8, "UTF8"); | |
} | |
public static void main(String args[]) throws Exception { | |
Decrypter decrypter = new Decrypter("ABCDEFGHIJKL"); | |
String encrypted = decrypter.encrypt("the quick brown fox jumps over the lazy dog"); | |
String decrypted = decrypter.decrypt(encrypted); | |
System.out.println(decrypted); | |
} | |
} |
In a scenario where decrypt
is called first (no call to encrypt
) , iv
is null and there is a null pointer exception. I encountered this after reading encrypted data from Firebase and trying to decrypt.
@Yair0007 I faced this too. Did you manage to find the solution ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I Have an Encrypted String, how can i decrypt it
String = "p1SvSCiAxupKrrZXzjXQk3lWe88xW7mTymvqGlmAOMM9DbYm3kkq7BLP336HwRdYOIBU9VNL0tFTchwBKbw+ZOlU6Ny3vTOtA3/hPqSUOOXDEu5aKnbbvywefu3jyX1u/EFbc1uqohJspem+d1kwxmzn
tbvui8AH6+mckIb6I9c="