这几天在做字段加密的内容。所以就把这部分东西简单的了解一下。

1、首先,加密分对称加密及不对称加密。

    对称加密:在消息发送前使用密钥对消息进行加密,在对方收到消息后,使用相同的密钥进行解密。

    非对称加密:加密和解密使用不同的密钥。通常有密钥A和B,使用A加密得到的密文只有B可以解密(A自身也不可解)。即为私钥和公钥。顾名思义,私钥加密,公钥解密。

    典型的对称加密是DES算法,典型的非对称加密是RSA算法。

2、这次使用了MD5和3DES,还是先对这个做一个归纳。

MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2、MD3和MD4发展而来。Message-Digest,意思为报文摘要,对不定长的报文做出定长的“报文摘要”。

附代码:    

import java.security.MessageDigest;public static final String encode(String s) {        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F' };try {byte[] btInput = s.getBytes();// 获得MD5摘要算法的 MessageDigest 对象MessageDigest mdInst = MessageDigest.getInstance("MD5");// 使用指定的字节更新摘要mdInst.update(btInput);// 获得密文byte[] md = mdInst.digest();// 把密文转换成十六进制的字符串形式int j = md.length;char str[] = new char[j * 2];int k = 0;for (int i = 0; i < j; i++) {byte byte0 = md[i];str[k++] = hexDigits[byte0 >>> 4 & 0xf];str[k++] = hexDigits[byte0 & 0xf];}return new String(str);} catch (Exception e) {e.printStackTrace();return null;}}

注意:一定要在加密前,将需要加密的字符串转为字节数组。

DES算法:是使用一个56位的密钥以及附加的8位奇偶校验位(每组的第8位作为奇偶校验位),产生最大64位的分组大小。这是一个迭代的分组密码,使用称为Feistel的技术,其中将加密的文本块分为两半,使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去。但最后一个循环不交换。DES使用16轮循环,使用异或、置换、代换、移位操作四种基本运算。

而3DES就是对数据块进行三次DES算法加密来进行加固。

代码如下:

public static String COMMON_KEY = "843ce";//密钥public static byte[] desEncrypt(String msg, String salt) {if (msg == null)msg = "";if (salt == null) {salt = COMMON_KEY;}byte[] keyBytes = new byte[8];int saltLen = salt.length();byte[] saltBytes = salt.getBytes();//转换成字节数组,这是加密的必要步骤!for (int i = 0; i < 8; i++) {keyBytes[i] = saltBytes[i % saltLen];}try {DESKeySpec keySpec = new DESKeySpec(keyBytes);SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");desCipher.init(Cipher.ENCRYPT_MODE, key);byte[] text = msg.getBytes("UTF-8");byte[] ciphertext = desCipher.doFinal(text);return ciphertext;} catch (Exception e) {e.printStackTrace();}return null;}public static String desDecrypt(byte[] msg, String salt) {if (msg == null)return null;if (salt == null) {salt = COMMON_KEY;}byte[] keyBytes = new byte[8];int saltLen = salt.length();byte[] saltBytes = salt.getBytes();for (int i = 0; i < 8; i++) {keyBytes[i] = saltBytes[i % saltLen];}try {DESKeySpec keySpec = new DESKeySpec(keyBytes);SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");desCipher.init(Cipher.DECRYPT_MODE, key);byte[] deciphertext = desCipher.doFinal(msg);return new String(deciphertext, "UTF-8");} catch (Exception e) {e.printStackTrace();}return null;}public static String dumpBytes(byte[] bytes) {StringBuffer sb = new StringBuffer();for (int i = 0; i < bytes.length; i++) {if (i % 32 == 0 && i != 0) {sb.append("\n");}String s = Integer.toHexString(bytes[i]);if (s.length() < 2) {s = "0" + s;}if (s.length() > 2) {s = s.substring(s.length() - 2);}sb.append(s);}return sb.toString();}public static byte[] parseBytes(String str) {try {int len = str.length() / 2;if (len <= 2) {return new byte[] { Byte.parseByte(str) };}byte[] arr = new byte[len];for (int i = 0; i < arr.length; i++) {arr[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);}return arr;} catch (Exception e) {return new byte[0];}}/** * 加密 *  * @param encrypt_value *            被加密的字符串 * @param encrypt_key *            加密的密钥 * @return */public static String encryptAsString(String encrypt_value,String encrypt_key) {return dumpBytes(desEncrypt(encrypt_value, encrypt_key));}/** * 解密 *  * @param encrypt_value *            要解密的字符串 * @param encrypt_key *            密钥 * @return */public static String desEncryptAsString(String encrypt_value,String encrypt_key) {return desDecrypt(parseBytes(encrypt_value), encrypt_key);}