AES加密解密代码,key是16位
2012-12-10 10:05
375 查看
import javax.crypto.*; import javax.crypto.spec.*; public class MyAES { public static void main(String[] args) throws Exception { /* * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定 */ String cKey = "1234567890abcDEF"; // 需要加密的字串 String cSrc = "LUZAIMOU"; // 加密 long lStart = System.currentTimeMillis(); // String enString = MyAES.encrypt(cSrc, cKey); // System.out.println("加密后的字串是:" + enString); long lUseTime = System.currentTimeMillis() - lStart; System.out.println("加密耗时:" + lUseTime + "毫秒"); // 解密 lStart = System.currentTimeMillis(); // String DeString = MyAES.decrypt(enString, cKey); // System.out.println("解密后的字串是:" + DeString); lUseTime = System.currentTimeMillis() - lStart; System.out.println("解密耗时:" + lUseTime + "毫秒"); } public static byte[] decrypt(byte[] sSrc, String sKey) throws Exception { try { // 判断Key是否正确 if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encrypted1 = sSrc; try { byte[] original = cipher.doFinal(encrypted1); return original; } catch (Exception e) { System.out.println(e.toString()); return null; } } catch (Exception ex) { System.out.println(ex.toString()); return null; } } // 判断Key是否正确 public static byte[] encrypt(byte[] sSrc, String sKey) throws Exception { if (sKey == null) { System.out.print("Key为空null"); return null; } // 判断Key是否为16位 if (sKey.length() != 16) { System.out.print("Key长度不是16位"); return null; } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc); return encrypted; } public static byte[] parseHexStr2Byte(String strhex) { if (strhex == null) { return null; } int l = strhex.length(); if (l % 2 == 1) { return null; } byte[] b = new byte[l / 2]; for (int i = 0; i != l / 2; i++) { b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16); } return b; } public static String parseByte2HexStr(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(); } }
相关文章推荐
- 【前端Js】高级加密解密标准AES加密(Javascript代码实现)
- Android AES加密/解密代码
- C#代码实现对AES加密解密
- 在Java中使用AES加密,并且加密的Key长度在16位以上
- python AES-16位加密解密功能实现
- AES加密解密代码
- C#实现AES加密和解密函数原代码
- [小代码]在Android和PHP之间的加密/解密,AES加密
- android使用AES加密和解密文件实例代码
- 代码虚拟的[小代码]在Android和PHP之间的加密/解密,AES加密
- 使用Python进行AES加密和解密的示例代码
- AES加密解密代码实现
- SQLServer CLR 函数AES加密解密代码
- MD5的32位16位加密解密代码
- AES加密时抛出 Illegal key size or default parameters
- Base64加密解密原理以及代码实现
- 不同语言的AES加密解密登录实现
- java 加密 解密 Illegal key size
- java实现RSA+AES的简单加密解密
- 兔子--AES加密,解密算法