All pastes #743729 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-20 18:31 UTC
rendered paste body
import java.io.*;
import java.lang.Exception;


class Decrypt {

	public static void main (String args[])  throws java.lang.Exception {
		long			n, e, d, ptext;
		int				pos;
		FileReader		in;
		InputStream     encryptFile;
		BufferedReader	bufRead;
		Writer 			decryptFile = new FileWriter("decrypted");
		String			line;
		int				charPtext[] = new int[4];
		
		
		n = e = d = ptext = 0;
		
		if(args.length < 2) {
			System.out.println("Usage:  Decrypt <plaintext> <keyfile>\n");
			System.exit(-1);
		}
		
		
		//Read keys
		in = new FileReader(args[1]);
		bufRead = new BufferedReader(in);
		
		//Read n
		line = bufRead.readLine();
		n = Long.parseLong(line);
		
		//Read e
		line = bufRead.readLine();
		e = Long.parseLong(line);
		
		//Read d
		line = bufRead.readLine();
		d = Long.parseLong(line);
		
		System.out.println("Keys\n----\n");
		System.out.println("n: "+n+"\ne: "+e+"\nd: "+d+"\n");
		
		encryptFile = new FileInputStream(args[0]);
		//bufRead = new BufferedReader(in);
		
		//Read encrypted
		pos = 0;
		int temp = (int)encryptFile.read();
		while(temp != -1)
		{	
			// not sure why, but values above 128 are negative, 
			//if(temp < 0)	temp = 256 + temp;
			
			
			charPtext[pos%4] = temp;
			
			if(++pos == 4)
			{	System.out.println( charPtext[0] + " " + charPtext[1] + " " + charPtext[2] + " "  + charPtext[3]);
				Decrypt(charPtext, d, n, decryptFile);
				
				charPtext[0] = charPtext[1] = charPtext[2] =  charPtext[3] = 0;
				pos = 0;
			}
			temp = (int)encryptFile.read();
		}
		
		if(pos > 0)  // means there is still stuff in charater buffer, so flush it
		{	Decrypt(charPtext, e, n, decryptFile);
		}
		
		
		decryptFile.close();
		//System.out.println();
		//System.out.println(expMod(2, 8, 5));
	} // end main


	public static long expMod(long M, long d, long n) 
	{	String eBinString = Long.toBinaryString(d);
		int eBinStringLen = eBinString.length();
		long	D = 1;
		String 	bit;
		
		//System.out.println("eBinString = " + eBinString);
				
		for(int i = 0; i < eBinStringLen; i++) 
		{	bit = eBinString.substring(i,i+1);
			
			if(bit.equals("0")) 
			{	D = (D * D) % n;
				//System.out.println("0: "+tmp);
			}		
			else if(bit.equals("1")) 
			{	D = (D * D) % n;
				D = (D * M) % n;
				//System.out.println("1: "+tmp);
			}
		}
		return D;
	}


	public static void Decrypt(int[] M, long d, long n, Writer decryptFile) {
				
		//Convert to long
		//long v = ((int)M[0] * 65536) + ((int)M[1] * 256) + (int)M[2];
		
		String eb0 = padString(Long.toBinaryString(M[0]), -8, "0");
		String eb1 = padString(Long.toBinaryString(M[1]), -8, "0");
		String eb2 = padString(Long.toBinaryString(M[2]), -8, "0");
		String eb3 = padString(Long.toBinaryString(M[3]), -8, "0");
		
		
		//System.out.println(eb0 + " " + eb1 + " " + eb2 + " " + eb3);
		long v = Long.parseLong( eb0 + eb1 + eb2 + eb3  , 2);
		
		//System.out.println(v);
		

		long E = expMod(v, d, n);
		int dByte0, dByte1, dByte2;
		
		String encryptedString = padString(Long.toBinaryString(E), -24, "0");
		

		
		dByte0 = Integer.parseInt( encryptedString.substring(0,  7+1) , 2);
		dByte1 = Integer.parseInt( encryptedString.substring(8,  15+1) , 2);
		dByte2 = Integer.parseInt( encryptedString.substring(16, 23+1) , 2);
		
		System.out.print(M); 
		System.out.println(" " + v + " ^ " + d + " mod " + n + " = " + E + " \t: " + dByte0 + " " + dByte1 + " " + dByte2);
		
		// write encrytpedString to file
		try 
		{	decryptFile.write( dByte0 ); 
			decryptFile.write( dByte1 );  
			decryptFile.write( dByte2 );
	    } catch (IOException ioe) {}
		

	}
	
	  /* Pads out a string upto padlen with pad chars
	   * padString("123", -8, "A") => "AAAAA123"
	   * padString("123", 8, "A")  => "123AAAAA"
	   */ 
	private static String padString(Object str, int padlen, String pad)
	{
		String padding = new String();
	    int len = Math.abs(padlen) - str.toString().length();
	    if (len < 1)
	      return str.toString();
	    for (int i = 0 ; i < len ; ++i)
	      padding = padding + pad;

	    return (padlen < 0 ? padding + str : str + padding);
	  }
	



}// end class Decrypt