亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 對稱加密算法實現詳解

發布時間:2020-08-24 14:26:05 來源:腳本之家 閱讀:256 作者:小破孩123 欄目:編程語言

前言

對于信息的加密方式多種多樣,之前為大家介紹了一種自己設計的加密方式,有興趣的朋友可以欣賞一下,歡迎給予指點。今天為大家介紹一下對稱加密方式,所謂對稱加密指的是加密和解密方式呈對稱格式,即解密是加密的逆過程,下面我們就看一下: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();
    }
  }
}

運行結果:

java 對稱加密算法實現詳解

第二種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();
    }
  }
}

運行結果:

java 對稱加密算法實現詳解

第三種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();
    }  
  }
}

運行結果:

java 對稱加密算法實現詳解

最后一種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 對稱加密算法實現詳解

結語

對于以上四種方式,PBE是一種較安全的加密方式,對加密起絕定作用的是鹽和password,決定著加密的結果。以上即使要為大家介紹的對稱加密方式,哪位有更好的方式,還望指教。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

察雅县| 达尔| 忻城县| 乡宁县| 广南县| 盈江县| 云梦县| 略阳县| 莱西市| 桦甸市| 彰化市| 涿州市| 东莞市| 土默特右旗| 临高县| 如皋市| 靖远县| 隆化县| 涿鹿县| 黄浦区| 霍林郭勒市| 綦江县| 沂水县| 江口县| 乐昌市| 周至县| 筠连县| 泽普县| 高密市| 宣武区| 唐河县| 赣州市| 蓬溪县| 屏南县| 湖北省| 宝应县| 凤凰县| 永兴县| 黎平县| 光山县| 游戏|