Advertisement
scotty92

Untitled

Oct 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package varekd.util;
  2.  
  3. /*************************************************************************
  4. * Compilation: javac RSA.java
  5. * Execution: java RSA N
  6. *
  7. * Generate an N-bit public and private RSA key and use to encrypt
  8. * and decrypt a random message.
  9. *
  10. * % java RSA 50
  11. * public = 65537
  12. * private = 553699199426609
  13. * modulus = 825641896390631
  14. * message = 48194775244950
  15. * encrpyted = 321340212160104
  16. * decrypted = 48194775244950
  17. *
  18. * Known bugs (not addressed for simplicity)
  19. * -----------------------------------------
  20. * - It could be the case that the message >= modulus. To avoid, use
  21. * a do-while loop to generate key until modulus happen to be exactly N bits.
  22. *
  23. * - It's possible that gcd(phi, publicKey) != 1 in which case
  24. * the key generation fails. This will only happen if phi is a
  25. * multiple of 65537. To avoid, use a do-while loop to generate
  26. * keys until the gcd is 1.
  27. *
  28. *************************************************************************/
  29.  
  30. import java.math.BigInteger;
  31. import java.security.KeyFactory;
  32. import java.security.KeyPair;
  33. import java.security.KeyPairGenerator;
  34. import java.security.NoSuchAlgorithmException;
  35. import java.security.PrivateKey;
  36. import java.security.PublicKey;
  37. import java.security.spec.PKCS8EncodedKeySpec;
  38. import java.security.spec.X509EncodedKeySpec;
  39.  
  40. import javax.crypto.Cipher;
  41.  
  42. public class RSA {
  43. private static KeyFactory keyFactory;
  44. private static PublicKey pubKey;
  45. private static PrivateKey privKey;
  46. static {
  47. try {
  48. keyFactory = KeyFactory.getInstance("RSA");
  49. pubKey = keyFactory
  50. .generatePublic(new X509EncodedKeySpec(
  51. new BigInteger(
  52. "258483531987721813854435365666199783121097212864526576114955744050873252978581213214062885665119329089273296913884093898593877564098511382732309048889240854054459372263273672334107564088395710980478911359605768175143527864461996266529749955416370971506195317045377519645018157466830930794446490944537605962330090699836840861268493872513762630835769942133970804813091619416385064187784658945")
  53. .toByteArray()));
  54. privKey = keyFactory
  55. .generatePrivate(new PKCS8EncodedKeySpec(
  56. new BigInteger(
  57. "126389230230897930352385109045517175528919326976939639978192012327670621489047580094424093866394604083620710942258641479882605306217705080633073206358002116577711775977035174649355992840385149147227804978220431329886904749249047207172981994234190017310148273188617548379533657419013879671232142718169518429371391936849151606245205570182197305333898616362563500262673852314745836689909720746451418490347388427381974609081779036643692442896601636017446393710362921966444628494554137329105609231821252714960402427087902143625637826354987050179190296311361184976459578647089802255916487029562372894817902016349527418080110572755696829671001527629662007011502494795321796989748708894483748787746164007093796775322700601606206239680220934740393355136437625692864876018489463040975412867784876767858234777778613227623572162881295529316433265197827292214481807179049611685053128209907494051691218003161010138655935539925662842276881932027193524730598562717449099166747466602094321757382874332291191770626601705016723177033286335759178872988144726412991304849553921854275796353460611722080118921976660955130059428940619614317969278356912087565839497213220194655672243883744862866647835331423918525974671607339058850826043973690788036967549648769172496014048685165109400959786151179359607410101378890865847238149636112094448917842512853709764865360978231071734030322981843223939320472985117985802975969976688950242865772248639234517663026594659960052526081995926396802458422109485608674299402862528020107837865224663685601410678473927829")
  58. .toByteArray()));
  59. } catch (Exception e) {
  60. }
  61. }
  62.  
  63. /**
  64. * Decrypt text using private key
  65. *
  66. * @param text
  67. * The encrypted text
  68. * @param key
  69. * The private key
  70. * @return The unencrypted text
  71. * @throws java.lang.Exception
  72. */
  73.  
  74. public static byte[] decrypt(byte[] text) throws Exception {
  75.  
  76. byte[] dectyptedText = null;
  77. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  78. cipher.init(Cipher.DECRYPT_MODE, privKey);
  79. dectyptedText = cipher.doFinal(text);
  80. return dectyptedText;
  81.  
  82. }
  83.  
  84. /**
  85. * Encrypt a text using public key.
  86. *
  87. * @param text
  88. * The original unencrypted text
  89. * @param key
  90. * The public key
  91. * @return Encrypted text
  92. * @throws java.lang.Exception
  93. */
  94. public static byte[] encrypt(byte[] text) throws Exception {
  95.  
  96. byte[] cipherText = null;
  97. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  98. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  99. cipherText = cipher.doFinal(text);
  100. return cipherText;
  101.  
  102. }
  103.  
  104. /**
  105. * Generate key which contains a pair of privae and public key using 1024
  106. * bytes
  107. *
  108. * @return key pair
  109. * @throws NoSuchAlgorithmException
  110. */
  111. public static KeyPair generateKey() throws NoSuchAlgorithmException {
  112.  
  113. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  114. keyGen.initialize(1024);
  115. KeyPair key = keyGen.generateKeyPair();
  116. return key;
  117.  
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement