All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.davidbracewell.EncryptionMethod Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
/*
 * (c) 2005 David B. Bracewell
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.davidbracewell;

import com.google.common.base.Throwables;
import lombok.NonNull;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;

/**
 * Constants for common encryption algorithms.
 *
 * @author David B. Bracewell
 */
public enum EncryptionMethod {
  AES("AES", 16),
  DES("DES", 16) {
    protected Cipher constructCipher(byte[] key, int mode) {
      try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(name);
        KeySpec keySpec = new DESKeySpec(ensureKeyLength(key));
        SecretKey secretkey = keyFactory.generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(name);
        cipher.init(mode, secretkey);
        return cipher;
      } catch (Exception e) {
        throw Throwables.propagate(e);
      }
    }
  },
  TRIPLE_DES("DESede", 24),
  BLOWFISH("Blowfish", 16);

  protected final String name;
  protected final int keyLength;

  /**
   * Private Constructor
   *
   * @param name The name used for Java internals
   */
  EncryptionMethod(String name, int keyLength) {
    this.name = name;
    this.keyLength = keyLength;
  }

  protected final byte[] ensureKeyLength(byte[] key) {
    if (key.length == keyLength) {
      return key;
    }
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      throw Throwables.propagate(e);
    }
    byte[] keyBytes = Arrays.copyOf(digest.digest(key), keyLength);
    for (int j = 0, k = 16; j < (keyLength - 16); ) {
      keyBytes[k++] = keyBytes[j++];
    }
    return keyBytes;
  }

  /**
   * Parses a String to find the correct EncryptionMethod.
   *
   * @param name The name;
   * @return An EncryptionMethod
   */
  public static EncryptionMethod fromName(@NonNull String name) {
    for (EncryptionMethod en : EncryptionMethod.values()) {
      if (en.name.equals(name)) {
        return en;
      }
    }
    return EncryptionMethod.valueOf(name);
  }

  protected Cipher constructCipher(byte[] key, int mode) {
    try {
      SecretKeySpec keySpec = new SecretKeySpec(ensureKeyLength(key), name);
      Cipher cipher = Cipher.getInstance(name);
      cipher.init(mode, keySpec);
      return cipher;
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  /**
   * Encrypts content into a Base64 encoded string.
   *
   * @param content The content
   * @param key     The password
   * @return A Base64 encoded version of the encrypted content
   */
  public final String encrypt(String content, String key) {
    return encrypt(content.getBytes(), key.getBytes());
  }

  /**
   * Encrypts content into a Base64 encoded string.
   *
   * @param content The content
   * @param key     The password
   * @return A Base64 encoded version of the encrypted content
   */
  public String encrypt(byte[] content, byte[] key) {
    try {
      Cipher cipher = constructCipher(key, Cipher.ENCRYPT_MODE);
      byte[] encryptedText = cipher.doFinal(content);
      return new String(Base64.getEncoder().withoutPadding().encode(encryptedText));
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  /**
   * Decrypts encrypted content in a Base64 encoded string into a string.
   *
   * @param content The encrypted content
   * @param key     The password
   * @return An unencrypted version of the content
   */
  public final String decryptToString(String content, String key) {
    return new String(decrypt(content, key.getBytes()), StandardCharsets.UTF_8);
  }

  /**
   * Decrypts encrypted content in a Base64 encoded string into a byte array.
   *
   * @param content The encrypted content
   * @param key     The password
   * @return An unencrypted version of the content
   */
  public final byte[] decrypt(String content, String key) {
    return decrypt(content, key.getBytes());
  }

  /**
   * Decrypts encrypted content in a Base64 encoded byte array into a byte array.
   *
   * @param content The encrypted content
   * @param key     The password
   * @return An unencrypted version of the content
   */
  public byte[] decrypt(String content, byte[] key) {
    try {
      Cipher cipher = constructCipher(key, Cipher.DECRYPT_MODE);
      return cipher.doFinal(Base64.getDecoder().decode(content.trim()));
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

}// END OF EncryptionMethod




© 2015 - 2025 Weber Informatics LLC | Privacy Policy