Java Randomness Test Suite

JRandTest at SourceForge

com.fasteasytrade.JRandTest.Algo
Class RSACrypt

java.lang.Object
  extended bycom.fasteasytrade.JRandTest.Algo.Cipher
      extended bycom.fasteasytrade.JRandTest.Algo.RSACrypt

public class RSACrypt
extends Cipher

Java RSA algorithm implementation.

 2 Sep 2003: Zur Aougav
 2 Sep 2003: Copyright (c) 2003 Zur Aougav 
 2 Sep 2003: New RSA java class.
 2 Sep 2003: Use simple securerandom class to generate p & q primes.
 2 Sep 2003: Read and write biginteger to/from streams as object or simple outputstream (with length).
 2 Sep 2003: Vector encrypt(byte[] message) does not handle the last bytes properly. Buffer
		is filled with 0. Decryption will have 0 (nulls) at end of decoded string.
 3 Sep 2003: Add test in main: Shall I compare thee to a summer's day. Shakespeare.
 3 Sep 2003: Add save/loadPublicKey and save/loadPrivateKey.
 3 Sep 2003: Add -g option: to generate and print keys.
 3 Sep 2003: Add -gs option: to generate and save keys in rsapublic.key and rsaprivate.key.
 3 Sep 2003: Add encryptFile(infilename,outfilename) and decryptFile(infilename,outfilename).
 4 Sep 2003: Add GZIP support.
		encryptFileGzip(infilename,outfilename) and decryptFileGzip(infilename,outfilename).
 4 Sep 2003: Changed default e=3 to e=65537.
 17  Mar 2005: encrypt (0 || random 32 bits || data).
 

Author:
Zur Aougav

Field Summary
(package private)  BigInteger d
           
(package private)  BigInteger e
           
(package private)  int msglen
           
(package private)  BigInteger n
           
(package private)  Random rnd
           
 
Constructor Summary
RSACrypt()
          Almost null contructor
 
Method Summary
 BigInteger decrypt(BigInteger message)
          Decrypt one big integer
 void decryptFile(String infile, String outfile)
          decrypt infile to outfile.
 void decryptFileGzip(String infile, String outfile)
           
 BigInteger encrypt(BigInteger message)
          Encrypt one big integer
 Vector encrypt(byte[] message)
          encrypt byte[] and returns vector of bigintegers.
 Vector encrypt(byte[] message, int mlen)
          encrypt byte[] and returns vector of bigintegers.
 void encryptFile(String infile, String outfile)
          encrypt infile to outfile.
 void encryptFileGzip(String infile, String outfile)
          encrypt infile to outfile with compression.
 void generateKeys(int bitlen)
          Generate public and private keys based on two primes, p and q, we make public (n, e), and private (n, d).
 int getMessageLength()
          Returns message length in bytes.
 void loadPrivateKey(String infilename)
          Load private key from file
 void loadPublicKey(String infilename)
          Load public key from file
static void main(String[] args)
          Main to test several options of class
static BigInteger read(DataInputStream in)
          Read one big integer from inputstream.
static BigInteger read(ObjectInputStream in)
          Read one big integer from inputstream, as java object.
static BigInteger readBigInteger(GZIPInputStream in)
          Read one big integer from GZIPinputstream.
static int readInt(GZIPInputStream in)
          Read int from GZIPinputstream.
 void savePrivateKey(String outfilename)
          Save private key to file
 void savePublicKey(String outfilename)
          Save public key to file
static void test1()
          Just testing RSA class
static void usage()
          Print usage of main RSA class
static void write(BigInteger v, DataOutputStream out)
          Write one big integer to outputstream, including length.
static void write(BigInteger v, GZIPOutputStream out)
          Write one big integer to GZIPoutputstream, including length.
static void write(BigInteger v, ObjectOutputStream out)
          Write one big integer to outputstream, as java object.
static void write(int v, GZIPOutputStream out)
          Write int to GZIPOutputStream
 
Methods inherited from class com.fasteasytrade.JRandTest.Algo.Cipher
compareBytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

n

BigInteger n

d

BigInteger d

e

BigInteger e

msglen

int msglen

rnd

Random rnd
Constructor Detail

RSACrypt

public RSACrypt()
Almost null contructor

Method Detail

generateKeys

public void generateKeys(int bitlen)
Generate public and private keys based on two primes, p and q, we make public (n, e), and private (n, d). We discard p and q.


encrypt

public BigInteger encrypt(BigInteger message)
Encrypt one big integer


decrypt

public BigInteger decrypt(BigInteger message)
Decrypt one big integer


encrypt

public Vector encrypt(byte[] message)
encrypt byte[] and returns vector of bigintegers.


encrypt

public Vector encrypt(byte[] message,
                      int mlen)
encrypt byte[] and returns vector of bigintegers. encrypt just the first mlen bytes. Encrypt: byte 0 : 0 byte 1 - 4 = random number byte 5 .. getMessageLen-1 = data


encryptFile

public void encryptFile(String infile,
                        String outfile)
                 throws IOException
encrypt infile to outfile.

Throws:
IOException

decryptFile

public void decryptFile(String infile,
                        String outfile)
                 throws IOException
decrypt infile to outfile.

Throws:
IOException

encryptFileGzip

public void encryptFileGzip(String infile,
                            String outfile)
                     throws IOException
encrypt infile to outfile with compression.

Throws:
IOException

decryptFileGzip

public void decryptFileGzip(String infile,
                            String outfile)
                     throws IOException
Throws:
IOException

getMessageLength

public int getMessageLength()
Returns message length in bytes. Based on n.


write

public static void write(BigInteger v,
                         DataOutputStream out)
                  throws IOException
Write one big integer to outputstream, including length.

Throws:
IOException

write

public static void write(BigInteger v,
                         ObjectOutputStream out)
                  throws IOException
Write one big integer to outputstream, as java object.

Throws:
IOException

write

public static void write(int v,
                         GZIPOutputStream out)
                  throws IOException
Write int to GZIPOutputStream

Throws:
IOException

write

public static void write(BigInteger v,
                         GZIPOutputStream out)
                  throws IOException
Write one big integer to GZIPoutputstream, including length.

Throws:
IOException

read

public static BigInteger read(DataInputStream in)
                       throws IOException
Read one big integer from inputstream. First read length, second read bytes to build the new big integer.

Throws:
IOException

read

public static BigInteger read(ObjectInputStream in)
                       throws IOException,
                              ClassNotFoundException
Read one big integer from inputstream, as java object.

Throws:
IOException
ClassNotFoundException

readInt

public static int readInt(GZIPInputStream in)
                   throws IOException
Read int from GZIPinputstream.

Throws:
IOException

readBigInteger

public static BigInteger readBigInteger(GZIPInputStream in)
                                 throws IOException
Read one big integer from GZIPinputstream. First read length, second read bytes to build the new big integer.

Throws:
IOException

savePublicKey

public void savePublicKey(String outfilename)
                   throws IOException
Save public key to file

Throws:
IOException

loadPublicKey

public void loadPublicKey(String infilename)
                   throws IOException,
                          ClassNotFoundException
Load public key from file

Throws:
IOException
ClassNotFoundException

savePrivateKey

public void savePrivateKey(String outfilename)
                    throws IOException
Save private key to file

Throws:
IOException

loadPrivateKey

public void loadPrivateKey(String infilename)
                    throws IOException,
                           ClassNotFoundException
Load private key from file

Throws:
IOException
ClassNotFoundException

test1

public static void test1()
Just testing RSA class


usage

public static void usage()
Print usage of main RSA class


main

public static void main(String[] args)
Main to test several options of class


Java Randomness Test Suite

JRandTest at SourceForge

JRandTest at SourceForge
Copyright © 2005 Zur Aougav. All Rights Reserved.