AES加密代码



public class EncryptUtil {
 // --------------------部分代码-----------

 /**
  * AES加密
  *
  * @param key
  *            密钥信息
  * @param content
  *            待加密信息
  */
 public static byte[] encodeAES(byte[] key, byte[] content) throws Exception {
  // 不是16的倍数的,补足
  int base = 16;
  if (key.length % base != 0) {
   int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
   byte[] temp = new byte[groups * base];
   Arrays.fill(temp, (byte) 0);
   System.arraycopy(key, 0, temp, 0, key.length);
   key = temp;
  }

  SecretKey secretKey = new SecretKeySpec(key, "AES");
  IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
  byte[] tgtBytes = cipher.doFinal(content);
  return tgtBytes;
 }

 /**
  * AES解密
  *
  * @param key
  *            密钥信息
  * @param content
  *            待加密信息
  * @return
  * @throws Exception
  */
 public static byte[] decodeAES(byte[] key, byte[] content) throws Exception {
  // 不是16的倍数的,补足
  int base = 16;
  if (key.length % base != 0) {
   int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
   byte[] temp = new byte[groups * base];
   Arrays.fill(temp, (byte) 0);
   System.arraycopy(key, 0, temp, 0, key.length);
   key = temp;
  }

  SecretKey secretKey = new SecretKeySpec(key, "AES");
  IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
  byte[] tgtBytes = cipher.doFinal(content);
  return tgtBytes;
 }
 
 
 
 
 //身份验证使用这个方法。
 public static String encryptAES(String content, String key) {
  try {
   return bytesToHexString(encodeAES(key.getBytes(),
     content.getBytes()));
  } catch (Exception e) {
   return null;
  }
 }

 public static String decryptAES(String content, String key) {
  try {
   byte[] contentByte = hexStringToByte(content);
   return new String(decodeAES(key.getBytes(), contentByte),
     "UTF-8");
  } catch (Exception e) {
   return null;
  }
 }

 
 /**
  * 把字节数组转换成16进制字符串
  *
  * @parambArray
  * @return
  */
 public static String bytesToHexString(byte[] bArray) {
  StringBuffer sb = new StringBuffer(bArray.length);
  String sTemp;
  for (int i = 0; i<bArray.length; i++) {
   sTemp = Integer.toHexString(0xFF &bArray[i]);
   if (sTemp.length() < 2)
    sb.append(0);
   sb.append(sTemp.toUpperCase());
  }
  return sb.toString();
 }

 /*
  * 把16进制字符串转换成字节数组
 *
  * @param hex
  *
  * @return
  */
 public static byte[] hexStringToByte(String hex) {
  int len = (hex.length() / 2);
  byte[] result = new byte[len];
  char[] achar = hex.toCharArray();
  for (int i = 0; i<len; i++) {
   int pos = i * 2;
   result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
  }
  return result;
 }

 private static byte toByte(char c) {
  byte b = (byte) "0123456789ABCDEF".indexOf(c);
  return b;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
aes加密代码,非常完整,包括加密解密。 AESCrypt File Format Description Items in quotes are a literal string. Words outside of quotes are a textual description of the contents. Fixed-valued octets are written in hexidecimal form (e.g., 0x01). The AESCrypt version 2 file format is as follows. 3 Octets - 'AES' 1 Octet - 0x02 (Version) 1 Octet - Reserved .... Start of repeating extension block section 2 Octet - Length in octets (in network byte order) of an extension identifier and contents. If 0x0000, then no further extensions exist and the next octet is the start of the Initialization Vector (IV). Following an extension, this length indicator would appear again to indicate presence or absense of another extension and the size of any such extension. nn Octets - Extension identifier. This is either a URI or an identifier defined by the AES developer community and documented on the standard extensions page, either of which is terminated by a single 0x00 octet. All extension identifiers are case sensitive. Examples of URIs: http://www.aescrypt.com/extensions/creator/ urn:oid:1.3.6.1.4.1.17090.55.14 urn:uuid:85519EA3-1DA6-45b9-9041-8CD368D8C086 Note: A URI was used to allow anybody to define extension types, though we should strive to define a standard set of extensions. Examples of standard extension identifiers: CREATED-DATE CREATED-BY A special extension is defined that has no name, but is merely a "container" for extensions to be added after the AES file is initially created. Such an extension avoids the need to read and re-write the entire file in order to add a small extension. Software tools that create AES files should insert a 128-octet "container" extension, placing a 0x00 in the first octet of the extension identifier field. Developers may then insert extensions into this "container" area and reduce the size of this "container" as necessary. If larger extensions are added or the "container" area is filled entirely, then reading and re-writing the entire file would be necessary to add additional extensions. nn Octets - The contents of the extension .... End of repeating extension block section 16 Octets - Initialization Vector (IV) used for encrypting the IV and symmetric key that is actually used to encrypt the bulk of the plaintext file. 48 Octets - Encrypted IV and 256-bit AES key used to encrypt the bulk of the file 16 octets - Initialization Vector 32 octets - encryption key 32 Octets - HMAC nn Octets - Encrypted message (2^64 octets max) 1 Octet - File size modulo 16 in least significant bit positions 32 Octets - HMAC Thus, the footprint of the file is at least 136 octets. The AESCrypt version 1 file format is as follows. 3 Octets - 'AES' 1 Octet - 0x01 (Version) 1 Octet - Reserved 16 Octets - Initialization Vector (IV) used for encrypting the IV and symmetric key that is actually used to encrypt the bulk of the plaintext file. 48 Octets - Encrypted IV and 256-bit AES key used to encrypt the bulk of the file 16 octets - Initialization Vector 32 octets - encryption key 32 Octets - HMAC nn Octets - Encrypted message (2^64 octets max) 1 Octet - File size modulo 16 in least significant bit positions 32 Octets - HMAC Thus, the footprint of the file is at least 134 octets. The AESCrypt version 0 file format is as follows. 3 Octets - 'AES' 1 Octet - 0x00 (Version) 1 Octet - File size modulo 16 in least significant bit positions 16 Octets - Initialization Vector (IV) nn Octets - Encrypted message (2^64 octets max) 32 Octets - HMAC Thus, the footprint of the file is at least 53 octets.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值