AES加密解密代码,key是16位
来源:互联网 发布:linux 清除arp缓存 编辑:程序博客网 时间:2024/04/30 21:13
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[n] & 0XFF)); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(); }}
- AES加密解密代码,key是16位
- AES加密解密代码
- AES 256位 加密解密
- AES加密解密代码实现
- Android AES加密/解密代码
- python AES-16位加密解密功能实现
- java AES 128 位加密解密算法
- AES 之128位加密与解密
- SQLServer CLR 函数AES加密解密代码
- MD5的32位16位加密解密代码
- php AES cbc模式 pkcs7 128位加密解密
- android aes 128位加密解密 一般商用
- 加密解密。(aes, java)
- AES加密解密类
- AES 加密/解密备忘
- java,AES加密,解密
- AES 加密解密 c#
- AES加密解密程序。
- 实例解析linux内核I2C体系结构
- 二维数组大折腾
- Android自适应不同分辨率
- android 加速度传感器妙用与自定义View
- js showModalDialog参数传递
- AES加密解密代码,key是16位
- Linux常用性能检测命令
- 高通驱动开发参考(二)
- MSVC vs. MinGW 之 (lib,dll,def,obj,exe) vs (a,dll,def,o,exe) 玩转攻略手记_Kaien Space
- QMessageBox按钮汉化
- HTTP 数据传输 -- Wizfi210应用笔记
- ExtJS4组件_Grid配置-属性-方法详解-案例
- Objective-C与JavaScript的交互
- android 传感器实例 以及 自定义可复用控件的使用