1 JAVA实现 2 加密 3 4 注意:DES加密和解密过程中,密钥长度都必须是8的倍数 5 6 public byte[] desCrypto(byte[] datasource, String password) { 7 try{ 8
9 DESKeySpec desKey = new DESKeySpec(password.getBytes()); 10 //创建一个密匙工厂,然后用它把DESKeySpec转换成 11 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 12 SecretKey securekey = keyFactory.generateSecret(desKey);
13 //Cipher对象实际完成加密操作 14 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 偏移量
IvParameterSpec iv = new IvParameterSpec(password.getBytes()); 15 //用密匙初始化Cipher对象 16 cipher.init(Cipher.ENCRYPT_MODE, securekey, iv); 17 //现在,获取数据并加密 18 //正式执行加密操作 19 return cipher.doFinal(datasource); 20 }catch(Throwable e){ 21 e.printStackTrace(); 22 } 23 return null; 24 }
25 26 解密 27 28 29 private byte[] decrypt(byte[] src, String password) throws Exception { 30 31 32 // 创建一个DESKeySpec对象 33 DESKeySpec desKey = new DESKeySpec(password.getBytes()); 34 // 创建一个密匙工厂 35 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 36 // 将DESKeySpec对象转换成SecretKey对象 37 SecretKey securekey = keyFactory.generateSecret(desKey); 38 // Cipher对象实际完成解密操作 39 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
// 偏移量
IvParameterSpec iv = new IvParameterSpec(password.getBytes());
40 // 用密匙初始化Cipher对象 41 cipher.init(Cipher.DECRYPT_MODE, securekey, iv); 42 // 真正开始解密操作 43 return cipher.doFinal(src); 44 } 45
46 测试场景 47 例如,我们可以利用如上函数对字符串进行加密解密,也可以对文件进行加密解密,如: 48 [java] view plaincopy 49 //待加密内容 50 String str = "测试内容"; 51 //密码,长度要是8的倍数 52 String password = "12345678"; 53 byte[] result = DESCrypto.desCrypto(str.getBytes(),password); 54 System.out.println("加密后内容为:"+new String(result)); 55 56 //直接将如上内容解密 57 try { 58 byte[] decryResult = des.decrypt(result, password); 59 System.out.println("加密后内容为:"+new String(decryResult)); //加密后内容为:测试内容
60 } catch (Exception e1) { 61 e1.printStackTrace(); 62 }
计算机中的数据都是二进制的,不管是字符串还是文件,而加密后的也是二进制的,
由于加密后输出的为byte[],而byte[]对我们显示是不友好的(乱码),所以需要将byte[]转换为其他编码,一般都是转换为base64编码,
1 /** 2 * 将byte[]转为各种进制的字符串 3 * @param bytes byte[] 4 * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制 5 * @return 转换后的字符串 6 */ 7 public static String binary(byte[] bytes, int radix){ 8 return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数 9 } 10 11 /** 12 * base 64 encode 13 * @param bytes 待编码的byte[] 14 * @return 编码后的base 64 code 15 */ 16 public static String base64Encode(byte[] bytes){ 17 return new BASE64Encoder().encode(bytes); 18 } 19 20 /** 21 * base 64 decode 22 * @param base64Code 待解码的base 64 code 23 * @return 解码后的byte[] 24 * @throws Exception 25 */ 26 public static byte[] base64Decode(String base64Code) throws Exception{ 27 return QStringUtil.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code); 28 }
1 /** 2 * 把二进制转化为大写的十六进制 3 * 4 * @param bytes 5 * @return 6 */ 7 private static String byte2hex(byte[] bytes) { 8 StringBuilder sign = new StringBuilder(); 9 for (int i = 0; i < bytes.length; i++) { 10 String hex = Integer.toHexString(bytes[i] & 0xFF); 11 if (hex.length() == 1) { 12 sign.append("0"); 13 } 14 sign.append(hex.toUpperCase()); 15 } 16 return sign.toString(); 17 }