[Java]代码
import java.security.Key; import java.security.SecureRandom; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; public class DESUtil { /* * * 注: 1、DES使用56位密钥,以现代计算能力,24小时内即可被破解; * 2、3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准)。使用3条56位的密钥对 数据进行三次加密。 * 3、DES算法的加密密钥是根据用户输入的密码生成的,该算法把64位密码中的第8位、第16位、第24位、第32位、第40位、第48位、第56位、第64位作为奇偶校验位,在计算密钥时要忽略这8位. * 在generateKey()生成的随机密钥为8位(64bit) */ public static final String ALGORITHM = "DES"; public static final String TRANSFORMATION = "DES/ECB/PKCS5Padding"; /** * 生成随机密钥 * * @return * @throws Exception */ public static Key generateKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(new SecureRandom()); Key key = keyGenerator.generateKey(); return key; } /** * 生成固定密钥 * * @param seed * @return * @throws Exception */ public static Key generateKey(byte[] seed) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(new SecureRandom(seed)); Key key = keyGenerator.generateKey(); return key; } /** * 生成固定密钥 * * @param password * @return * @throws Exception */ public static Key generateKey(String password) throws Exception { return generateKey(password.getBytes()); } /** * 执行加密 * * @param content * @param key 长度必须为8位,即64bit * @return * @throws Exception */ public static byte[] encrypt(byte[] content, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); byte[] output = cipher.doFinal(content); return output; } /** * 执行加密 * * @param content * @param password * @return * @throws Exception */ public static byte[] encrypt(byte[] content, String password) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, generateKey(password)); byte[] output = cipher.doFinal(content); return output; } /** * 执行解密 * * @param content * @param key 长度必须为8位,即64bit * @return * @throws Exception */ public static byte[] decrypt(byte[] content, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); byte[] output = cipher.doFinal(content); return output; } /** * 执行解密 * * @param content * @param password * @return * @throws Exception */ public static byte[] decrypt(byte[] content, String password) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, generateKey(password)); byte[] output = cipher.doFinal(content); return output; } public static void main(String[] args) throws Exception { System.out.println(Arrays.toString(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "012345"))); System.out.println(new String(decrypt(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "012345"), "012345"))); System.out.println(Arrays.toString(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "01234567".getBytes()))); System.out.println(new String(decrypt(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "01234567".getBytes()), "01234567".getBytes()))); /* * 控制台输出: * * [117, -109, -80, -75, 51, -86, -57, -109, -94, 58, 38, 94, 39, -107, -60, 65, -122, -7, -124, 2, -23, -30, -98, -64, 90, 26, 15, 82, 84, 102, -108, -3, -68, -4, 110, -86, -106, 19, -65, -110, 2, 15, 49, -79, -98, 38, -39, 6] * DES使用56位密钥,以现代计算能力 * [-6, 63, -15, 73, -28, 85, 125, 64, 6, 55, -63, 99, 114, 21, 108, -49, 19, 11, -15, -126, 36, -92, 62, 112, -40, 64, -102, 127, -94, -53, -89, 33, 72, -20, -126, -90, 105, -37, -68, -46, -61, -36, 5, -103, 27, 32, 84, 28] * DES使用56位密钥,以现代计算能力 */ } }
[Java]代码
import java.security.Key; import java.security.SecureRandom; import java.util.Arrays; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; public class TripleDESUtil { public static final String ALGORITHM = "DESede"; public static final String TRANSFORMATION = "DESede/ECB/PKCS5Padding"; /** * 生成随机密钥 * * @return * @throws Exception */ public static Key generateKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(new SecureRandom()); Key key = keyGenerator.generateKey(); return key; } /** * 生成固定密钥 * * @param seed * @return * @throws Exception */ public static Key generateKey(byte[] seed) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(new SecureRandom(seed)); Key key = keyGenerator.generateKey(); return key; } /** * 生成固定密钥 * * @param password * @return * @throws Exception */ public static Key generateKey(String password) throws Exception { return generateKey(password.getBytes()); } /** * 执行加密 * * @param content * @param key 长度必须为8位,即64bit * @return * @throws Exception */ public static byte[] encrypt(byte[] content, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); byte[] output = cipher.doFinal(content); return output; } /** * 执行加密 * * @param content * @param password * @return * @throws Exception */ public static byte[] encrypt(byte[] content, String password) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, generateKey(password)); byte[] output = cipher.doFinal(content); return output; } /** * 执行解密 * * @param content * @param key 长度必须为8位,即64bit * @return * @throws Exception */ public static byte[] decrypt(byte[] content, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM)); byte[] output = cipher.doFinal(content); return output; } /** * 执行解密 * * @param content * @param password * @return * @throws Exception */ public static byte[] decrypt(byte[] content, String password) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, generateKey(password)); byte[] output = cipher.doFinal(content); return output; } public static void main(String[] args) throws Exception { System.out.println(Arrays.toString(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345"))); System.out.println(new String(decrypt(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345"), "012345"))); System.out.println(Arrays.toString(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345670123456701234567".getBytes()))); System.out.println(new String(decrypt(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345670123456701234567".getBytes()), "012345670123456701234567".getBytes()))); /* * 控制台输出: * * [-46, 78, 30, 115, 9, -79, -59, 46, 8, 46, -1, -92, -53, -42, -86, 15, -8, -25, -18, 92, 100, -109, 68, -9, -42, 80, 42, 60, -62, -43, 41, 84, -114, 52, 92, -115, -92, 16, -15, 3, 36, -105, 69, 118, -126, 61, 81, 121, -99, -89, -67, 91, 70, 19, 85, 9] * 使用3条56位的密钥对 数据进行三次加密。 * [38, 108, 104, -124, 124, -73, -66, -121, -43, -41, -102, 74, -71, -98, 71, 4, 7, -50, 78, -28, 39, -103, 115, -93, -88, -107, -113, 89, -41, 55, -93, 111, -43, -120, -47, -50, -2, -104, 107, 105, 114, 45, 120, 40, 103, 64, 19, 60, 37, 18, 100, 71, 106, -66, 123, -66] * 使用3条56位的密钥对 数据进行三次加密。 */ } }