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

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

java中怎么實(shí)現(xiàn)對(duì)稱(chēng)加密算法-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需設(shè)計(jì)網(wǎng)站,是成都網(wǎng)站營(yíng)銷(xiāo)公司,為白烏魚(yú)提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開(kāi)發(fā)等。成都網(wǎng)站改版熱線: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);//設(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ì)稱(chēng)加密算法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


名稱(chēng)欄目:java中怎么實(shí)現(xiàn)對(duì)稱(chēng)加密算法-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://weahome.cn/article/dgpdsg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部