📄 main.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package RSA;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.math.BigInteger;/** * * @author Fredrik */public class Main { /** * @param args the command line arguments */ public static void main(String[] args)throws IOException { RSA myRSA=new RSA(256); //performs three encryptions and decryptions //the values are taken from http://www.skepticfiles.org/faq/testdes.htm String filename = "D:\\My Documents\\test.JPG"; File sourceFile = new File(filename); String enfile = filename + ".encrypt"; File encryptfile = new File(enfile); int fileSize = (int) (sourceFile.length()); int block ; if(fileSize%31==0) block= ((int) (fileSize / 31)); else block= ((int) (fileSize / 31))+1; fileSize = 32 * block ; byte[] buff = new byte[fileSize]; byte[] temp = new byte[32]; //System.out.println(block); FileOutputStream out = null; FileInputStream in = new FileInputStream(sourceFile); in.read(buff); in.close(); //加密开始 long begin = System.currentTimeMillis();//计时开始 for(int j = 0 ; j<block ; j++){ for(int k=0 ; k<31 ; k++) { temp[k] = buff[j*31+k]; } BigInteger bigtemp = new BigInteger(temp); //System.out.printf("%02x\n",temp); BigInteger ciphertext=myRSA.encrypt(bigtemp); temp = ciphertext.toByteArray(); for(int k=0 ; k<31 ; k++){ buff[j*31+k] = temp[k]; //System.out.println(k); } } out = new FileOutputStream(encryptfile); out.write(buff); out.close(); long current = System.currentTimeMillis(); System.out.println((current - begin) + " ms");//计时结束 System.out.println("Encrypt Success!"); //加密结束 in = new FileInputStream(encryptfile); in.read(buff); in.close(); String defile = filename + ".decrypt"; File decryptfile = new File(defile); //解密开始 begin = System.currentTimeMillis();//计时开始 for(int j = 0 ; j<block ; j++){ for(int k=0 ; k<31 ; k++) { temp[k] = buff[j*31+k]; } BigInteger bigtemp = new BigInteger(temp); //System.out.printf("%02x\n",temp); BigInteger ciphertext=myRSA.decrypt(bigtemp); temp = ciphertext.toByteArray(); for(int k=0 ; k<31 ; k++){ buff[j*31+k] = temp[k]; } } /*bigtemp = new BigInteger(buff); ciphertext=myRSA.decrypt(bigtemp); buff = ciphertext.toByteArray();*/ out = new FileOutputStream(decryptfile); out.write(buff); out.close(); current = System.currentTimeMillis(); System.out.println((current - begin) + " ms");//计时结束 System.out.println("Decrypt Success!"); /* BigInteger[] plaintext={BigInteger.valueOf(10),BigInteger.valueOf(20),BigInteger.valueOf(30)}; for(int i=0;i<plaintext.length;i++) System.out.printf("%02x ", plaintext[i]); System.out.println(); BigInteger[] ciphertext=myRSA.encrypt(plaintext); for(int i=0;i<ciphertext.length;i++) System.out.printf("%02x ", ciphertext[i]); System.out.println(); BigInteger[] decrypted=myRSA.decrypt(ciphertext); for(int i=0;i<decrypted.length;i++) System.out.printf("%02x ", decrypted[i]); System.out.println();*/ }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -