对称密码的编程使用(DES、3DES、AES)

    xiaoxiao2021-03-25  100

    对称密码的概念

    加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆加解密通信模型

    特点:算法公开、计算量小、加密速度快、加密效率高弱点:双方都使用同样密钥,安全性得不到保证

    DES 算法的编程使用密钥偏短(56位)、生命周期短

    package com.crypt.des; import com.crypt.ByteToHexUtil; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * des加密 * Created by zhangweixiang on 4/17/2016. */ public class DESUtil { /** * 生成desckey * @param type * @return * @throws Exception */ public static byte[] generateKey(String type) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(type); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * 通过指定的deskey加密 * @param data 加密的数据 * @param key 秘钥 * @param type 加密方式 * @return 加密信息 * @throws Exception */ public static byte[] encrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.ENCRYPT_MODE,secretKey); return cipher.doFinal(data); } /** * des解密 * @param data 需要解密的数据 * @param key 解密秘钥 * @param type 类型 * @return 解密后的结果 * @throws Exception */ public static byte[] decrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.DECRYPT_MODE,secretKey); return cipher.doFinal(data); } public static void main(String[] args) throws Exception { String data = "test desc"; String type = "DES";// DES/ECB/PKCS5Padding byte[] key = DESUtil.generateKey(type); byte[] encData = DESUtil.encrypt(data.getBytes(),key,type); String encDataStr = ByteToHexUtil.bytesToHexString(encData); System.out.println(data+">>des encrypt>>"+encDataStr); byte[] decData = DESUtil.decrypt(encData,key,type); System.out.println(encDataStr+">>des decrypt>>"+new String(decData)); } }

    3DES 算法的编程使用

    将密钥长度增至112位或168位,通过增加迭代次数提高安全性

    缺点:处理速度较慢、密钥计算时间较长、加密效率不高

    package com.crypt.des; import com.crypt.ByteToHexUtil; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * 3des加密 * Created by zhangweixiang on 4/17/2016. */ public class TripleDESUtils { /** * 生成3desckey * @param type * @return * @throws Exception */ public static byte[] generateKey(String type) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(type); keyGenerator.init(112);//128 168 SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * 通过指定的3deskey加密 * @param data 加密的数据 * @param key 秘钥 * @param type 加密方式 * @return 加密信息 * @throws Exception */ public static byte[] encrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.ENCRYPT_MODE,secretKey); return cipher.doFinal(data); } /** * 3des解密 * @param data 需要解密的数据 * @param key 解密秘钥 * @param type 类型 * @return 解密后的结果 * @throws Exception */ public static byte[] decrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.DECRYPT_MODE,secretKey); return cipher.doFinal(data); } public static void main(String[] args) throws Exception { String data = "test desc"; String type = "DESede";// DES/ECB/PKCS5Padding byte[] key = TripleDESUtils.generateKey(type); byte[] encData = TripleDESUtils.encrypt(data.getBytes(),key,type); String encDataStr = ByteToHexUtil.bytesToHexString(encData); System.out.println(data+">>3des encrypt>>"+encDataStr); byte[] decData = TripleDESUtils.decrypt(encData,key,type); System.out.println(encDataStr+">>3des decrypt>>"+new String(decData)); } }

    AES 算法的编程使用

    1.AES:高级数据加密标准,能够有效抵御已知的针对DES算法的所有攻击

    2.特点:密钥建立时间短、灵敏性好、内存需求低、安全性高

    package com.crypt.des; import com.crypt.ByteToHexUtil; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * aes加密/解密 * Created by zhangweixiang on 4/17/2016. */ public class AESUtil { /** * 生成adesckey * @param type * @return * @throws Exception */ public static byte[] generateKey(String type) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(type); keyGenerator.init(128);//默认为128位,如果使用192 256则需获取无政策文件(从oracle官网下载UnlimitedJECPolicyJDK7解压后将 //其中的2个jar拷贝到jre下的lib下的security中即可 ) SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * 通过指定的aeskey加密 * @param data 加密的数据 * @param key 秘钥 * @param type 加密方式 * @return 加密信息 * @throws Exception */ public static byte[] encrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.ENCRYPT_MODE,secretKey); return cipher.doFinal(data); } /** * aes解密 * @param data 需要解密的数据 * @param key 解密秘钥 * @param type 类型 * @return 解密后的结果 * @throws Exception */ public static byte[] decrypt(byte[] data,byte[] key,String type) throws Exception { SecretKey secretKey = new SecretKeySpec(key,type); Cipher cipher = Cipher.getInstance(type); cipher.init(Cipher.DECRYPT_MODE,secretKey); return cipher.doFinal(data); } public static void main(String[] args) throws Exception { String data = "test desc"; String type = "AES";//AES/ECB/PKCS5Padding byte[] key = AESUtil.generateKey(type); byte[] encData = AESUtil.encrypt(data.getBytes(),key,type); String encDataStr = ByteToHexUtil.bytesToHexString(encData); System.out.println(data+">>aes encrypt>>"+encDataStr); byte[] decData = AESUtil.decrypt(encData,key,type); System.out.println(encDataStr+">>aes decrypt>>"+new String(decData)); } }
    转载请注明原文地址: https://ju.6miu.com/read-36977.html

    最新回复(0)