快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java中怎么实现对称加密算法-创新互联

本篇文章给大家分享的是有关java中怎么实现对称加密算法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站成都网站建设按需设计网站,是成都网站营销公司,为白乌鱼提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站改版热线:13518219792

首先我们先看一下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();    }  }}

以上就是java中怎么实现对称加密算法,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


当前名称:java中怎么实现对称加密算法-创新互联
文章路径:http://6mz.cn/article/dgpdsg.html

其他资讯