Results 1 to 2 of 2
  1. #1
    kinnylaw is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Replies: 8
    Last Post: 11-06-2009, 06:52 AM
  3. Replies: 15
    Last Post: 03-18-2009, 10:59 PM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •