Java AES aesHandler(byte[] toBeHandleData, String password, boolean isEncrypt)

Here you can find the source of aesHandler(byte[] toBeHandleData, String password, boolean isEncrypt)

Description

aes Handler

License

Apache License

Declaration

private static byte[] aesHandler(byte[] toBeHandleData, String password, boolean isEncrypt) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final byte[] IV = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    private static byte[] aesHandler(byte[] toBeHandleData, String password, boolean isEncrypt) {
        try {//from  w w  w.  j  a  v  a  2  s . co  m
            SecretKeySpec e = new SecretKeySpec(toKey(password), "AES");
            Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
            IvParameterSpec initialParameter = new IvParameterSpec(IV);
            cipher.init(isEncrypt ? 1 : 2, e, initialParameter);
            return cipher.doFinal(toBeHandleData);
        } catch (Exception var6) {
            return null;
        }
    }

    private static byte[] toKey(String toBeHashedString) {
        try {
            MessageDigest e = MessageDigest.getInstance("MD5");
            e.reset();
            e.update(toBeHashedString.getBytes());
            return e.digest();
        } catch (Exception var2) {
            return null;
        }
    }
}

Related

  1. aesCipher()
  2. aesCrypt(byte[] content, Key key, int crypt)
  3. aesKey(int keySize)
  4. aesStoreKeyToFile(Key secretKey, String path)
  5. createSessionKey()
  6. decrypt(String encryptedText, String key)