熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

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

2022-06-13   來源: Java核心技術 

  //package
  /*
  運行本程序你需要下載JCEBouncy Castle的JCE with Provider and Lightweight API
   網止是
   配置如下
   在WINDOWS中你需要把下載的bcprovjdkjar文件拷貝到兩個地方
   一個在你安裝的JDK目錄中比如說我的是C:\jsdkrc\jre\lib\ext
   另一個在你的JDK運行環境中我的是在C:\Program Files\Java\jrerc\lib\ext
   另外還要在對兩個javasecurity進行修改
   我的在 C:\jsdkrc\jre\lib\security\javasecurity
   C:\Program Files\Java\jrerc\lib\security\javasecurity;
   在javasecurity中加入
   securityprovider=orgbouncycastlejceproviderBouncyCastleProvider
   如果一切順利你就可以運行本程序了
  
   該程序具有對你的文件加解密功能需要你指定的數據程序中已給了接口
   比如說你指定了要加密的文件名txt加密後的文件存放位置txt
   還有口令password如liufeng運行該程序那麼txt 中將是txt的密文
   注意口令是解密的鑰匙不要忘記
   其他解密過程自己參考
  
   本程序利用會話密鑰加密提供很多接口如果你項目中需要加密過程可以稍加改進為你所用
  */
  import javasecurity*;
  import javasecurityspec*;
  import javaxcrypto*;
  import javaxcryptospec*;
  import javaio*;
  import javautil*;
  
  
  public class FileEncryptorRSA {
  
  
  private static final int ITERATIONS=;//計算次數在加鹽中用到
  private static byte[] publicKeyBytes;//公鑰
  private static byte[] privateKeyBytes;//私鑰
  private static String SessionKey;//會話密鑰
  public static String ENCRYPT_PRIVATEKEY_FILE=txt;//該文件放置加密的私鑰
  private static String TEXT_FILE=txt;//要加密的文件
  private static String ENCRPTOR_TEXT_FILE=txt;//被加密後的文件
  private static String DENCRYPTOR_TEXT_FILE=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){
  thispassword=password;
  }
  
  
  //create a RSA secretKey
   public static void createKey()throws Exception{
  KeyPairGenerator keyPairGenerator=KeyPairGeneratorgetInstance(RSA);
  keyPairGeneratorinitialize();
  KeyPair keyPair=keyPairGeneratorgenKeyPair();
  //得到公鑰的字節數組
   publicKeyBytes=keyPairgetPublic()getEncoded();
  //得到私鑰
   byte[] privateKeyBytes=keyPairgetPrivate()getEncoded();
   byte[] encrytedPrivatekey=passwordEncrypt(passwordtoCharArray()privateKeyBytes);
   FileOutputStream fos=new FileOutputStream(ENCRYPT_PRIVATEKEY_FILE);
   foswrite(encrytedPrivatekey);
   fosclose();
   }
  
  
  
  //通過給的口令加密私鑰
   private static byte[] passwordEncrypt(char[] passwordbyte[] privateKeyBytes)
   throws Exception{
   //create byte salt
   byte[] salt=new byte[];
   Random random=new Random();
   randomnextBytes(salt);
   //create a PBE key and cipher
   PBEKeySpec keySpec=new PBEKeySpec(password);
   SecretKeyFactory keyFactory=SecretKeyFactorygetInstance(PBEWithSHAAndTwofishCBC);
   SecretKey key=keyFactorygenerateSecret(keySpec);
   PBEParameterSpec paramSpec=new PBEParameterSpec(saltITERATIONS);
   Cipher cipher=CiphergetInstance(PBEWithSHAAndTwofishCBC);
   cipherinit(CipherENCRYPT_MODEkeyparamSpec);
   //Encrypt the byte[]
   byte[] cipherPriKey=cipherdoFinal(privateKeyBytes);
   //write out salt and then the cipherPriKey
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   baoswrite(salt);
   baoswrite(cipherPriKey);
   return baostoByteArray();
   }
  
  
  
  //用會話密鑰加密給定的文件然後用公鑰加密會話密鑰並存入文件中
  //最後加密後的文件由密鑰長度+已加密的密鑰(會話密鑰)+密文
   public static void encrypt()throws Exception{
  
   //轉換成RSA密鑰
   XEncodedKeySpec keySpec=new XEncodedKeySpec(publicKeyBytes);
   KeyFactory keyFactory=KeyFactorygetInstance(RSA);
   PublicKey publickey=keyFactorygeneratePublic(keySpec);
   //打開存貯密文的文件
   DataOutputStream output=new DataOutputStream(new FileOutputStream(ENCRPTOR_TEXT_FILE));
   //創建RSA的CIpher
   Cipher rsaCipher=CiphergetInstance(RSA/ECB/PKCSPadding);
   rsaCipherinit(CipherENCRYPT_MODEpublickey);
   //創建會話密鑰(Rijndael)
   KeyGenerator rijndaelKeyGenerator=KeyGeneratorgetInstance(Rijndael);
   rijndaelKeyGeneratorinit();
   Key rijndaelKey=rijndaelKeyGeneratorgenerateKey();
   //公鑰加密會話密鑰
   byte[] encodedKeyBytes=rsaCipherdoFinal(rijndaelKeygetEncoded());
   outputwriteInt(encodedKeyByteslength);
   outputwrite(encodedKeyBytes);
   //產生IV向量
   SecureRandom random=new SecureRandom();
   byte[] iv=new byte[];
   randomnextBytes(iv);
   outputwrite(iv);
  
   //加密正文
   IvParameterSpec spec=new IvParameterSpec(iv);
   Cipher symmetricCipher=CiphergetInstance(Rijndael/CBC/PKCSPadding);
   symmetricCipherinit(CipherENCRYPT_MODErijndaelKeyspec);
   CipherOutputStream cos=new CipherOutputStream(outputsymmetricCipher);
   FileInputStream input=new FileInputStream(TEXT_FILE);
  
   int theByte=;
   while((theByte=inputread())!=){
   coswrite(theByte);
   }
   inputclose();
   cosclose();
   return;
   }
  
  
  
  //得到私鑰
   private static byte[] passwordDecrypt(char[] passwordbyte[] ciphertext)
   throws Exception{
   byte[] salt=new byte[];
   ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
   baisread(salt);
   byte[] remainingCiphertext=new byte[ciphertextlength];
   baisread(remainingCiphertextciphertextlength);
   PBEKeySpec keySpec=new PBEKeySpec(password);
   SecretKeyFactory keyFactory=SecretKeyFactorygetInstance(PBEWithSHAAndTwofishCBC);
   SecretKey key=keyFactorygenerateSecret(keySpec);
   PBEParameterSpec paramSpec=new PBEParameterSpec(saltITERATIONS);
   Cipher cipher=CiphergetInstance(PBEWithSHAAndTwofishCBC);
   cipherinit(CipherDECRYPT_MODEkeyparamSpec);
   return cipherdoFinal(remainingCiphertext);
   }
  
  
  //解密加密的文件
   public static void decrypt()
   throws Exception{
   FileInputStream fis=new FileInputStream(ENCRYPT_PRIVATEKEY_FILE);
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   int theByte=;
   while((theByte=fisread())!=){
   baoswrit
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26781.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.