📄 main.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package DES;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/** * * @author Fredrik */public class Main { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException{ long plaintext,ciphertext,key; key=0x0101010101010101l; DES cipher=new DES(key); //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 = ((int) (fileSize / 8)) + 1 ; fileSize = 8 * block ; byte[] buff = new byte[fileSize]; byte[] temp = new byte[8]; long temp1; FileOutputStream out = null; FileInputStream in = new FileInputStream(sourceFile); in.read(buff); in.close(); //加密开始 long begin = System.currentTimeMillis();//计时开始 for(int j = 0 ; j<block ; j++){ temp1 = 0; for(int k=0; k<8; k++){ temp[k]= 0; } for(int k=0 ; k<8 ; k++) { temp[k] = buff[j*8+k]; } for(int k=0 ; k<8 ; k++){ temp1 += (((long)temp[k])&0x00000000000000ffl)<<(8*k); } //System.out.printf("%016x\n",temp1); ciphertext=cipher.encrypt(temp1); //System.out.printf("%016x\n",ciphertext); for(int k=0 ; k<8 ; k++) { temp[k] = (byte) ((ciphertext>>>(k*8))&0x00000000000000ffl); } temp1 = 0; /*for(int k=0 ; k<8 ; k++){ temp1 += (((long)temp[k])&0x00000000000000ffl)<<(8*k); //temp1 = temp[7]&0x00000000000000ffl<<14; //System.out.printf("%016x\n",temp1); }*/ //System.out.printf("%016x\n",ciphertext); for(int k=0 ; k<8 ; k++){ buff[j*8+k] = temp[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++){ temp1 = 0; for(int k=0; k<8; k++){ temp[k]= 0; } for(int k=0 ; k<8 ; k++) { temp[k] = buff[j*8+k]; } //temp1 = temp[7]&0x00000000000000ffl; //System.out.printf("%016x\n",temp1); //temp1 = 0; for(int k=0 ; k<8 ; k++){ temp1 += (((long)temp[k])&0x00000000000000ffl)<<(8*k); //System.out.printf("%016x\n",temp1); } plaintext=cipher.decrypt(temp1); //System.out.printf("%016x\n",plaintext); for(int k=0 ; k<8 ; k++) { temp[k] = (byte) ((plaintext>>>(k*8))&0x00000000000000ffl); } for(int k=0 ; k<8 ; k++){ buff[j*8+k] = temp[k]; } } out = new FileOutputStream(decryptfile); out.write(buff); out.close(); current = System.currentTimeMillis(); System.out.println((current - begin) + " ms");//计时结束 System.out.println("Decrypt Success!"); //run the first test message //plaintext=0x95F8A5E5DD31D900l; // ciphertext=cipher.encrypt(plaintext); //System.out.printf("key=%016x\tplaintext=%016x\tciphertext=%016x\n",key,plaintext,ciphertext); // long plain2=cipher.decrypt(ciphertext); // System.out.printf("key=%016x\tplaintext=%016x\tciphertext=%016x\n",key,plain2,ciphertext); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -