Java実装ファイルの暗号解読機能例


本論文の実例はJava実装ファイルの暗号解読機能を皆さんに共有するための参考になります。具体的には以下の通りです。

package com.copy.encrypt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
  /**
   *   file    
   * @param fileUrl     
   * @param key   
   * @throws Exception
   */
  public static void encrypt(String fileUrl, String key) throws Exception {
    File file = new File(fileUrl);
    String path = file.getPath();
    if(!file.exists()){
      return;
    }
    int index = path.lastIndexOf("\\");
    String destFile = path.substring(0, index)+"\\"+"abc";
    File dest = new File(destFile);
    InputStream in = new FileInputStream(fileUrl);
    OutputStream out = new FileOutputStream(destFile);
    byte[] buffer = new byte[1024];
    int r;
    byte[] buffer2=new byte[1024];
    while (( r= in.read(buffer)) > 0) {
        for(int i=0;i<r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==255?0:++b;
        }
        out.write(buffer2, 0, r);
        out.flush();
    }
    in.close();
    out.close();
    file.delete();
    dest.renameTo(new File(fileUrl));
    appendMethodA(fileUrl, key);
    System.out.println("    ");
  }
  /**
   *
   * @param fileName
   * @param content   
   */
   public static void appendMethodA(String fileName, String content) {
      try {
        //            ,     
        RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
        //     ,   
        long fileLength = randomFile.length();
        //           。
        randomFile.seek(fileLength);
        randomFile.writeBytes(content);
        randomFile.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }
   /**
   *   
   * @param fileUrl    
   * @param tempUrl     
   * @param ketLength     
   * @return
   * @throws Exception
   */
   public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
      File file = new File(fileUrl);
      if (!file.exists()) {
        return null;
      }
      File dest = new File(tempUrl);
      if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
      }
      InputStream is = new FileInputStream(fileUrl);
      OutputStream out = new FileOutputStream(tempUrl);
      byte[] buffer = new byte[1024];
      byte[] buffer2=new byte[1024];
      byte bMax=(byte)255;
      long size = file.length() - keyLength;
      int mod = (int) (size%1024);
      int div = (int) (size>>10);
      int count = mod==0?div:(div+1);
      int k = 1, r;
      while ((k <= count && ( r = is.read(buffer)) > 0)) {
        if(mod != 0 && k==count) {
          r = mod;
        }
        for(int i = 0;i < r;i++)
        {
          byte b=buffer[i];
          buffer2[i]=b==0?bMax:--b;
        }
        out.write(buffer2, 0, r);
        k++;
      }
      out.close();
      is.close();
      return tempUrl;
    }
   /**
   *         
   * @param fileName
   * @return
   */
   public static String readFileLastByte(String fileName, int keyLength) {
     File file = new File(fileName);
     if(!file.exists())return null;
     StringBuffer str = new StringBuffer();
      try {
        //            ,     
        RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
        //     ,   
        long fileLength = randomFile.length();
        //           。
        for(int i = keyLength ; i>=1 ; i--){
          randomFile.seek(fileLength-i);
          str.append((char)randomFile.read());
        }
        randomFile.close();
        return str.toString();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null;
     }
}
PS:暗号解読に興味のある友達は、当駅のオンラインツールも参照できます。
テキストオンライン暗号解読ツール(AES、DES、RC 4などを含む):
http://tools.jb51.net/password/txt_アンコール
MD 5オンライン暗号化ツール:
http://tools.jb51.net/password/CreateMD5Password
オンラインハッシュ/ハッシュアルゴリズム暗号化ツール:
http://tools.jb51.net/password/hash_encrypt
オンラインMD 5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160暗号化ツール:
http://tools.jb51.net/password/hash_md 5_share
オンラインshar 1/shar 224/shar 256/shar 384/shar 512暗号化ツール:
http://tools.jb51.net/password/sha_アンコール
java関連の内容についてもっと興味がある読者は、当駅のテーマを調べてもいいです。「Java数学演算技術のまとめ」、「Javaデータ構造とアルゴリズム教程」、「Java文字と文字列操作テクニックのまとめ」、「java日付と時間操作テクニックのまとめ」、「Java操作DOMノード技術のまとめ」、「Javaキャッシュ操作テクニックのまとめ
本論文で述べたように、皆さんのjavaプログラムの設計に役に立ちます。