Guest User

Untitled

a guest
Dec 14th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. public static String doEncryption(String input) {
  2.  
  3.  
  4. try {
  5.  
  6. if (!RSAService.areKeysPresent()) {
  7. RSAService.generateKey();
  8. }
  9.  
  10.  
  11. ObjectInputStream inputStream;
  12.  
  13. // Encrypt the string using the public key
  14. inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
  15. PublicKey publicKey = (PublicKey) inputStream.readObject();
  16. byte[] cipherText = RSAService.encrypt(input, publicKey);
  17. return cipherText.toString();
  18.  
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return "ERROR: Public key file is probably missing";
  23. }
  24.  
  25. public static String doDecryption(String input) {
  26.  
  27. try {
  28.  
  29. if (!RSAService.areKeysPresent()) {
  30. RSAService.generateKey();
  31. }
  32.  
  33. ObjectInputStream inputStream;
  34.  
  35.  
  36. // Decrypt the cipher text using the private key.
  37. inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
  38. PrivateKey privateKey = (PrivateKey) inputStream.readObject();
  39. String out = decrypt(input.getBytes(), privateKey);
  40. return out;
  41.  
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. return "ERROR: Private key file is probably missing or doesn't match the public key";
  46. }
  47.  
  48. javax.crypto.BadPaddingException: Data must start with zero
  49. at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:325)
  50. at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:272)
  51. at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
  52. at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
  53. at javax.crypto.Cipher.doFinal(Cipher.java:2087)
  54. at rsaendecryptor.RSAService.decrypt(RSAService.java:132)
  55. at rsaendecryptor.RSAService.doDecryption(RSAService.java:180)
  56. at rsaendecryptor.RSAEnDecrypt.main(RSAEnDecrypt.java:20)
  57. java.lang.NullPointerException
  58. at java.lang.String.<init>(String.java:556)
  59. at rsaendecryptor.RSAService.decrypt(RSAService.java:138)
  60. at rsaendecryptor.RSAService.doDecryption(RSAService.java:180)
  61. at rsaendecryptor.RSAEnDecrypt.main(RSAEnDecrypt.java:20)
  62.  
  63. public class RSAService {
  64.  
  65. /**
  66. * String to hold name of the encryption algorithm.
  67. */
  68. public static final String ALGORITHM = "RSA";
  69.  
  70. /**
  71. * String to hold the name of the private key file.
  72. */
  73. public static final String PRIVATE_KEY_FILE = "private.key";
  74.  
  75. /**
  76. * String to hold name of the public key file.
  77. */
  78. public static final String PUBLIC_KEY_FILE = "public.key";
  79.  
  80. /**
  81. * Generate key which contains a pair of private and public key using 1024
  82. * bytes. Store the set of keys in Prvate.key and Public.key files.
  83. *
  84. */
  85. public static void generateKey() {
  86.  
  87. try {
  88. final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
  89. keyGen.initialize(1024);
  90. final KeyPair key = keyGen.generateKeyPair();
  91.  
  92. File privateKeyFile = new File(PRIVATE_KEY_FILE);
  93. File publicKeyFile = new File(PUBLIC_KEY_FILE);
  94.  
  95. // Create files to store public and private key
  96. privateKeyFile.createNewFile();
  97.  
  98. if (publicKeyFile.getParentFile() != null) {
  99. publicKeyFile.getParentFile().mkdirs();
  100. }
  101. publicKeyFile.createNewFile();
  102.  
  103. // Saving the Public key in a file
  104. ObjectOutputStream publicKeyOS = new ObjectOutputStream(
  105. new FileOutputStream(publicKeyFile));
  106. publicKeyOS.writeObject(key.getPublic());
  107. publicKeyOS.close();
  108.  
  109. // Saving the Private key in a file
  110. ObjectOutputStream privateKeyOS = new ObjectOutputStream(
  111. new FileOutputStream(privateKeyFile));
  112. privateKeyOS.writeObject(key.getPrivate());
  113. privateKeyOS.close();
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. }
  119.  
  120. /**
  121. * The method checks if the pair of public and private key has been
  122. * generated.
  123. *
  124. * @return flag indicating if the pair of keys were generated.
  125. */
  126. public static boolean areKeysPresent() {
  127.  
  128. File privateKey = new File(PRIVATE_KEY_FILE);
  129. File publicKey = new File(PUBLIC_KEY_FILE);
  130.  
  131. if (privateKey.exists() && publicKey.exists()) {
  132. return true;
  133. }
  134. return false;
  135. }
  136.  
  137. /**
  138. * Encrypt the plain text using public key.
  139. *
  140. * @param text : original plain text
  141. * @param key :The public key
  142. * @return Encrypted text
  143. * @throws java.lang.Exception
  144. */
  145. public static byte[] encrypt(String text, PublicKey key) {
  146. byte[] cipherText = null;
  147. try {
  148. // get an RSA cipher object and print the provider
  149. final Cipher cipher = Cipher.getInstance(ALGORITHM);
  150. // encrypt the plain text using the public key
  151. cipher.init(Cipher.ENCRYPT_MODE, key);
  152. cipherText = cipher.doFinal(text.getBytes());
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156. return cipherText;
  157. }
  158.  
  159. /**
  160. * Decrypt text using private key.
  161. *
  162. * @param text :encrypted text
  163. * @param key :The private key
  164. * @return plain text
  165. * @throws java.lang.Exception
  166. */
  167. public static String decrypt(byte[] text, PrivateKey key) {
  168. byte[] dectyptedText = null;
  169. try {
  170. // get an RSA cipher object and print the provider
  171. final Cipher cipher = Cipher.getInstance(ALGORITHM);
  172.  
  173. // decrypt the text using the private key
  174. cipher.init(Cipher.DECRYPT_MODE, key);
  175. dectyptedText = cipher.doFinal(text);
  176.  
  177. } catch (Exception ex) {
  178. ex.printStackTrace();
  179. }
  180.  
  181. return new String(dectyptedText);
  182. }
  183.  
  184. public static String doEncryption(String input) {
  185.  
  186.  
  187. try {
  188.  
  189. if (!RSAService.areKeysPresent()) {
  190. RSAService.generateKey();
  191. }
  192.  
  193.  
  194. ObjectInputStream inputStream;
  195.  
  196. // Encrypt the string using the public key
  197. inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
  198. PublicKey publicKey = (PublicKey) inputStream.readObject();
  199. byte[] cipherText = RSAService.encrypt(input, publicKey);
  200. return cipherText.toString();
  201.  
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. }
  205. return "ERROR: Public key file is probably missing";
  206. }
  207.  
  208.  
  209. public static String doDecryption(String input) {
  210.  
  211. try {
  212.  
  213. if (!RSAService.areKeysPresent()) {
  214. RSAService.generateKey();
  215. }
  216.  
  217. ObjectInputStream inputStream;
  218.  
  219.  
  220. // Decrypt the cipher text using the private key.
  221. inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
  222. PrivateKey privateKey = (PrivateKey) inputStream.readObject();
  223. String out = decrypt(input.getBytes(), privateKey);
  224. return out;
  225.  
  226. } catch (Exception e) {
  227. e.printStackTrace();
  228. }
  229. return "ERROR: Private key file is probably missing or doesn't match the public key";
  230. }
  231.  
  232. public static String doEncryption(String input)
  233.  
  234. return (Base64.encode(cipherText)).toString();
  235.  
  236. String out = decrypt(Base64.decode(input), privateKey);
  237. return out;
Add Comment
Please, Sign In to add comment