RSAToString.java
  1 package testrsa;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.math.BigInteger;
  5 
  6 /**
  7  * This class provides basic methods for converting BigInters to Strings
  8  * and vice versa. It uses the RSA class to conduct chiper operations
  9  * @author Alexander Scheidl
 10  */
 11 public class RSAToString {
 12 
 13     RSA rsa = null;
 14 
 15     /**
 16      * Constructor
 17      * @param rsa with the RSA class
 18      */
 19     public RSAToString(RSA rsa) {
 20 
 21         this.rsa = rsa;
 22 
 23     }
 24 
 25     /**
 26      * This method is called for encryption of a given String.
 27      *
 28      * First the STring is converted into a Byte array and then encrypted
 29      * with help of the RSA.encrypt method.
 30      *
 31      * @param s String to be encrypted
 32      * @return encrypted String
 33      */
 34     public String doEncryption(String s) {
 35         BigInteger big = new BigInteger("0");
 36         try {
 37             byte[] b = s.getBytes("ISO-8859-1");
 38             big = rsa.encrypt(this.byteToBigInteger(b));
 39         } catch (RsaException ex) {
 40             ex.printStackTrace();
 41         } catch (UnsupportedEncodingException ex) {
 42             ex.printStackTrace();
 43         }
 44 
 45         return this.BigIntegerToHexString(big);
 46 
 47     }
 48 
 49     /**
 50      * Decryption method. <br><br>
 51      *
 52      * This method gets a Hex-String, converts it into a BigInteger and does
 53      * decryption with the decrypt method. Finally the BigInteger again is
 54      * retransformed with the BigIntegerToString() method.
 55      *
 56      * @param s the String that has to be decrypted
 57      * @return the decrypted String
 58      * @throws RsaException if an error from the decrypt method comes
 59      *
 60      */
 61     public String doDecryption(String s) {
 62         BigInteger big = new BigInteger(s, 16);
 63         try {
 64             big = rsa.decrypt(big);
 65         } catch (RsaException ex) {
 66             ex.printStackTrace();
 67         }
 68 
 69 
 70         return this.BigIntegerToString(big).trim();
 71 
 72     }
 73 
 74     /**
 75      * method for signing a given (Hash)String
 76      * @param s String to be signed
 77      * @return signed String
 78      */
 79     public String doSign(String s) {
 80 
 81         BigInteger big = new BigInteger(s, 16);
 82         try {
 83             big = rsa.sign(big);
 84         } catch (RsaException ex) {
 85             ex.printStackTrace();
 86         }
 87 
 88 
 89         return this.BigIntegerToHexString(big);
 90     }
 91 
 92     /**
 93      * This is the verification method.<br><br>
 94      *
 95      * It gets a Hex-String that represents the signed String  with the private
 96      * key from sender.<br><br>
 97      *
 98      * First the Hex is converted into a BigInteger with help of the standard
 99      * constructor from the BigInteger class. After that the Integer will be
100      * verified with the RSA.verifier() method which returns a BigInteger value.
101      * <br><br>
102      * Finally this BigInteger value has to be converted into a String with the
103      * BigIntegerToString() method.
104      *
105      * @param s Hex-String with signed String
106      * @return Hex-String with checked (public key) String
107      * @throws RsaException if an error occurs from the rsa class
108      */
109     public String doVerify(String s) {
110 
111         BigInteger big = new BigInteger(s, 16);
112 
113         big = rsa.verify(big);
114         return this.BigIntegerToHexString(big);
115 
116     }
117 
118     /**
119      * This method converts a byte array to an BigInteger
120      *
121      * @param b Byte array
122      * @return BigInteger value
123      */
124     private BigInteger byteToBigInteger(byte[] b) {
125         BigInteger bigInt = new BigInteger("0");
126 
127         for (int i = 0; i < b.length; i++) {
128             bigInt = bigInt.shiftLeft(8);
129             Integer value = new Integer((b[i] & 0x7F) + (b[i] < 0 ? 128 : 0)); // 0 to 255 Ascii Code
130             bigInt = bigInt.or(new BigInteger(value.toString()));
131         }
132         return bigInt;
133     }
134 
135     /**
136      * This method converts a BigInteger to a String (Ascii Code).<br><br>
137      *
138      * First the BigInteger is converted into a ByteArray (toByteArray method)
139      * then the array is looped and finally transformed into an char value
140      * that will be appended to the String with help from a StingBuffer.
141      *
142      * @param big BigInteger that will be converted
143      * @return converted BigInteger numbers into ASCII Code Char-String.
144      */
145     private String BigIntegerToString(BigInteger big) {
146         byte[] bnew = big.toByteArray();
147         StringBuffer sb = new StringBuffer();
148         for (int i = 0; i < bnew.length; i++) {
149             int value = (bnew[i] & 0x7F) + (bnew[i] < 0 ? 128 : 0); // 0 to 255 Ascii Code
150             sb.append((char) value);
151         }
152         return sb.toString();
153     }
154 
155     /**
156      * simple method to convert a BigInteger to a Hex-String
157      * @param big BigInteger value to be converted
158      * @return Hex String
159      */
160     private String BigIntegerToHexString(BigInteger big) {
161 
162         String str = big.toString(16);
163 
164 
165         return str;
166 
167     }
168 
169     /**
170      * This method fits a String to a specified length represented by the
171      * blocksize value
172      * @param str String to be fitted
173      * @param Blocksize Byte-Blocksize
174      * @return fitted String
175      */
176     public String fitStringToOneSize(String str, int Blocksize) {
177 
178         StringBuffer sbuftemp = new StringBuffer(str);
179         int block = str.length() / Blocksize;
180 
181         int rappend = Blocksize - (str.length() - (block * Blocksize));
182 
183         if (rappend > 0) {
184             for (int i = 0; i < rappend; i++) {
185                 sbuftemp.append(" ");
186             }
187         }
188         return sbuftemp.toString();
189     }
190 }
191 
192 
193 
194