📄 anotherexample.java
字号:
import java.io.*;import java.security.MessageDigest;import java.security.SecureRandom;import java.security.NoSuchAlgorithmException;/** * @author reikon (Thomas Dixon) * This file is released into public domain. (no license) * PLEASE NOTE THAT USING STRINGS FOR PASSPHRASES IS INSECURE! * THIS IS DONE AS AN EXAMPLE ONLY!!! */public class AnotherExample { private static boolean decrypt = false; /** * @param args */ public static void main(String[] args) { if (args.length == 0) usage(); for (int i = 0; i < args.length; i++) { if (args[i].equals("-e")) { crypt(args[i+1],args[i+2],args[i+3]); i+=3; } else if (args[i].equals("-d")) { decrypt = true; crypt(args[i+1],args[i+2],args[i+3]); i+=3; } else if (args[i].equals("-h") || args[i].equals("-help") || args[i].equals("--help")) { usage(); } else { usage(); } } } private static void crypt(String password, String infile, String outfile) { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(new File(infile)); } catch (IOException ioe) { System.err.println("File " + infile + " not found... Please check your spelling and try again.\n"); System.exit(-1); } try { if (new File(outfile).exists()) { System.out.println("File: " + outfile + " already exists. Overwrite? [Y/n]"); if ((char)System.in.read() == ('n' | 'N')) System.exit(-1); } out = new FileOutputStream(new File(outfile)); } catch (IOException ioe) { System.err.println("Unable to create file: " + outfile + ": Ensure you have proper permissions to create this file."); System.exit(-1); } // create or read IV byte[] sessionIV = new byte[8]; try { if (!decrypt) { SecureRandom random = new SecureRandom(); random.nextBytes(sessionIV); out.write(sessionIV); } else { in.read(sessionIV,0,8); } } catch (IOException ioe) { } RC4 rc4 = new RC4(); // create the key byte[] sessionKey = myKeyGen("SHA1", password.getBytes(), sessionIV, 65000, 160); try { rc4.engineInit(sessionKey); } catch (Exception e) { } if (!decrypt) { System.out.println("Encrypting..."); } else { System.out.println("Decrypting..."); } // initialize the cipher properly byte[] dummyBlock = new byte[256]; for (int i = 0; i < 256; i++) dummyBlock[i] = (byte)0x00; for (int i = 0; i < 16; i++) rc4.engineCrypt(dummyBlock,0); // encipher try { byte[] rawbuf = new byte[1024]; int numRead = 0; while ((numRead = in.read(rawbuf)) >= 0) { byte[] buf = new byte[numRead]; System.arraycopy(rawbuf,0,buf,0,numRead); out.write(rc4.engineCrypt(buf,0)); } in.close(); out.close(); } catch (IOException ioe) { System.err.println("FATAL: IO Error "+ioe); System.exit(-1); } System.out.println("Done"); } private static byte[] myKeyGen(String mdStr, byte[] password, byte[] keysalt, int iteration, int keysize) { // bits to bytes. keysize = (keysize / 8); MessageDigest md = null; try { // Get the message digest. md = MessageDigest.getInstance(mdStr); } catch (NoSuchAlgorithmException e) { System.out.println(e); } // concatenate password and salt. byte[] pwAndSalt = new byte[password.length+keysalt.length]; System.arraycopy(password, 0, pwAndSalt, 0, password.length); System.arraycopy(keysalt, 0, pwAndSalt, password.length, keysalt.length); byte[] hash = pwAndSalt; // Create the key as md(md(md(md(...(pw+salt))...) for (int i = 0; i < iteration; i++) { md.update(hash); hash = md.digest(); } byte[] key = new byte[keysize]; System.arraycopy(hash, 0, key, 0, keysize); return key; } private static void usage() { System.out.println("usage: [-e|-d password /file/to/(en|de)crypt /output/filename] [-h] [--help]"); System.out.println("ex : java AnotherExample -e \"apassword\" ./File.txt ./File.enc"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -