Results 1 to 2 of 2
- 11-28-2009, 04:47 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
How to break a text file into block.
I'm currently working on a lab assignment for school, it involves with encrypting a text file with RSA encryption/decryption. Now the problem is I need to break the text file into block before encrypting it, so that the block size is not bigger than the size of the modulus value. My question is how would you break the file into several small block. The following is the code provided as reference to implement my code.
import java.math.BigInteger;
import java.io.*;
import java.util.Random;
class RSA {
static public void main(String args[]){
int KEY_SIZE = 128;
// Create a BigInteger constant for the integer ’1’.
BigInteger one = new BigInteger("1");
// Determine a modulus of sufficient size (based on the size of P and Q).
BigInteger p = new BigInteger(KEY_SIZE,20, new Random());
BigInteger q = new BigInteger(KEY_SIZE,20, new Random());
BigInteger n = p.multiply(q);
// Calculate PHI(n) for use in determining the public and private exponents.
BigInteger phi = p.subtract(one).multiply(q.subtract(one));
// Select a public exponent (repeat until GCD condition is met).
BigInteger e = new BigInteger(32,4, new Random());
BigInteger gcd = phi.gcd(e);
while (!gcd.equals(one)) {
e = new BigInteger(32,4, new Random());
gcd = phi.gcd(e);
}
// Calculate the private exponent.
BigInteger d = e.modInverse(phi);
// Print out all pertinent data:
System.out.println("Key Length = " + KEY_SIZE);
System.out.println();
System.out.println("P = " + p);
System.out.println("Q = " + q);
System.out.println("Modulus n = " + n);
System.out.println("PHI = " + phi);
System.out.println("Public Exponent e = " + e);
System.out.println("Private Exponent d = " + d);
System.out.println("d e mod phi = " + d.multiply(e).mod(phi));
// Create message to be encoded as a string!!
String messageString;
try{
messageString = args[0];
}
catch(ArrayIndexOutOfBoundsException error){
messageString = "RSA is cool";
}
// Create a simple BigInteger message using the constructor for Strings
// representing numbers.
byte[] byteArray = messageString.getBytes();
BigInteger messageAsBigInt = new BigInteger(byteArray);
// Encrypt and decrypt the message text.
BigInteger cypherAsBigInt = messageAsBigInt.modPow(e,n);
BigInteger decypherAsBigInt = cypherAsBigInt.modPow(d,n);
byte[] byteArrayBack = messageAsBigInt.toByteArray();
String messageStringBack = new String(byteArrayBack);
System.out.println("The messageString is : " + messageString);
System.out.println("The message as messageAsBigInt : " + messageAsBigInt);
System.out.println("The cyphertext cypherAsBigInt is : " + cypherAsBigInt);
System.out.println("The decrypted cipher decypherAsBigInt is: " + decypherAsBigInt);
System.out.println("The messageStringBack is : " + messageStringBack);
} // main()
} // RSA
- 11-28-2009, 08:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Insert text records from text file into a DB
By nicedad in forum JDBCReplies: 8Last Post: 11-06-2009, 06:52 AM -
[SOLVED] Making a new folder, reading an entire file in one block read
By fogus in forum New To JavaReplies: 15Last Post: 03-18-2009, 10:59 PM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks