⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileencryption.java

📁 这是一个文本加密、解密系统
💻 JAVA
字号:
package example;

import java.io.*;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;


public class FileEncryption
{
    static byte buffer[];
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        // TODO Auto-generated method stub
        File file = new File("SingleTestcase.html");
        try 
        {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            try 
            {
                StringBuffer sb = new StringBuffer();
                String s;
                while((s = br.readLine()) != null)
                {
                    sb.append(s);
                    sb.append("\n");
                }
                
                br.close();
                
                //System.out.println("Input String is : " + s);
                System.out.println("\nStart generate AES key");
                KeyGenerator keyGen=KeyGenerator.getInstance("DES");
                keyGen.init(56);
                Key key=keyGen.generateKey();
                System.out.println("Finish generating AES key");
                //System.out.println("key is " + new String(key.getEncoded()));

                //获得一个私?加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
                Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
                
                //使用私?加密
                System.out.println("\nStart encryption:");
                cipher.init(Cipher.ENCRYPT_MODE,key);
                buffer = sb.toString().getBytes("UTF-8");
                byte[] cipherText=cipher.doFinal(buffer);
                System.out.println("Finish encryption:");
                
                FileWriter fw_c = new FileWriter("ciphertext.txt");
                BufferedWriter bw_c = new BufferedWriter(fw_c);
                bw_c.write(new String(cipherText, "UTF-8"));
                System.out.println("cipher text string length are " + new String(cipherText, "UTF-8").length());
                System.out.println("cipher text byte length are " + cipherText.length);
                
                bw_c.close();
                fw_c.close();
                
                String cipher_text = new String(cipherText, "UTF-8");
                
                //System.out.println(new String(cipherText,"UTF-8"));
                
                System.out.println("\nStart decryption:");
                cipher.init(Cipher.DECRYPT_MODE,key);
                byte[] newPlainText=cipher.doFinal(cipher_text.getBytes("UTF-8"));
                System.out.println("Finish decryption:");
                
                FileWriter fw_p = new FileWriter("plaintext.html");
                BufferedWriter bw_p = new BufferedWriter(fw_p);
                bw_p.write(new String(newPlainText, "UTF-8"));

                bw_p.close();
                fw_p.close();
                
                //System.out.println(new String(newPlainText,"UTF-8"));
            } 
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 
        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -