时间:2021-05-19
前言
对于信息的加密方式多种多样,之前为大家介绍了一种自己设计的加密方式,有兴趣的朋友可以欣赏一下,欢迎给予指点。今天为大家介绍一下对称加密方式,所谓对称加密指的是加密和解密方式呈对称格式,即解密是加密的逆过程,下面我们就看一下:DES、3DES、AES、PBE这四种方式,他们的加密强度依次递增。好吧闲话少说,上代码:
首先我们先看一下DES:
package cn.edu.hpu.des;import java.security.Key;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class jdk_DES { private static final String src = "I Love You !"; public static void main(String [] args){ jdk_DES.jdkDES(); } @SuppressWarnings("static-access") public static void jdkDES(){ try { //获得KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56);//设置为默认值56即可 //获得KEY对象 SecretKey secrekeyone = keyGenerator.generateKey(); byte [] byteskey = secrekeyone.getEncoded(); //KEY转换 DESKeySpec deskeyspec = new DESKeySpec(byteskey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); Key secerkeytwo = factory.generateSecret(deskeyspec); //加密 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(cipher.ENCRYPT_MODE, secerkeytwo);//设置模式为加密 byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdkEDS:"+result.toString()); //解密 cipher.init(cipher.DECRYPT_MODE, secerkeytwo);//设置模式为解密 result = cipher.doFinal(result); System.out.println("jdkEDS:"+new String(result)); } catch (Exception e) { e.printStackTrace(); } }}运行结果:
第二种3重DES:
package cn.edu.hpu.des;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class jdk_3DES { private static final String src = "I Love You !"; public static void main(String [] args){ jdk_3DES.jdk3DES(); } @SuppressWarnings("static-access") public static void jdk3DES(){ try { //获得KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); //keyGenerator.init(112);//设置密钥长度,默认值为168,也可设置为112 keyGenerator.init(new SecureRandom());//设置为默认值 //获得KEY对象 SecretKey secrekeyone = keyGenerator.generateKey(); byte [] byteskey = secrekeyone.getEncoded(); //KEY转换 DESKeySpec deskeyspec = new DESKeySpec(byteskey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); Key secerkeytwo = factory.generateSecret(deskeyspec); //加密 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(cipher.ENCRYPT_MODE, secerkeytwo);//设置模式为加密 byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdkEDS:"+result.toString()); //解密 cipher.init(cipher.DECRYPT_MODE, secerkeytwo);//设置模式为解密 result = cipher.doFinal(result); System.out.println("jdkEDS:"+new String(result)); } catch (Exception e) { e.printStackTrace(); } }}运行结果:
第三种AES:
package cn.edu.hpu.aes;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class jdk_AES { private static final String src = "I Love You !"; public static void main(String [] args){ AES(); } public static void AES(){ try { //获得key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(new SecureRandom());//默认密钥长度为: SecretKey secretKey = keyGenerator.generateKey(); byte [] keyBytes = secretKey.getEncoded(); //key转换 Key key = new SecretKeySpec(keyBytes, "AES"); //加密 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("AES="+result.toString()); //解密 cipher.init(Cipher.DECRYPT_MODE, key); result = cipher.doFinal(result); System.out.println("AES="+new String(result)); } catch (Exception e) { e.printStackTrace(); } }}运行结果:
最后一种PBE:
package cn.edu.hpu.pbe;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.PBEKeySpec;import javax.crypto.spec.PBEParameterSpec;public class jdk_PBE { public static final String src = "I Love You !"; public static void main(String [] args){ jdkPBE(); } private static void jdkPBE(){ try { //初始化盐 SecureRandom random = new SecureRandom(); byte [] salt = random.generateSeed(8); //口令和密钥 String password = "imooc"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());//生成密钥转换对象 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); Key key = factory.generateSecret(pbeKeySpec); //加密 PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);//实例化PBE对象的一个输入的材料:参数分别为"盐和迭代次数" Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec); byte [] result = cipher.doFinal(src.getBytes()); System.out.println("PBE:"+result.toString()); //解密 cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec); result = cipher.doFinal(result); System.out.println("PBE:"+new String(result)); } catch (Exception e) { e.printStackTrace(); } }}运行结果:
结语
对于以上四种方式,PBE是一种较安全的加密方式,对加密起绝定作用的是盐和password,决定着加密的结果。以上即使要为大家介绍的对称加密方式,哪位有更好的方式,还望指教。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了java实现的RSA加密算法。分享给大家供大家参考,具体如下:一、什么是非对称加密1、加密的密钥与加密的密钥不相同,这样的加密算法称之为非对称加密
高级加密标准(AES,AdvancedEncryptionStandard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是
最近项目需要选择一套对称加密算法,来满足前后端之间的加解密操作。初步打算前端使用crypto-js来实现,后端使用java本身的加密算法实现,但遇到了一个问题:
非对称加密算法是相对于对称加密算法来说的,对于对称加密算法请查阅之前的总结,今天为大家介绍一下DH算法,DH是一种密钥交换算法,接收方根据发送方加密时的密钥,生
本文实例为大家分享了python实现AES加密和解密的具体代码,供大家参考,具体内容如下参考:python实现AES加密和解密AES加密算法是一种对称加密算法,