真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java中怎么實(shí)現(xiàn)對(duì)稱加密算法

本篇文章給大家分享的是有關(guān)java中怎么實(shí)現(xiàn)對(duì)稱加密算法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)成立與2013年,先為海門等服務(wù)建站,海門等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為海門企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

首先我們先看一下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);//設(shè)置為默認(rèn)值56即可      //獲得KEY對(duì)象      SecretKey secrekeyone = keyGenerator.generateKey();      byte [] byteskey = secrekeyone.getEncoded();            //KEY轉(zhuǎn)換      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);//設(shè)置模式為加密      byte[] result = cipher.doFinal(src.getBytes());      System.out.println("jdkEDS:"+result.toString());            //解密      cipher.init(cipher.DECRYPT_MODE, secerkeytwo);//設(shè)置模式為解密      result = cipher.doFinal(result);      System.out.println("jdkEDS:"+new String(result));          } catch (Exception e) {      e.printStackTrace();    }  }}

運(yùn)行結(jié)果:

第二種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);//設(shè)置密鑰長(zhǎng)度,默認(rèn)值為168,也可設(shè)置為112      keyGenerator.init(new SecureRandom());//設(shè)置為默認(rèn)值      //獲得KEY對(duì)象      SecretKey secrekeyone = keyGenerator.generateKey();      byte [] byteskey = secrekeyone.getEncoded();            //KEY轉(zhuǎn)換      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);//設(shè)置模式為加密      byte[] result = cipher.doFinal(src.getBytes());      System.out.println("jdkEDS:"+result.toString());            //解密      cipher.init(cipher.DECRYPT_MODE, secerkeytwo);//設(shè)置模式為解密      result = cipher.doFinal(result);      System.out.println("jdkEDS:"+new String(result));          } catch (Exception e) {      e.printStackTrace();    }  }}

運(yùn)行結(jié)果:

第三種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());//默認(rèn)密鑰長(zhǎng)度為:      SecretKey secretKey = keyGenerator.generateKey();      byte [] keyBytes = secretKey.getEncoded();            //key轉(zhuǎn)換      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();    }    }}

運(yùn)行結(jié)果:

最后一種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());//生成密鑰轉(zhuǎn)換對(duì)象      SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");      Key key = factory.generateSecret(pbeKeySpec);            //加密      PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100);//實(shí)例化PBE對(duì)象的一個(gè)輸入的材料:參數(shù)分別為"鹽和迭代次數(shù)"      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中怎么實(shí)現(xiàn)對(duì)稱加密算法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


本文標(biāo)題:java中怎么實(shí)現(xiàn)對(duì)稱加密算法
網(wǎng)站網(wǎng)址:http://weahome.cn/article/gcjpho.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部