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

com.jianggujin.codec.JSymmetrical Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2017-2018 jianggujin (www.jianggujin.com).
 * 
 * Licensed 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.jianggujin.codec;

import java.io.InputStream;
import java.io.OutputStream;

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

import com.jianggujin.codec.util.JCipherInputStream;
import com.jianggujin.codec.util.JCipherOutputStream;
import com.jianggujin.codec.util.JCodecException;
import com.jianggujin.codec.util.JCodecUtils;

/**
 * 对称加密
 * 
 * @author jianggujin
 *
 */
public class JSymmetrical {

   /**
    * 对称算法
    * 
    * @author jianggujin
    *
    */
   public static enum JSymmetricalAlgorithm {
      DES, DESede, ARCFOUR, AES, Blowfish, RC2, RC4;

      public String getName() {
         return this.name();
      }
   }

   /**
    * 初始化密钥
    * 
    * @param algorithm
    * @return
    */
   public static SecretKey initKey(JSymmetricalAlgorithm algorithm) {
      return initKey(algorithm.getName());
   }

   /**
    * 初始化密钥
    * 
    * @param algorithm
    * @return
    */
   public static SecretKey initKey(String algorithm) {
      return JCodecUtils.initKey(algorithm);
   }

   /**
    * 初始化密钥
    * 
    * @param algorithm
    * @return
    */
   public static byte[] initEncodedKey(JSymmetricalAlgorithm algorithm) {
      return initEncodedKey(algorithm.getName());
   }

   /**
    * 初始化密钥
    * 
    * @param algorithm
    * @return
    */
   public static byte[] initEncodedKey(String algorithm) {
      return initKey(algorithm).getEncoded();
   }

   /**
    * 加密
    * 
    * @param data
    * @param key
    * @param algorithm
    * @return
    */
   public static byte[] encrypt(byte[] data, byte[] key, JSymmetricalAlgorithm algorithm) {
      return encrypt(data, key, algorithm.getName());
   }

   /**
    * 加密
    * 
    * @param data
    * @param key
    * @param algorithm
    * @return
    */
   public static byte[] encrypt(byte[] data, byte[] key, String algorithm) {
      Cipher cipher = getCipher(key, algorithm, Cipher.ENCRYPT_MODE);
      return JCodecUtils.doFinal(data, cipher);
   }

   /**
    * 加密
    * 
    * @param data
    * @param secretKey
    * @return
    */
   public static byte[] encrypt(byte[] data, SecretKey secretKey) {
      Cipher cipher = getCipher(secretKey, Cipher.ENCRYPT_MODE);
      return JCodecUtils.doFinal(data, cipher);
   }

   public static OutputStream wrap(OutputStream out, byte[] key, JSymmetricalAlgorithm algorithm) {
      return wrap(out, key, algorithm.getName());
   }

   public static OutputStream wrap(OutputStream out, byte[] key, String algorithm) {
      Cipher cipher = getCipher(key, algorithm, Cipher.ENCRYPT_MODE);
      return new JCipherOutputStream(cipher, out);
   }

   public static OutputStream wrap(OutputStream out, SecretKey secretKey) {
      Cipher cipher = getCipher(secretKey, Cipher.ENCRYPT_MODE);
      return new JCipherOutputStream(cipher, out);
   }

   /**
    * 解密
    * 
    * @param data
    * @param key
    * @param algorithm
    * @return
    */
   public static byte[] decrypt(byte[] data, byte[] key, JSymmetricalAlgorithm algorithm) {
      return decrypt(data, key, algorithm.getName());
   }

   /**
    * 解密
    * 
    * @param data
    * @param key
    * @param algorithm
    * @return
    */
   public static byte[] decrypt(byte[] data, byte[] key, String algorithm) {
      Cipher cipher = getCipher(key, algorithm, Cipher.DECRYPT_MODE);
      return JCodecUtils.doFinal(data, cipher);
   }

   /**
    * 解密
    * 
    * @param data
    * @param secretKey
    * @return
    */
   public static byte[] decrypt(byte[] data, SecretKey secretKey) {
      Cipher cipher = getCipher(secretKey, Cipher.DECRYPT_MODE);
      return JCodecUtils.doFinal(data, cipher);
   }

   public static InputStream wrap(InputStream in, byte[] key, JSymmetricalAlgorithm algorithm) {
      return wrap(in, key, algorithm.getName());
   }

   public static InputStream wrap(InputStream in, byte[] key, String algorithm) {
      Cipher cipher = getCipher(key, algorithm, Cipher.DECRYPT_MODE);
      return new JCipherInputStream(cipher, in);
   }

   public static InputStream wrap(InputStream in, SecretKey secretKey) {
      Cipher cipher = getCipher(secretKey, Cipher.DECRYPT_MODE);
      return new JCipherInputStream(cipher, in);
   }

   public static Cipher getCipher(byte[] key, JSymmetricalAlgorithm algorithm, int opmode) {
      return getCipher(key, algorithm.getName(), opmode);
   }

   public static Cipher getCipher(byte[] key, String algorithm, int opmode) {
      SecretKey secretKey = getSecretKey(key, algorithm);
      return getCipher(secretKey, opmode);
   }

   public static Cipher getCipher(SecretKey secretKey, int opmode) {
      JCodecUtils.checkOpMode(opmode);
      try {
         Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
         cipher.init(opmode, secretKey);
         return cipher;
      } catch (Exception e) {
         throw new JCodecException(e);
      }
   }

   /**
    * 获得密钥
    * 
    * @param key
    * @param algorithm
    * @return
    */
   public static SecretKey getSecretKey(byte[] key, JSymmetricalAlgorithm algorithm) {
      return getSecretKey(key, algorithm.getName());
   }

   /**
    * 获得密钥
    * 
    * @param key
    * @param algorithm
    * @return
    */
   public static SecretKey getSecretKey(byte[] key, String algorithm) {
      try {
         // if ("DES".equals(algorithm))
         // {
         // DESKeySpec dks = new DESKeySpec(key);
         // SecretKeyFactory keyFactory =
         // SecretKeyFactory.getInstance(algorithm);
         // SecretKey secretKey = keyFactory.generateSecret(dks);
         // return secretKey;
         // }
         return new SecretKeySpec(key, algorithm);
      } catch (Exception e) {
         throw new JCodecException(e);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy