Advertisement
Guest User

Untitled

a guest
Apr 8th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1.     public AES(final char[] password, final byte[] salt, final byte[] iv)
  2.             throws NoSuchAlgorithmException, InvalidKeySpecException,
  3.             NoSuchPaddingException, InvalidKeyException,
  4.             InvalidParameterSpecException, IllegalBlockSizeException,
  5.             BadPaddingException, UnsupportedEncodingException,
  6.             InvalidAlgorithmParameterException {
  7.  
  8.         final AlgorithmParameterSpec aps = new IvParameterSpec(iv);
  9.  
  10.         // Derive the key, given password and salt
  11.         final SecretKeyFactory factory = SecretKeyFactory
  12.                 .getInstance(SECRET_KEY_ALGORITHM);
  13.         final KeySpec spec = new PBEKeySpec(password, salt, ITERATIONS,
  14.                 KEY_LENGTH);
  15.         final SecretKey tmp = factory.generateSecret(spec);
  16.         final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
  17.  
  18.         // Build encryptor and get IV
  19.         final Cipher enc_cipher = Cipher.getInstance(TRANSFORMATION);
  20.         enc_cipher.init(Cipher.ENCRYPT_MODE, secret, aps);
  21.         /*final AlgorithmParameters params = enc_cipher.getParameters();
  22.         final byte[] iv = params.getParameterSpec(IvParameterSpec.class)
  23.                 .getIV();*/
  24.  
  25.         // Build decryptor
  26.         final Cipher dec_cipher = Cipher.getInstance(TRANSFORMATION);
  27.         dec_cipher.init(Cipher.DECRYPT_MODE, secret, aps);
  28.  
  29.         this.m_enc_cipher = enc_cipher;
  30.         this.m_dec_cipher = dec_cipher;
  31.  
  32.         this.m_iv = iv;
  33.  
  34.         this.m_secret_key = secret.getEncoded();
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement