RSA encryption and decryption methods, rsa encryption and decryption

Source: Internet
Author: User

RSA encryption and decryption methods, rsa encryption and decryption

RSAsecurity. java

Package com. mstf. rsa; import java. security. keyFactory; import java. security. keyPair; import java. security. keyPairGenerator; import java. security. privateKey; import java. security. publicKey; import java. security. interfaces. rsw.vatekey; import java. security. interfaces. RSAPublicKey; import java. security. spec. PKCS8EncodedKeySpec; import java. security. spec. x509EncodedKeySpec; import javax. crypto. cipher; import Com.sun.org. apache. xerces. internal. impl. dv. util. Base64;/* RSA tool class. Provides encryption, decryption, and key-to-Peer generation methods. The security of RSA depends on the decomposition of large numbers. Both the public key and private key are functions with two large prime numbers (decimal digits greater than 100. It is estimated that it is difficult to deduce the plaintext from a key and ciphertext to produce a product key that breaks down two prime numbers: 1. select two large prime numbers p, q, and calculate n = p * q; 2. random selection of encryption key e, requires e and P-1) * (q-1) Interoperability 3. the Euclid algorithm is used to calculate and decrypt the key d, so that it meets the requirements of e * d = 1 (mod (p-1) * (q-1) (where n, d must also be mutually qualitative) 4: so far, it is concluded that the public key is (n, e) and the private key is (n, d) RSA speed * because of the large number calculation, the fastest RSA speed is 100 times slower than DES, whether it is software or hardware implementation. * Speed has always been a defect of RSA. Generally, it is used only for a small amount of data encryption. */Public class RSAsecurity {public static String src = "admin"; public void priENpubDE () {try {// initialize the key KeyPairGenerator keyPairGenerator = KeyPairGenerator. getInstance ("RSA"); // key length keyPairGenerator. initialize (1024); // initialize the key pair KeyPair keyPair = keyPairGenerator. generateKeyPair (); // Public Key RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair. getPublic (); // Private Key rs?vatekey = (rs?vatekey) keyPair. getPrivate (); // 2. private Key Encryption and Public Key decryption ---- encryption // generate private key PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec (rsw.vatekey. getEncoded (); KeyFactory keyFactory = KeyFactory. getInstance ("RSA"); PrivateKey privateKey = keyFactory. generatePrivate (pkcs8EncodedKeySpec); // The Cipher class provides the password function for encryption and decryption. The object Cipher cipher = Cipher is instantiated through getinstance. getInstance ("RSA"); // initialize the encrypted cipher. init (Cipher. ENCRYPT_MODE, privateKey); byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("private key encryption, Public Key decryption ---- encryption:" + Base64.encode (result); // 3. private Key Encryption, Public Key decryption ---- decryption // generate public key X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec (rsaPublicKey. getEncoded (); keyFactory = KeyFactory. getInstance ("RSA"); PublicKey publicKey = keyFactory. generatePublic (x509EncodedKeySpec); cipher = Cipher. getInstance ("RSA"); // initialize and decrypt the cipher. init (Cipher. DECRYPT_MODE, publicKey); result = cipher. doFinal (result); System. out. println ("private key encryption, Public Key decryption ---- decryption:" + new String (result);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public void pubENpriDE () {try {// 1. initialize KeyPairGenerator keyPairGenerator = KeyPairGenerator. getInstance ("RSA"); // key length keyPairGenerator. initialize (512); // initialize the key pair KeyPair keyPair = keyPairGenerator. generateKeyPair (); // Public Key RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair. getPublic (); // Private Key rs?vatekey = (rs?vatekey) keyPair. getPrivate (); // 2. public key encryption and Private Key decryption ---- encrypt X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec (rsaPublicKey. getEncoded (); KeyFactory keyFactory = KeyFactory. getInstance ("RSA"); PublicKey publicKey = keyFactory. generatePublic (x509EncodedKeySpec); // initialize encryption // The Cipher class provides the password function for encryption and decryption. The object Cipher cipher = Cipher is instantiated through getinstance. getInstance ("RSA"); cipher. init (Cipher. ENCRYPT_MODE, publicKey); // encrypted string byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("public key encryption, Private Key decryption ---- encryption:" + Base64.encode (result); // 3. public key encryption and Private Key decryption ----- decrypt PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec (rsw.vatekey. getEncoded (); keyFactory = KeyFactory. getInstance ("RSA"); PrivateKey privateKey = keyFactory. generatePrivate (pkcs8EncodedKeySpec); // initialize and decrypt the cipher. init (Cipher. DECRYPT_MODE, privateKey); // decryption string result = cipher. doFinal (result); System. out. println ("public key encryption, Private Key decryption ----- decryption:" + new String (result);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

RSAtest. java

Package com. mstf. rsa; import com. mstf. rsa. RSAsecurity; public class RSAtest {public static void main (String [] args) {RSAsecurity rsAsecurity = new RSAsecurity (); System. out. println ("private key encryption Public Key decryption example:"); rsAsecurity. priENpubDE (); System. out. println ("public key encryption Private Key decryption example:"); rsAsecurity. pubENpriDE ();}}

  

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.