Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.PBEKeySpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.security.AlgorithmParameters;
- import java.security.spec.KeySpec;
- public class AESTest {
- public static void main(String[] args) throws Exception {
- String textToEncrypt = "SecretPhrase";
- // hexadecimal of "SecretPhrase" generated by crypto
- String hexGeneratedInCrypto = "a39e724d866461b97d0982d6ef72e567";
- byte[] salt = null; // I don't know tha salt because in crypto it's calculated automatically
- char[] password = "mypassword".toCharArray();
- SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
- SecretKey tmp = factory.generateSecret(spec);
- SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, secret);
- AlgorithmParameters params = cipher.getParameters();
- byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
- byte[] encrypted = cipher.doFinal(textToEncrypt.getBytes("UTF-8"));
- // I need hex(encrypted) == hexGeneratedInCrypto
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement