Java encryption algorithm RSA

Source: Internet
Author: User
Tags asymmetric encryption

Public key encryption, also known as asymmetric encryption, is slow, encryption and decryption keys are different. A person holds a private key, and anyone can know the public key.

Package com. stone. security; import java. security. keyPair; import java. security. keyPairGenerator; import java. security. privateKey; import java. security. publicKey; import java. util. arrays; import javax. crypto. cipher;/*** RSA algorithm public key encryption asymmetric encryption * @ author stone * @ date 00:28:38 */public class RSA {public static final String KEY_ALGORITHM = "RSA "; public static final String CIPHER_ALGORITHM_ECB1 = "RSA/ECB/PKCS1Padding"; public static final String CIPHER_ALGORITHM_ECB2 = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding "; // public static final String CIPHER_ALGORITHM_ECB3 = "OAEPWithSHA-256AndMGF1Padding" cannot be used; // static PublicKey publicKey; static PrivateKey privateKey; static Cipher cipher; static KeyPair keyPair; public static void main (String [] args) throws Exception {method1 ("Skoda U * (Sfsad7f () * ^ % $ "); method2 ("Skoda U * (Sfsad7f () * ^ % $"); method3 ("Skoda U * (Sfsad7f () * ^ % $ ");} /*** public key encryption. Private Key decryption uses the default CIPHER_ALGORITHM_ECB1 * @ param str * @ throws Exception */static void method1 (String str) throws Exception {KeyPairGenerator keyGenerator = KeyPairGenerator. getInstance (KEY_ALGORITHM); KeyPair keyPair = keyGenerator. generateKeyPair (); publicKey = keyPair. getPublic (); privateKey = keyPair. getPrivate (); cipher = Cipher. getInstance (KEY_ALGORITHM); cipher. init (Cipher. ENCRYPT_MODE, publicKey); // public key encryption byte [] encrypt = cipher. doFinal (str. getBytes (); System. out. println ("1 after public key encryption:" + Arrays. toString (encrypt); cipher. init (Cipher. DECRYPT_MODE, privateKey); // Private Key decryption byte [] decrypt = cipher. doFinal (encrypt); System. out. println ("1 after Private Key decryption:" + new String (decrypt);}/*** private key encryption, public Key decryption uses the default CIPHER_ALGORITHM_ECB1 * @ param str * @ throws Exception */static void method2 (String str) throws Exception {KeyPairGenerator keyGenerator = KeyPairGenerator. getInstance (KEY_ALGORITHM); KeyPair keyPair = keyGenerator. generateKeyPair (); publicKey = keyPair. getPublic (); privateKey = keyPair. getPrivate (); cipher = Cipher. getInstance (KEY_ALGORITHM); cipher. init (Cipher. ENCRYPT_MODE, privateKey); // Private Key Encryption byte [] encrypt = cipher. doFinal (str. getBytes (); System. out. println ("private key encrypted 2:" + Arrays. toString (encrypt); cipher. init (Cipher. DECRYPT_MODE, publicKey); // Public Key decryption byte [] decrypt = cipher. doFinal (encrypt); System. out. println ("2 after Public Key decryption:" + new String (decrypt);}/*** private key encryption, use CIPHER_ALGORITHM_ECB1 = RSA/ECB/PKCS1Padding * @ param str * @ throws Exception */static void method3 (String str) throws Exception {KeyPairGenerator keyGenerator = KeyPairGenerator. getInstance (KEY_ALGORITHM); KeyPair keyPair = keyGenerator. generateKeyPair (); publicKey = keyPair. getPublic (); privateKey = keyPair. getPrivate (); cipher = Cipher. getInstance (CIPHER_ALGORITHM_ECB1); cipher. init (Cipher. ENCRYPT_MODE, privateKey); // Private Key Encryption byte [] encrypt = cipher. doFinal (str. getBytes (); System. out. println ("3 after private key encryption:" + Arrays. toString (encrypt); cipher. init (Cipher. DECRYPT_MODE, publicKey); // Public Key decryption byte [] decrypt = cipher. doFinal (encrypt); System. out. println ("3 after Public Key decryption:" + new String (decrypt ));}}


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.