Main.java
  1 /*
  2  * Main.java
  3  *
  4  * @author Alexander Scheidl
  5  *
  6  */
  7 package testrsa;
  8 
  9 import java.security.NoSuchAlgorithmException;
 10 import java.util.Enumeration;
 11 import java.util.Vector;
 12 
 13 public class Main {
 14 
 15     /** Creates a new instance of Main */
 16     public Main() {
 17     }
 18 
 19     /**
 20      * @param args the command line arguments
 21      */
 22     public static void main(String[] args) {
 23 
 24 
 25         int hauptlauf = 1; // determines the max of loops for the program, with every loop the key
 26         // will be increase by 1 until 1024, then he starts by 256 again.
 27         int lauflänge = 10000; // determines the maxlength of the strings to encode and decode
 28         int schrittlänge = 1000; // determines the run length of the strings to be created
 29         // expample: läuflänge = 2000; schrittlänge = 100; means 21 Strings
 30         // with 1 to 2001 char with 100' steps (1, 101, 201... 2001)
 31         int keyindex = 255; // index for the key length (standard: 256)
 32         // key shoud not be smaller than 129 because N must be > 2^128 (16 Byte restriction)
 33         Counter richtig = new Counter(); // create instance of a counter for right en/decode
 34         Counter falsch = new Counter(); // create instance of a counter for false en/decode
 35         Counter richtighash = new Counter(); // create instance of a counter for right hash creation
 36         Counter falschhash = new Counter(); // create instance of a counter for false hash creation
 37         Hash hashtest = new Hash();  // create instance of a hash class
 38         Vector falschstring = new Vector(); // vector for false decoded strings
 39         Vector falschstringhash = new Vector(); // vector for false decoded strings
 40         RandomString input = new RandomString(); // create instance of RandomString
 41         RSA recipient = new RSA(); // rsa class for en-/decryption
 42         RSA sign = new RSA();   // rsa class for signature
 43 
 44 
 45         for (int haupt = 1; haupt <= hauptlauf; ++haupt) {  // determines the main loop
 46             if (keyindex == 1024) {
 47                 keyindex = 256;
 48             } // max length for the keys
 49             keyindex += 1; // use for variable key length
 50 
 51             System.out.println("keylength: " + keyindex);
 52             /**
 53              * create keys
 54              *
 55              * @param length first parameter gives the length of the key
 56              * @param reliable second parameter gives you the reliability that the big integer is a prim
 57              * @return keyN for modulus
 58              * @return keyE public key
 59              * @return keyD private key
 60              */
 61             int keyLength = recipient.generateKeys(keyindex);
 62 
 63 
 64             RSAToString RSAcrypt = new RSAToString(recipient);
 65 
 66 
 67             System.out.println("---------------------------");
 68             System.out.println("Elemente für Verschlüsselung");
 69             System.out.println("D: " + recipient.getPublicKey()[0]);
 70             System.out.println("E: " + recipient.getPrivateKey());
 71             System.out.println("N: " + recipient.getPublicKey()[1]);
 72             System.out.println("---------------------------");
 73 
 74             /**
 75              * Set the keys for hashtesting
 76              * because the keys for decrypt and signature are different
 77              *
 78              */
 79             int keyLengthsign = sign.generateKeys(keyindex);
 80 
 81             RSAToString RSAsign = new RSAToString(sign);
 82 
 83 
 84 
 85             System.out.println("---------------------------");
 86             System.out.println("Elemente für Hashes");
 87             System.out.println("D: " + sign.getPublicKey()[0]);
 88             System.out.println("E: " + sign.getPrivateKey());
 89             System.out.println("N: " + sign.getPublicKey()[1]);
 90             System.out.println("---------------------------");
 91 
 92 
 93 
 94             for (int j = 1; j <= lauflänge + 1; j += schrittlänge) {  // Lauf in "schrittlänge" Schritten
 95                 String teststring = input.createRandomString(j); // build string with length i
 96 
 97 
 98 
 99                 teststring = new String(RSAcrypt.fitStringToOneSize(teststring, 16)); // normalize string so that it can be divided into
100                 // 16 byte blocks
101                 System.out.println("Teststring with length of " + j + ": " + teststring);
102 
103                 StringBuffer sbuf = new StringBuffer("");
104                 for (int z = 0; z < teststring.length() / 16; z++) { // divide string into 16 byte blocks
105 
106                     String encrypt = RSAcrypt.doEncryption(teststring.substring(16 * z, 16 * (z + 1)));
107 
108                     String decrypt = RSAcrypt.doDecryption(encrypt);
109 
110                     sbuf.append(decrypt);
111                 }
112                 System.out.println("Decrypt    with length of " + j + ": " + sbuf.toString());
113                 try {
114                     String verify = hashtest.createHash(teststring); // do hash for teststring
115                     System.out.println("Teststring_Hash: " + verify);
116 
117                     StringBuffer sverify = new StringBuffer("");
118                     for (int c = 0; c < 10; c++) { // divide hash into 10 4-Byte blocks and do
119                         // signing and verification for every block seperately
120                         // so it is secured that also with small keys a robust signation will be done
121                         String signature = (RSAsign.doSign(verify.substring(c * 4, 4 * (c + 1)))); // make the signature
122                         String checksign = RSAsign.doVerify(signature);// do encryption for the signature
123                         while (checksign.length() < 4) {
124                             checksign = "0" + checksign;
125                         }
126                         sverify.append(checksign);
127                     }
128                     String checksign = sverify.toString();
129 
130                     System.out.println("Checksign:       " + checksign);
131                     if (verify.equals(checksign)) {
132                         richtighash.erhoehe(); // if hast teststring = hash decrypted string increase counter
133                     } else {
134                         falschhash.erhoehe();
135                         falschstringhash.add(verify);
136                     }
137 
138                 } catch (NoSuchAlgorithmException ex) { // exception handler for createHash
139                     ex.printStackTrace();
140                 }
141 
142 
143                 /**
144                  * countermodul, gives you the count of error or right decryptions
145                  *
146                  * equal method because strings are objects
147                  *
148                  * @param teststring String with the normal Message
149                  * @param decrypt decrypted message
150                  *
151                  */
152                 if (teststring.trim().equals(sbuf.toString())) {
153                     richtig.erhoehe();
154                 } else {
155                     falsch.erhoehe();
156                     falschstring.addElement(teststring); // wrong decrypted messages are put in a vector
157                 }
158             }
159 
160             /*
161              * gives you the wrong decrypted messages via a enumeration
162              *
163              */
164             for (Enumeration el = falschstring.elements(); el.hasMoreElements();) {
165                 String elment = el.nextElement().toString();
166                 System.out.println("Wrong decrypted Strings: <" + elment + "> Länge: " + elment.toString().length());
167 
168             }
169 
170             for (Enumeration elh = falschstringhash.elements(); elh.hasMoreElements();) {
171                 String elment = elh.nextElement().toString();
172                 System.out.println("Wrong HASH Strings: <" + elment + "> ");
173 
174             }
175         }
176 
177         System.out.println("------------------------------------");
178         System.out.println("right encryptions:" + richtig.stand());
179         System.out.println("right hashes:" + richtighash.stand());
180         System.out.println("wrong encryptions:" + falsch.stand());
181         System.out.println("wrong hashes:" + falschhash.stand());
182         System.out.println("------------------------------------");
183 
184 
185     }
186 }
187 
188