Java Utililty Methods AES

List of utility methods to do AES

  1. HOME
  2. Java
  3. A
  4. AES

Description

The list of methods to do AES are organized into topic(s).

Method

Cipher aesCipher()
aes Cipher
return Cipher.getInstance("AES/CCM/NoPadding", "BC");
byte[] aesCrypt(byte[] content, Key key, int crypt)
aes Crypt
try {
 Cipher cipher = Cipher.getInstance(AES_CIPER_ALGRITHM);
 cipher.init(crypt, key);
 return cipher.doFinal(content);
} catch (Exception e) {
 e.printStackTrace();
return content;
...
byte[] aesHandler(byte[] toBeHandleData, String password, boolean isEncrypt)
aes Handler
try {
 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;
...
SecretKey aesKey(int keySize)
aes Key
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(keySize);
return kgen.generateKey();
void aesStoreKeyToFile(Key secretKey, String path)
aes Store Key To File
try {
 Path store = Paths.get(path);
 Files.write(store, secretKey.getEncoded());
} catch (Exception e) {
 e.printStackTrace();
String createSessionKey()
Creates random session keys
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(128);
byte[] original = gen.generateKey().getEncoded();
String s = DatatypeConverter.printBase64Binary(original);
return s;
String decrypt(String encryptedText, String key)
decrypt
try {
 byte[] encrypted = BaseEncoding.base64Url().decode(encryptedText);
 Cipher cipher = Cipher.getInstance("AES");
 cipher.init(Cipher.DECRYPT_MODE, getAesKey(key));
 byte[] plain = cipher.doFinal(encrypted);
 if (plain == null || plain.length <= 8) {
 throw new RuntimeException("wrong encrypted text.");
 byte[] data = new byte[plain.length - 8];
 System.arraycopy(plain, 8, data, 0, data.length);
 return new String(data, "ISO-8859-1");
} catch (InvalidKeyException e) {
 throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
 throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
 throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
 throw new RuntimeException(e);
} catch (BadPaddingException e) {
 throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
 throw new RuntimeException(e);
String decrypt_aes(String key, String initVector, String encrypted)
decrypaes
try {
 IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
 SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
 byte[] original = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted));
 return new String(original);
} catch (Exception ex) {
...
String encryptAes(String value)
encrypt Aes
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = c.doFinal(value.getBytes());
String encryptedValue = DatatypeConverter.printBase64Binary(encryptedBytes);
return encryptedValue;
String encryptDecrypt(int mode, String data, String key, String salt, String iv)
Encrypts and decrypts a string.
KeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray(), DatatypeConverter.parseBase64Binary(salt),
 ITERATIONS, KEY_LENGTH);
SecretKey secretKey = getSecretKey(pbeKeySpec);
Cipher cipher = getCipher();
cipher.init(mode, secretKey, new IvParameterSpec(DatatypeConverter.parseBase64Binary(iv)));
if (mode == DECRYPT_MODE) {
 return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(data)), StandardCharsets.UTF_8);
} else {
...


AltStyle によって変換されたページ (->オリジナル) /