anyuan2002.com - vwin网

查找: 您的方位主页 > 网络频道 > 阅览资讯:移动开发---AES加密解密

移动开发---AES加密解密

2019-04-05 05:06:42 来历:www.anyuan2002.com 【
vwin娱乐场

aes加密解密

 

package com.extremely.clson.amusive.constellations.utils;

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

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

/**
 * Date: 2019/4/1
 * autor:Wayne
 */
public class AESUtil {
  private static String key = "123456789";

  /**
   *   * 加密
   *   * @param content
   *   * @param strKey
   *   * @return
   *   * @throws Exception
   *   
   */
  public static byte[] encrypt(String content, String strKey) throws Exception {
    SecretKeySpec skeySpec = getKey(strKey);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] encrypted = cipher.doFinal(content.getBytes());
    return encrypted;
  }

  /**
   *   * 解密
   *   * @param strKey
   *   * @param content
   *   * @return
   *   * @throws Exception
   *   
   */
  public static String decrypt(byte[] content, String strKey) throws Exception {
    SecretKeySpec skeySpec = getKey(strKey);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    byte[] original = cipher.doFinal(content);
    String originalString = new String(original);
    return originalString;
  }

  private static SecretKeySpec getKey(String strKey) throws Exception {
    byte[] arrBTmp = strKey.getBytes();
    // 创立一个空的16位字节数组(默认值为0)
    byte[] arrB = new byte[16];

    for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
      arrB[i] = arrBTmp[i];
    }

    SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");

    return skeySpec;
  }

  /**
   *   * base 64 encode
   *   * @param bytes 待编码的byte[]
   *   * @return 编码后的base 64 code
   *   
   */
  public static String base64Encode(byte[] bytes) {
    return new BASE64Encoder().encode(bytes);
  }

  /**
   *   * base 64 decode
   *   * @param base64Code 待解码的base 64 code
   *   * @return 解码后的byte[]
   *   * @throws Exception
   *   
   */
  public static byte[] base64Decode(String base64Code) throws Exception {
    return base64Code.isEmpty() ? null : new BASE64Decoder().decodeBuffer(base64Code);
  }

  /**
   *   * AES加密为base 64 code
   *   * @param content 待加密的内容
   *   * @param encryptKey 加密密钥
   *   * @return 加密后的base 64 code
   *   * @throws Exception //加密传String类型,回来String类型
   *   
   */
  public static String aesEncrypt(String content, String encryptKey) throws Exception {
    return base64Encode(encrypt(content, encryptKey));
  }

  /**
   *   * 将base 64 code AES解密
   *   * @param encryptStr 待解密的base 64 code
   *   * @param decryptKey 解密密钥
   *   * @return 解密后的string  //解密传String类型,回来String类型
   *   * @throws Exception
   *   
   */
  public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
    return encryptStr.isEmpty() ? null : decrypt(base64Decode(encryptStr), decryptKey);
  }

  public static void main(String[] args) throws Exception {
//    String encrypt = aesEncrypt("test", key);
//    System.out.println(encrypt);
    String decrypt = aesDecrypt("ii7wTRyDfeqtT9odaURoPA==", key);
    System.out.println(decrypt);
  }

}

 

 
 

本文地址:http://www.anyuan2002.com/a/question/100264.html
Tags: 移动 开发 ---AES
修改:vwin网
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部