国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一個用JAVA開發的會話密鑰程序,可能對你有所幫助

2019-11-18 12:10:47
字體:
來源:轉載
供稿:網友

  //package
  /*
  運行本程序你需要下載JCE,Bouncy Castle的JCE with PRovider and Lightweight API
   網止是 http://www.bouncycastle.org
   配置如下:
   在WINDOWS中,你需要把下載的bcprov-jdk14-119.jar文件拷貝到兩個地方:
   一個在你安裝的JDK目錄中,比如說我的是C:/j2sdk1.4.0-rc/jre/lib/ext
   另一個在你的JDK運行環境中,我的是在C:/Program Files/java/j2re1.4.0-rc/lib/ext;
   另外還要在對兩個java.security進行修改:
   我的在 C:/j2sdk1.4.0-rc/jre/lib/security/java.security;
   C:/Program Files/Java/j2re1.4.0-rc/lib/security/java.security;
   在java.security中加入
   security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider
   假如一切順利,你就可以運行本程序了。
  
   該程序具有對你的文件加解密功能。需要你指定的數據,程序中已給了接口。
   比如說你指定了要加密的文件名"4.txt",加密后的文件存放位置"6.txt",
   還有口令passWord如"liufeng"后,運行該程序,那么"6.txt" 中將是"4.txt"的密文。
   注重口令是解密的鑰匙,不要忘記。
   其他解密過程自己參考。
  
   本程序利用會話密鑰加密,提供很多接口。假如你項目中需要加密過程,可以稍加改進為你所用
  */
  import java.security.*;
  import java.security.spec.*;
  import javax.crypto.*;
  import javax.crypto.spec.*;
  import java.io.*;
  import java.util.*;
  
  
  public class FileEncryptorRSA {
  
  
  private static final int ITERATIONS=1000;//計算次數,在加鹽中用到
  private static byte[] publicKeyBytes;//公鑰
  private static byte[] privateKeyBytes;//私鑰
  private static String sessionKey;//會話密鑰
  public static String ENCRYPT_PRIVATEKEY_FILE="1.txt";//該文件放置加密的私鑰
  private static String TEXT_FILE="4.txt";//要加密的文件
  private static String ENCRPTOR_TEXT_FILE="5.txt";//被加密后的文件
  private static String DENCRYPTOR_TEXT_FILE="6.txt";//解密后的文件
  private static String password="liufeng";//口令用于加密私鑰
  
  
  public void setTEXT_FILE(String fileName){
  TEXT_FILE=fileName;
  }
  public void setENCRYPT_PRIVATEKEY_FILE(String fileName){
   ENCRYPT_PRIVATEKEY_FILE=fileName;
   }
   public String getENCRYPT_PRIVATEKEY_FILE(){
   return ENCRYPT_PRIVATEKEY_FILE;
   }
  
  public void setENCRPTOR_TEXT_FILE(String fileName){
  ENCRPTOR_TEXT_FILE=fileName;
  }
  public String getENCRPTOR_TEXT_FILE(){
  return ENCRPTOR_TEXT_FILE;
  }
  public void setDENCRYPTOR_TEXT_FILE(String fileName){
   DENCRYPTOR_TEXT_FILE=fileName;
   }
   public String getDENCRYPTOR_TEXT_FILE(){
   return DENCRYPTOR_TEXT_FILE;
   }
  public void setPassword(String password){
  this.password=password;
  }
  
  
  //create a RSA secretKey
   public static void createKey()throws Exception{
  KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
  keyPairGenerator.initialize(1024);
  KeyPair keyPair=keyPairGenerator.genKeyPair();
  //得到公鑰的字節數組
   publicKeyBytes=keyPair.getPublic().getEncoded();
  //得到私鑰
   byte[] privateKeyBytes=keyPair.getPrivate().getEncoded();
   byte[] encrytedPrivatekey=passwordEncrypt(password.toCharArray(),privateKeyBytes);
   FileOutputStream fos=new FileOutputStream(ENCRYPT_PRIVATEKEY_FILE);
   fos.write(encrytedPrivatekey);
   fos.close();
   }
  
  
  
  //通過給的口令加密私鑰
   private static byte[] passwordEncrypt(char[] password,byte[] privateKeyBytes)
   throws Exception{
   //create 8 byte salt
   byte[] salt=new byte[8];
   Random random=new Random();
   random.nextBytes(salt);
   //create a PBE key and cipher
   PBEKeySpec keySpec=new PBEKeySpec(password);
   SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
   SecretKey key=keyFactory.generateSecret(keySpec);
   PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
   Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
   cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec);
   //Encrypt the byte[]
   byte[] cipherPriKey=cipher.doFinal(privateKeyBytes);
   //write out salt ,and then the cipherPriKey
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   baos.write(salt);
   baos.write(cipherPriKey);
   return baos.toByteArray();
   }
  
  
  
  //用會話密鑰加密給定的文件,然后用公鑰加密會話密鑰,并存入文件中
  //最后加密后的文件由密鑰長度+已加密的密鑰(會話密鑰)+密文
   public static void encrypt()throws Exception{
  
   //轉換成RSA密鑰
   X509EncodedKeySpec keySpec=new X509EncodedKeySpec(publicKeyBytes);
   KeyFactory keyFactory=KeyFactory.getInstance("RSA");
   PublicKey publickey=keyFactory.generatePublic(keySpec);
   //打開存貯密文的文件
   DataOutputStream output=new DataOutputStream(new FileOutputStream(ENCRPTOR_TEXT_FILE));
   //創建RSA的CIpher
   Cipher rsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
   rsaCipher.init(Cipher.ENCRYPT_MODE,publickey);
   //創建會話密鑰(Rijndael)
   KeyGenerator rijndaelKeyGenerator=KeyGenerator.getInstance("Rijndael");
   rijndaelKeyGenerator.init(256);
   Key rijndaelKey=rijndaelKeyGenerator.generateKey();
   //公鑰加密會話密鑰
   byte[] encodedKeyBytes=rsaCipher.doFinal(rijndaelKey.getEncoded());
   output.writeInt(encodedKeyBytes.length);
   output.write(encodedKeyBytes);
   //產生IV向量
   SecureRandom random=new SecureRandom();
   byte[] iv=new byte[16];
   random.nextBytes(iv);
   output.write(iv);
  
   //加密正文
   IvParameterSpec spec=new IvParameterSpec(iv);
   Cipher symmetricCipher=Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
   symmetricCipher.init(Cipher.ENCRYPT_MODE,rijndaelKey,spec);
   CipherOutputStream cos=new CipherOutputStream(output,symmetricCipher);
   FileInputStream input=new FileInputStream(TEXT_FILE);
  
   int theByte=0;
   while((theByte=input.read())!=-1){
   cos.write(theByte);
   }
   input.close();
   cos.close();
   return;
   }
  
  
  
  //得到私鑰
   private static byte[] passwordDecrypt(char[] password,byte[] ciphertext)
   throws Exception{
   byte[] salt=new byte[8];
   ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
   bais.read(salt,0,8);
   byte[] remainingCiphertext=new byte[ciphertext.length-8];
   bais.read(remainingCiphertext,0,ciphertext.length-8);
   PBEKeySpec keySpec=new PBEKeySpec(password);
   SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
   SecretKey key=keyFactory.generateSecret(keySpec);
   PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
   Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
   cipher.init(Cipher.DECRYPT_MODE,key,paramSpec);
   return cipher.doFinal(remainingCiphertext);
   }
  
  
  //解密加密的文件
   public static void decrypt()
   throws Exception{
   FileInputStream fis=new FileInputStream(ENCRYPT_PRIVATEKEY_FILE);
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   int theByte=0;
   while((theByte=fis.read())!=-1){
   baos.writ

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邹平县| 法库县| 临颍县| 昔阳县| 垣曲县| 白河县| 南充市| 平度市| 东至县| 宿州市| 遂平县| 中江县| 河源市| 砀山县| 门源| 民和| 项城市| 甘南县| 刚察县| 开封市| 于田县| 启东市| 铜川市| 上杭县| 濮阳县| 来凤县| 浦城县| 苍南县| 会昌县| 耒阳市| 诸暨市| 山东| 天峻县| 梁平县| 聊城市| 阿拉善盟| 阳谷县| 平果县| 彭山县| 清涧县| 库尔勒市|