publicexample.java

来自「这是一个文本加密、解密系统」· Java 代码 · 共 105 行

JAVA
105
字号
package example;

/**
*PublicExample.java
*Copyright 2005-2-16
*/
//import java.security.Key;
import javax.crypto.Cipher;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.security.KeyPairGenerator;
import java.security.KeyPair;
/**
*一个简单的公?加密例子,Cipher类使用KeyPairGenerator生成的公?和私?
*/
public class PublicExample
{
public static void main(String[] args) throws Exception
{
    File encryp_src_file = new File("test.txt");
    FileReader file_reader = new FileReader(encryp_src_file);
    BufferedReader buffer_reader = new BufferedReader(file_reader);
    
    //read the src file content into a string buffer
    StringBuffer strbuf = new StringBuffer();
    String s;
    while((s = buffer_reader.readLine()) != null)
    {
        strbuf.append(s);
        strbuf.append('\n');
    }
    buffer_reader.close();
    file_reader.close();
    
    

    byte[] plainText=strbuf.toString().getBytes("UTF-8");
    System.out.println("Initial plaintext's length are : " + plainText.length);
    //构成一个RSA密钥
    System.out.println("\nStart generating RSA key");
    KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(1024);
    KeyPair key=keyGen.generateKeyPair();
    System.out.println("Finish generating RSA key");

    //获得一个RSA的Cipher类,使用公?加密
    Cipher cipher=Cipher.getInstance("RSA");
    System.out.println("\n"+cipher.getProvider().getInfo());

    System.out.println("\nStart encryption");
    cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());
    byte[] cipherText=cipher.doFinal(plainText);
    System.out.println("cipherText's length are : " + cipherText.length);
    System.out.println("Finish encryption:");
    System.out.println(new String(cipherText,"UTF-8"));
    
    
    //write the cipher text in byte into the target file
    File dst_file = new File("e.txt");
    FileOutputStream fout = new FileOutputStream(dst_file);
    BufferedOutputStream buf_output = new BufferedOutputStream(fout);
    buf_output.write(cipherText, 0, cipherText.length);
    
    buf_output.close();
    fout.close();
    
    //read the cipher text from the e.txt file
    File src_file = new File("e.txt");
    FileInputStream fin = new FileInputStream(src_file);
    BufferedInputStream buf_input = new BufferedInputStream(fin);
    
    //read the src file content into a byte array instead of into a string buffer
    byte byte_array[] = new byte[buf_input.available()];
    buf_input.read(byte_array);
    buf_input.close();
    fin.close();
    

    //使用私?解密
    System.out.println("\nStart decryption");
    cipher=Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());
    byte[] newPlainText=cipher.doFinal(byte_array);
    System.out.println("newPlainText's length are : " + newPlainText.length);
    System.out.println("Finish decryption:");
    System.out.println(new String(newPlainText,"UTF-8"));
    
    
    //write the plain text in bytes into the target file
    File dst_file_2 = new File("d.txt");
    FileOutputStream fout_2 = new FileOutputStream(dst_file_2);
    BufferedOutputStream buf_output_2 = new BufferedOutputStream(fout_2);
    buf_output_2.write(newPlainText, 0, newPlainText.length);
    
    buf_output_2.close();
    fout_2.close();
    }
}

⌨️ 快捷键说明

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