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

📄 ciphersaber.java

📁 java rc4 加密 rc4加密算法的一个实现
💻 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!!! * Based on AnotherExample.java */public class CipherSaber { 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[10];        try {            if (!decrypt) {                SecureRandom random = new SecureRandom();                random.nextBytes(sessionIV);                out.write(sessionIV);            } else {                in.read(sessionIV,0,10);            }        } catch (IOException ioe) {        }                RC4 rc4 = new RC4();                // create the key as password+iv        byte[] passBytes = password.getBytes();        byte[] sessionKey = new byte[passBytes.length+10];        // copy password        System.arraycopy(passBytes, 0, sessionKey, 0, passBytes.length);        //copy iv        System.arraycopy(sessionIV, 0, sessionKey, (sessionKey.length-10), sessionIV.length);                try {            rc4.engineInit(sessionKey);        } catch (Exception e) {        }                if (!decrypt) {            System.out.println("Encrypting...");        } else {            System.out.println("Decrypting...");        }                // 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 void usage() {        System.out.println("usage: [-e|-d password /file/to/(en|de)crypt /output/filename] [-h] [--help]");        System.out.println("ex   : java CipherSaber -e \"apassword\" ./File.txt ./File.enc");    }}

⌨️ 快捷键说明

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