Java DES 加密 解密 示例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package com.techzero.des; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /** * DESTest.java * * @author Techzero * @Email techzero@163.com * @Time 2013-12-12 下午2:22:58 */ public class DESTest { /** * @param args */ public static void main(String[] args) { String content = "DESTest"; // 密码长度必须是8的倍数 String password = "12345678"; System.out.println("密 钥:" + password); System.out.println("加密前:" + content); byte[] result = encrypt(content, password); System.out.println("加密后:" + new String(result)); String decryResult = decrypt(result, password); System.out.println("解密后:" + decryResult); } /** * 加密 * * @param content * 待加密内容 * @param key * 加密的密钥 * @return */ public static byte[] encrypt(String content, String key) { try { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, securekey, random); byte[] result = cipher.doFinal(content.getBytes()); return result; } catch (Throwable e) { e.printStackTrace(); } return null; } /** * 解密 * * @param content * 待解密内容 * @param key * 解密的密钥 * @return */ public static String decrypt(byte[] content, String key) { try { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, securekey, random); byte[] result = cipher.doFinal(content); return new String(result); } catch (Throwable e) { e.printStackTrace(); } return null; } } |
示例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.david.des; import java.security.SecureRandom; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /** * * 项目名称:CipherTest * 类 名 称:AESEncryptTools * 类 描 述:DES加密解密算法 * 创 建 人:david * 创建时间:2016年5月2日 下午8:00:21 * Copyright (c) david-版权所有 */ public final class DESEncryptTools { //加密算是是des private static final String ALGORITHM = "DES"; //转换格式 private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding"; //利用8个字节64位的key给src加密 @SuppressWarnings("unused") public static byte[] encrypt(byte[] src,byte[]key) { try { //加密 Cipher cipher = Cipher.getInstance(TRANSFORMATION); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM); KeySpec keySpec = new DESKeySpec(key); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); cipher.init(Cipher.ENCRYPT_MODE, secretKey,new SecureRandom()); byte[] enMsgBytes = cipher.doFinal(src); return enMsgBytes; } catch (Exception e) { e.printStackTrace(); } return null; } //利用8个字节64位的key给src解密 @SuppressWarnings("unused") public static byte[] decrypt(byte[] encryptBytes,byte[]key){ try { //解密 //Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher deCipher = Cipher.getInstance(TRANSFORMATION); SecretKeyFactory deDecretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM); KeySpec deKeySpec = new DESKeySpec(key); SecretKey deSecretKey = deDecretKeyFactory.generateSecret(deKeySpec); deCipher.init(Cipher.DECRYPT_MODE, deSecretKey,new SecureRandom()); byte[] deMsgBytes = deCipher.doFinal(encryptBytes); return deMsgBytes; } catch (Exception e) { e.printStackTrace(); } return null; } } |
Java 3DES 加密 解密 示例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
package des3; import java.io.IOException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; @SuppressWarnings("restriction") public class TripleDESTest { /** * 根据参数生成Key; * * @param strKey */ private Key getKey(String strKey) { Key key = null; try { KeyGenerator _generator = KeyGenerator.getInstance("DES"); _generator.init(new SecureRandom(strKey.getBytes())); key = _generator.generateKey(); _generator = null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return key; } /** * 获得一次3DES加密后的密文 * * @param * @return strMi */ public String getEncString(String strMing, String strKey) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; Key key = getKey(strKey); BASE64Encoder encoder = new BASE64Encoder(); try { byteMing = strMing.getBytes("utf-8"); byteMi = getEncCode(byteMing, key); strMi = encoder.encode(byteMi); } catch (Exception e) { e.printStackTrace(); } finally { encoder = null; byteMi = null; byteMing = null; } return strMi; } /** * 获得两次3DES加密后的密文 * * @param * @return strMi */ public String getTwiceEncString(String strMing, String strKey) { return getEncString(getEncString(strMing, strKey), strKey); } /** * 获得一次3DES解密后的明文 * * @param * @return strMing */ public String getDecString(String strMi, String strKey) { BASE64Decoder base64Decoder = new BASE64Decoder(); byte[] byteMing = null; byte[] byteMi = null; String strMing = ""; Key key = getKey(strKey); try { byteMi = base64Decoder.decodeBuffer(strMi); byteMing = getDecCode(byteMi, key); strMing = new String(byteMing, "utf-8"); } catch (IOException e) { e.printStackTrace(); } finally { base64Decoder = null; byteMing = null; byteMi = null; } return strMing; } /** * 获得两次3DES解密后的明文 * * @param * @return strMing */ public String getTwiceDecString(String strMi, String strKey) { return getDecString(getDecString(strMi, strKey), strKey); } /** * 获得一次3DES加密后的密文 * * @param byts * @return */ private byte[] getEncCode(byte[] byts, Key key) { byte[] byteFina = null; Cipher cipher; try { cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byteFina = cipher.doFinal(byts); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } /** * 获得一次3DES解密后的明文 * * @param bytd * @return */ private byte[] getDecCode(byte[] bytd, Key key) { byte[] byteFina = null; Cipher cipher = null; try { cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byteFina = cipher.doFinal(bytd); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } public static void main(String[] args) { TripleDESTest td = new TripleDESTest(); Key k = td.getKey("Key"); System.out.println("获得的密钥key是:" + k); String encyStr = td.getEncString("Test", "Key"); System.out.println("一次加密后的密文是:" + encyStr); String decyStr = td.getDecString(encyStr, "Key"); System.out.println("一次解密后的明文是:" + decyStr); encyStr = td.getTwiceEncString("Test", "Key"); System.out.println("两次加密后的密文是:" + encyStr); decyStr = td.getTwiceDecString(encyStr, "Key"); System.out.println("两次解密后的明文是:" + decyStr); } } |
示例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
// javac test3.java // java test3 import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * * @author Administrator */ public class STD3Des { public static void main(String[] args) throws Exception { byte[] key = ("f510b8737344cddbca1c8564").getBytes(); // byte[] keyiv = {0x66,0x6f,0x61,0x6f,0x63,0x75,0x65,0x6e}; byte[] keyiv = {'f','o','a','o','c','u','e','n'}; System.out.println("\r\nkey.length:" + key.length); byte[] data = "中国ABCabc1234".getBytes("UTF-8"); System.out.println("ECB加密解密"); byte[] str3 = des3EncodeECB(key, data); byte[] str4 = ees3DecodeECB(key, str3); System.out.println(new BASE64Encoder().encode(str3)); System.out.println(new String(str4, "UTF-8")); System.out.println("<=============>"); System.out.println("CBC加密解密"); byte[] str5 = des3EncodeCBC(key, keyiv, data); byte[] str6 = des3DecodeCBC(key, keyiv, str5); System.out.println(new BASE64Encoder().encode(str5)); System.out.println(new String(str6, "UTF-8")); } /** * ECB加密,不要IV * * @param key 密钥 * @param data 明文 * @return Base64编码的密文 * @throws Exception */ public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(data); return bOut; } /** * ECB解密,不要IV * * @param key 密钥 * @param data Base64编码的密文 * @return 明文 * @throws Exception */ public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(data); return bOut; } /** * CBC加密 * * @param key 密钥 * @param keyiv IV * @param data 明文 * @return Base64编码的密文 * @throws Exception */ public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.ENCRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; } /** * CBC解密 * * @param key 密钥 * @param keyiv IV * @param data Base64编码的密文 * @return 明文 * @throws Exception */ public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; } } |