Java Encryption Algorithm RSA

Source: Internet
Author: User

Java encryption Algorithm RSA2015-06-06 08:44 511 people read comments (0) favorite reports Classification:JAVA ($)

Public key cryptography is also known as asymmetric encryption, slow, encryption and decryption keys are not the same, a person holds the private key, anyone can know the public key

[Java]View Plaincopy
  1. Package com.stone.security;
  2. Import Java.security.KeyPair;
  3. Import Java.security.KeyPairGenerator;
  4. Import Java.security.PrivateKey;
  5. Import Java.security.PublicKey;
  6. Import Java.util.Arrays;
  7. Import Javax.crypto.Cipher;
  8. /**
  9. * RSA algorithm Public key Cryptography asymmetric Encryption
  10. * @author Stone
  11. * @date 2014-03-11 00:28:38
  12. */
  13. Public class RSA {
  14. public static final String key_algorithm = "RSA";
  15. public static final String CIPHER_ALGORITHM_ECB1 = "rsa/ecb/pkcs1padding";
  16. public static final String cipher_algorithm_ecb2 = "rsa/ecb/oaepwithsha-1andmgf1padding"; //Not available
  17. public static final String cipher_algorithm_ecb3 = "oaepwithsha-256andmgf1padding"; //Not available
  18. static PublicKey PublicKey;
  19. static Privatekey Privatekey;
  20. static Cipher Cipher;
  21. static KeyPair KeyPair;
  22. public static void Main (string[] args) throws Exception {
  23. Method1 ("Skoda u* (sfsad7f () *^%%$");
  24. METHOD2 ("Skoda u* (sfsad7f () *^%%$");
  25. Method3 ("Skoda u* (sfsad7f () *^%%$");
  26. }
  27. /** 
  28. * Public key encryption, private key decryption using default CIPHER_ALGORITHM_ECB1
  29. * @param str
  30. * @throws Exception
  31. */
  32. static void method1 (String str) throws Exception {
  33. Keypairgenerator keygenerator = keypairgenerator.getinstance (key_algorithm);
  34. KeyPair KeyPair = Keygenerator.generatekeypair ();
  35. PublicKey = Keypair.getpublic ();
  36. Privatekey = Keypair.getprivate ();
  37. cipher = Cipher.getinstance (key_algorithm);
  38. Cipher.init (Cipher.encrypt_mode, PublicKey); //Public Key Cryptography
  39. byte[] Encrypt = cipher.dofinal (Str.getbytes ());
  40. SYSTEM.OUT.PRINTLN ("Public Key Cryptography 1:" + arrays.tostring (encrypt));
  41. Cipher.init (Cipher.decrypt_mode, Privatekey); //private key decryption
  42. byte[] Decrypt = cipher.dofinal (encrypt);
  43. System.out.println ("Private key decryption 1:" + new String (decrypt));
  44. }
  45. /** 
  46. * Private key encryption, public key decryption using default CIPHER_ALGORITHM_ECB1
  47. * @param str
  48. * @throws Exception
  49. */
  50. static void Method2 (String str) throws Exception {
  51. Keypairgenerator keygenerator = keypairgenerator.getinstance (key_algorithm);
  52. KeyPair KeyPair = Keygenerator.generatekeypair ();
  53. PublicKey = Keypair.getpublic ();
  54. Privatekey = Keypair.getprivate ();
  55. cipher = Cipher.getinstance (key_algorithm);
  56. Cipher.init (Cipher.encrypt_mode, Privatekey); //private key Encryption
  57. byte[] Encrypt = cipher.dofinal (Str.getbytes ());
  58. System.out.println ("2 after private key encryption:" + arrays.tostring (encrypt));
  59. Cipher.init (Cipher.decrypt_mode, PublicKey); //Public key decryption
  60. byte[] Decrypt = cipher.dofinal (encrypt);
  61. SYSTEM.OUT.PRINTLN ("Public key decryption 2:" + new String (decrypt));
  62. }
  63. /** 
  64. * Private key encryption, public key decryption using CIPHER_ALGORITHM_ECB1 = rsa/ecb/pkcs1padding
  65. * @param str
  66. * @throws Exception
  67. */
  68. static void method3 (String str) throws Exception {
  69. Keypairgenerator keygenerator = keypairgenerator.getinstance (key_algorithm);
  70. KeyPair KeyPair = Keygenerator.generatekeypair ();
  71. PublicKey = Keypair.getpublic ();
  72. Privatekey = Keypair.getprivate ();
  73. cipher = Cipher.getinstance (CIPHER_ALGORITHM_ECB1);
  74. Cipher.init (Cipher.encrypt_mode, Privatekey); //private key Encryption
  75. byte[] Encrypt = cipher.dofinal (Str.getbytes ());
  76. System.out.println ("3 after private key encryption:" + arrays.tostring (encrypt));
  77. Cipher.init (Cipher.decrypt_mode, PublicKey); //Public key decryption
  78. byte[] Decrypt = cipher.dofinal (encrypt);
  79. SYSTEM.OUT.PRINTLN ("Public key decryption 3:" + new String (decrypt));
  80. }
  81. }

Java Encryption Algorithm RSA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.