Encryption and decryption using the RSA Algorithm on Android

Source: Internet
Author: User
Tags key string modulus

First, we can see: plaintext ---> Public Key ---> ciphertext --> key --> plaintext

RSA greatly improves file security because of the differences between public key <Public Key> and private key <key> <also called asymmetric algorithm encryption and decryption>. Symmetric algorithms are of the same key, which can be easily cracked.

RSA algorithm:

 

  1. ImportJava. Security. Key;
  2. ImportJava. Security. keyfactory;
  3. ImportJava. Security. keypair;
  4. ImportJava. Security. keypairgenerator;
  5. ImportJava. Security. privatekey;
  6. ImportJava. Security. publickey;
  7. ImportJava. Security. Interfaces. rsw.vatekey;
  8. ImportJava. Security. Interfaces. rsapublickey;
  9. ImportJava. Security. spec. pkcs8encodedkeyspec;
  10. ImportJava. Security. spec. x509encodedkeyspec;
  11. ImportJavax. crypto. cipher;
  12. ImportSun. Misc. base64decoder;
  13. ImportSun. Misc. base64encoder;
  14. Public ClassRsahelper {
  15. Public StaticPublickey getpublickey (string key)ThrowsException {
  16. Byte[] Keybytes;
  17. Keybytes = (NewBase64decoder (). decodebuffer (key );
  18. X509encodedkeyspec keyspec =NewX509encodedkeyspec (keybytes );
  19. Keyfactory = keyfactory. getinstance ("RSA ");
  20. Publickey = keyfactory. generatepublic (keyspec );
  21. ReturnPublickey;
  22. }
  23. Public StaticPrivatekey getprivatekey (string key)ThrowsException {
  24. Byte[] Keybytes;
  25. Keybytes = (NewBase64decoder (). decodebuffer (key );
  26. Pkcs8encodedkeyspec keyspec =NewPkcs8encodedkeyspec (keybytes );
  27. Keyfactory = keyfactory. getinstance ("RSA ");
  28. Privatekey = keyfactory. generateprivate (keyspec );
  29. ReturnPrivatekey;
  30. }
  31. Public StaticString getkeystring (Key key)ThrowsException {
  32. Byte[] Keybytes = key. getencoded ();
  33. String S = (NewBase64encoder (). encode (keybytes );
  34. ReturnS;
  35. }
  36. Public Static VoidMain (string [] ARGs)ThrowsException {
  37. Keypairgenerator keypairgen = keypairgenerator. getinstance ("RSA ");
  38. // Number of keys
  39. Keypairgen. initialize (1024 );
  40. // Key pair
  41. Keypair = keypairgen. generatekeypair ();
  42. // Public Key
  43. Publickey = (rsapublickey) keypair. getpublic ();
  44. // Private Key
  45. Privatekey = (rsw.vatekey) keypair. getprivate ();
  46. String publickeystring = getkeystring (publickey );
  47. System. Out. println ("public:/N" + publickeystring );
  48. String privatekeystring = getkeystring (privatekey );
  49. System. Out. println ("Private:/N" + privatekeystring );
  50. // Encryption/Decryption class
  51. Cipher cipher = cipher. getinstance ("RSA"); // cipher. getinstance ("RSA/ECB/pkcs1padding ");
  52. // Plaintext
  53. Byte[] Plaintext = "we are all good! Email: @ Sina.com ". getbytes ();
  54. // Encryption
  55. Cipher. INIT (Cipher. encrypt_mode, publickey );
  56. Byte[] Enbytes = cipher. dofinal (plaintext );
  57. // Obtain the key using the key string
  58. Publickey = getpublickey (publickeystring );
  59. Privatekey = getprivatekey (privatekeystring );
  60. // Decrypt
  61. Cipher. INIT (Cipher. decrypt_mode, privatekey );
  62. Byte[] Debytes = cipher. dofinal (enbytes );
  63. Publickeystring = getkeystring (publickey );
  64. System. Out. println ("public:/N" + publickeystring );
  65. Privatekeystring = getkeystring (privatekey );
  66. System. Out. println ("Private:/N" + privatekeystring );
  67. String S =NewString (debytes );
  68. System. Out. println (s );
  69. }
  70. }

In actual development! We use our own modulus and publicexponent to encrypt and generate public keys, while privateexponet is used to generate private keys for development.

 

  1. ImportJava. Math. biginteger;
  2. ImportJava. Security. keyfactory;
  3. ImportJava. Security. privatekey;
  4. ImportJava. Security. publickey;
  5. ImportJava. Security. spec. rsw.vatekeyspec;
  6. ImportJava. Security. spec. rsapublickeyspec;
  7. ImportJavax. crypto. cipher;
  8. Public ClassRsakey {
  9. PublicPublickey getpublickey (string modulus, string publicexponent)ThrowsException {
  10. Biginteger M =NewBiginteger (modulus );
  11. Biginteger E =NewBiginteger (publicexponent );
  12. Rsapublickeyspec keyspec =NewRsapublickeyspec (M, e );
  13. Keyfactory = keyfactory. getinstance ("RSA ");
  14. Publickey = keyfactory. generatepublic (keyspec );
  15. ReturnPublickey;
  16. }
  17. PublicPrivatekey getprivatekey (string modulus, string privateexponent)ThrowsException {
  18. Biginteger M =NewBiginteger (modulus );
  19. Biginteger E =NewBiginteger (privateexponent );
  20. Rsw.vatekeyspec keyspec =NewRsw.vatekeyspec (M, e );
  21. Keyfactory = keyfactory. getinstance ("RSA ");
  22. Privatekey = keyfactory. generateprivate (keyspec );
  23. ReturnPrivatekey;
  24. }
  25. Public Static VoidMain (string [] ARGs)ThrowsException {
  26. String modulus = "10103166745709600780215616551837697832816413714471062522342538060943596036859967333870827790358555455232243383580565187280643159050869924436081447583051139 ";
  27. String publicexponent = "65537 ";
  28. String privateexponet = "367979294475011322800474185715497882523349856362702385535371444397399388741997039894583483410120364529325888461124714276674612930833020362278754665756193 ";
  29. Rsakey key =NewRsakey ();
  30. Publickey = key. getpublickey (modulus, publicexponent );
  31. Privatekey = key. getprivatekey (modulus, privateexponet );
  32. // Encryption/Decryption class
  33. Cipher cipher = cipher. getinstance ("RSA"); // "RSA/ECB/pkcs1padding" indicates "algorithm/working mode/filling mode"
  34. // Plaintext
  35. Byte[] Plaintext = "Hello world! ". Getbytes ();
  36. // Encryption
  37. Cipher. INIT (Cipher. encrypt_mode, publickey );
  38. Byte[] Enbytes = cipher. dofinal (plaintext );
  39. Cipher. INIT (Cipher. decrypt_mode, privatekey );
  40. Byte[] Debytes = cipher. dofinal (enbytes );
  41. String S =NewString (debytes );
  42. System. Out. println (s );
  43. }

 

  1. }
Related Article

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.