[ vtl_design @ 04.06.2009. 00:00 ] @
Pozdrav, Patim se vec danima nisam nesto dobar sa javom ali jako mi je ovo potrebno. Da li mi neko moze modifikovat code ispod za ova 4 fajla, da mi kao input ne cita iz datoteke vec da bude obicni String, dakle da i ne cita i ne upisuje nista u/iz datoteke vec cita iz obicnog Stringa a ispisuje u System.out.println(); Hvala puno! Code: Demo.java --------------- package kripto; import java.math.*; /** * <p>Title:Class Demo</p> * <p>Description: This class facilitates testing the random generation * of the keys, and the encryption and decryption of text</p> * <p>Author: Radhika Reddy</p> */ public class Demo{ /** * Main class to test RSA algorithm. */ public static void main (String args[]) { RSA rsa = new RSA(); Encrypt encrypt = new Encrypt(rsa.getPublicKey(), rsa.getN()); Decrypt decrypt = new Decrypt(rsa.getPrivateKey(), rsa.getN()); } } Encrypt.java ------------------------- package kripto; import java.math.*; /** * <p>Title:Class Encrypt</p> * <p>Description: This class encrypts a plaintext with the given * public keys. (d,n) </p> * <p>Author: Radhika Reddy</p> */ public class Encrypt { /** * Default Class Constructor. * Uses predefined public keys. */ public Encrypt(){ /** * Following keys were generated at http://crypto.cs.mcgill.ca/~crepeau/RSA/generator_frame.html */ publickey_e = new BigInteger("61404965926479355855905591146111446036174180344042322260881284389185001535129"); publickey_n = new BigInteger("87804687216282445014344449262787492088471510759278970766557708167685450735849"); doEncryption(); } /** * Class Constructor with keys provided. * This method takes two parameters (public keys e and n) * and encrypts the plaintext stored by default in file "input.txt" * The default file with the ciphertext is "encrypt.txt". * @param e public key * @param n public key */ public Encrypt(BigInteger e, BigInteger n){ //set the public keys based on input publickey_e = e; publickey_n = n; doEncryption(); } /** * Class Constructor with keys and input filename provided. * This method takes three parameters (input filepath, and * public keys e and n). It encrypts the input text using the * keys and writes the output to file "encrypt.txt". * @param filename Input/Plaintext filepath * @param e public key * @param n public key */ public Encrypt(String filename, String e, String n){ //set the public keys based on input publickey_e = new BigInteger(e); publickey_n = new BigInteger(n); plaintxt = filename; doEncryption(); } /** * This method performs the main actions of calling the IO class * and encrypting the plaintext */ public void doEncryption(){ //perform IO IO io = new IO(plaintxt,ciphertxt); //read the plaintext file io.openPlaintext(); String temp_str = new String(); //read each line of plaintext and encode it with the keys provided while( (temp_str = io.nextLine()) != null){ //use 16 character chunks to encode data int chunks = (temp_str.length()/16) + 1; String arr[] = io.breakUpString(temp_str,chunks); // For each block, encrypt the message for(int i = 0; i < arr.length; i++){ BigInteger ascii_val = new BigInteger(io.encodeString(arr[i])); //write encoded text to file "encrypt.txt" io.writeCipher(io.encryptText(ascii_val,publickey_e,publickey_n)); } } io.closeFile(); //close output file } //end doEncryption /** * Main function to run the Encryption program directly */ public static void main (String args[]) { int len; len = args.length; if(len != 3){ System.out.println("No arguments provided. Default keys and files will be used."); System.out.println("Command format is <Encrypt plaintext publickey_e publickey_n"); Encrypt encrypt = new Encrypt(); } else { Encrypt encrypt = new Encrypt(args[0], args[1], args[2]); //user input } } //end main /* * Data Members */ private BigInteger publickey_e; private BigInteger publickey_n; //default filenames for ciphertext and final output private String plaintxt = "input.txt"; private String ciphertxt = "encrypt.txt"; } //end class Encrypt Decrypt.java --------------- package kripto; import java.math.*; /** * <p>Title:Class Decrypt</p> * <p>Description: This class decrypts a ciphertext with the given * public and private keys. (d,n)</p> * <p>Author: Radhika Reddy</p> */ public class Decrypt{ /** * Default Class Constructor. * Uses predefined public and private keys. */ public Decrypt(){ /** * Following keys were generated at http://crypto.cs.mcgill.ca/~crepeau/RSA/generator_frame.html */ publickey_n = new BigInteger("87804687216282445014344449262787492088471510759278970766557708167685450735849"); publickey_d = new BigInteger("59773221267454873409080055372070998244169751182929627179176156213454118330409"); doDecryption(); } /** * Class Constructor. * The method takes two parameters (private and public key) * and decrypts the cipher text created by the encryption * class Encrypt. The default file with the ciphertext is * "encrypt.txt". The default output goes to "decrypt.txt" * @param d private key * @param n public key */ public Decrypt(BigInteger d, BigInteger n){ //set the private and public keys based on input publickey_d = d; publickey_n = n; doDecryption(); } /** * Class Constructor with keys and input filename provided. * This method takes three parameters (input filepath, and * public key n and private key d). It decrypts the ciphertext using the * keys and writes the output to file "decrypt.txt". * @param filename Input/Plaintext filepath * @param d private key * @param n public key */ public Decrypt(String filename, String d, String n){ //set the private and public keys based on input publickey_d = new BigInteger(d); publickey_n = new BigInteger(n); ciphertxt = filename; doDecryption(); } /** * This method performs the main actions of calling the IO class * and decrypting the ciphertext */ public void doDecryption(){ String temp_str = new String(); //perform IO IO io = new IO(ciphertxt, outtxt); //read the ciphertext file io.openCipher(); //read each line of cipher text and decode it with the keys provided while( (temp_str = io.nextLine()) != null) { BigInteger ciphertext = new BigInteger(temp_str); String x = io.decryptText(ciphertext, publickey_d, publickey_n); //write decoded text to output file io.writeOut(io.decodeString(x)); } io.closeFile(); //close output file } //end doDecryption /** * Main function to run the Decryption program directly */ public static void main (String args[]) { int len; len = args.length; if(len != 3){ System.out.println("No arguments provided. Default keys and files will be used."); System.out.println("Command format is <Encrypt plaintext privatekey_d publickey_n"); Decrypt decrypt = new Decrypt(); } else { Decrypt decrypt = new Decrypt(args[0], args[1], args[2]); //user input } } //end main /* * Data Members */ private BigInteger publickey_d; private BigInteger publickey_n; //default filenames for ciphertext and final output private String ciphertxt = "encrypt.txt"; private String outtxt = "decrypt.txt"; } //end class Decrypt OI.java ------------- package kripto; import java.io.*; import java.lang.Byte; import java.math.*; /** * <p>Title:Class IO</p> * <p>Description: This class handles input-output for testing * the RSA algorithm</p> * <p>Author: Radhika Reddy</p> */ public class IO{ /** * Default constructor. * Sets the input and output files to the defualt names * "input.txt" and "encrypt.txt" */ public IO(){ input = new File(in); output = new File(out); } /** * Constructor that accepts the input and output filenames. * @param in_file Input filename * @param out_file Output filename */ public IO(String in_file, String out_file){ in = in_file; out = out_file; } /** * Open the plain text file and the cipher text file for writing output. */ public void openPlaintext(){ //open plaintext file for encryption input = new File(in); //open ciphertext file for output output = new File(out); //read in input and prepare output file for writing try { reader = new FileReader(input); buffer = new BufferedReader(reader); writer = new FileWriter(output); } catch (FileNotFoundException fnfe) { System.out.println("File not found: " + fnfe.getMessage()); } catch (IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } } //end openPlaintext /** * Read next Line from input file * @return String containing the next line */ public String nextLine(){ try { aString = buffer.readLine(); } catch(IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } return aString; } //end nextLine /** * Write encoded text to the ciphertext file. * @param str String containing encoded text */ public void writeCipher(String str){ try { writer.write(str); writer.write("\r\n"); }catch (IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } } //end writeCipher /** * Write decoded text to the decrypted file. * @param str String containing decoded text */ public void writeOut(String str){ try { writer.write(str); }catch (IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } } //end writeOut /** * Open the ciphertext file for reading */ public void openCipher(){ input = new File(in); output = new File(out); //read in input and prepare output file for writing try { reader = new FileReader(input); buffer = new BufferedReader(reader); writer = new FileWriter(output); } catch (FileNotFoundException fnfe) { System.out.println("File not found: " + fnfe.getMessage()); } catch (IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } } //end openCipher /** * Close all open file that were written to. */ public void closeFile(){ //close the output file try { writer.close(); } catch(IOException ioe) { System.out.println("IOException: " + ioe.getMessage()); } //let the user know where we have written the file System.out.println("Output writen to " + out + "\n"); } //end closeFile /** * Method to divide each line read from the plaintext file * into 16 character blocks. * @param s String containing line read * @param chunks Array to hold each line, 16 chars at a time */ public static String[] breakUpString(String s, int chunks){ String temp[] = new String[chunks]; int first = 0, last = 16; if(last > s.length()){ last = s.length(); } //break each line into blocks on 16 chars each. for(int i = 0; i < chunks; i++){ temp[i] = s.substring(first,last); first = last; last += 16; if(last > s.length()) last = s.length(); } return temp; } //end breaUpString /** * Encrypt the text using E(p) = p^e mod n. * @param ascii_val Plaintext value in bytes * @param e Private key e * @param n Public Key n */ public String encryptText(BigInteger ascii_val, BigInteger e, BigInteger n){ if ( print_key_flag == 0){ System.out.println("Keys used for Encryption:\n"); System.out.println("Public key n: " + n); System.out.println("Private key e: " + e); print_key_flag++; } return (ascii_val.modPow(e,n)).toString(); } //end encryptText /** * Decrypt the text using D(c) = c^d mod n. * @param ciphertext Encoded text * @param d Private key d * @param n Public key n */ public String decryptText(BigInteger ciphertext, BigInteger d, BigInteger n){ if (print_key_flag == 0){ System.out.println("Keys used for Decryption:\n"); System.out.println("Public key n: " + n); System.out.println("Private key d: " + d); print_key_flag++; } return (ciphertext.modPow(d,n)).toString(); } //end decryptText /** * Convert the plaintext into it's corresponding byte value. * @param plaintext Plaintext as a string */ public String encodeString(String plaintext){ // Go through each character in the plaintext, and convert it to a byte (ascii value) // Then return the big integer representation (as a string) byte array[] = new byte[MAX_LENGTH]; for(int i = 0; i < plaintext.length(); i++){ array[15-i] = (byte)plaintext.charAt(i); } String encodedstring = ((new BigInteger(array)).toString()); return encodedstring; } //end encodeString /** * Convert the ciphertext into it's corresponding plaintext. * @param numerictext ASCII value of encoded text. */ public String decodeString(String numerictext) { // Create a big integer representation of our string BigInteger biginteger = new BigInteger(numerictext); BigInteger modvalue = new BigInteger("256"); byte array[] = new byte[MAX_LENGTH]; byte newarray[]; int strlen = MAX_LENGTH; // Store the byte ascii values into our array for(int i = 0; i < MAX_LENGTH; i++) { array[i] = (byte)(biginteger.mod(modvalue)).intValue(); biginteger = biginteger.divide(modvalue); } // Trim down the array while(array[strlen-1] == 0) { strlen--; } // Copy contents into the newarray and return the string object newarray = new byte[strlen]; for(int i = 0; i < strlen; i++){ newarray[i] = array[i]; } // Convert ascii values to a string and return the decoded string return new String(newarray); } //end decodeString //NOTE:The string break up logic was used from http://www.cs.colostate.edu/~pfau /** * Data members */ private File input,output; //input & output files private FileReader reader; //input buffer private FileWriter writer; //output buffer private BufferedReader buffer; //input buffer private static StringBuffer s = new StringBuffer(120); //buffer for name String in = "input.txt"; //default filenames String out = "encrypt.txt"; private String aString = " "; private final int MAX_LENGTH = 16; //Buffer size private int print_key_flag = 0; //Print keys only once } //end class IO |