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 ));}}