首页 > 代码库 > (转)Java DES 与Base64
(转)Java DES 与Base64
原文地址http://blog.csdn.net/tomatozq/article/details/20773559
1,DES
/** * 解密 * @param message * @param key * @return * @throws Exception */ public static String decrypt(String message,String encoding) throws Exception { byte[] bytesrc = http://www.mamicode.com/convertHexString(message); "DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding)); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte,encoding); } /** * 加密 * @param message * @param key * @return * @throws Exception */ public static String encrypt(String message,String encoding) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(encoding)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes(encoding)); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] buf = cipher.doFinal(message.getBytes(encoding)); String a = toHexString(buf).toUpperCase(); return a; } /** * 字符串转换为16进制数组 * @param ss * @return */ public static byte[] convertHexString(String ss) { byte digest[] = new byte[ss.length() / 2]; for (int i = 0; i < digest.length; i++) { String byteString = ss.substring(2 * i, 2 * i + 2); int byteValue = http://www.mamicode.com/Integer.parseInt(byteString, 16); "0" + plainText; hexString.append(plainText); } return hexString.toString(); }
2,Base64
byte[] enc = Base64.encode(encText.getBytes(encoding), Base64.DEFAULT); byte[] bytesrc = http://www.mamicode.com/Base64.decode(encText.getBytes(encoding), Base64.DEFAULT); >
(转)Java DES 与Base64
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。