Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2009, 05:47 AM
Member
 
Join Date: Nov 2009
Posts: 1
Rep Power: 0
kinnylaw is on a distinguished road
Default 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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-28-2009, 09:49 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,264
Rep Power: 3
JosAH is on a distinguished road
Default
String.substring(i, i+size)?

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert text records from text file into a DB nicedad Database 8 11-06-2009 07:52 AM
[SOLVED] Making a new folder, reading an entire file in one block read fogus New To Java 15 03-18-2009 11:59 PM
find and replace text from a text file gezzel New To Java 2 09-19-2008 05:04 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
count character in text file as input file aNNuur New To Java 0 06-18-2007 07:46 AM


All times are GMT +2. The time now is 01:16 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org