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
- 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 Cryptography asymmetric Encryption
- * @author Stone
- * @date 2014-03-11 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"; //Not available
- public static final String cipher_algorithm_ecb3 = "oaepwithsha-256andmgf1padding"; //Not available
- 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 using 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 Cryptography
- byte[] Encrypt = cipher.dofinal (Str.getbytes ());
- SYSTEM.OUT.PRINTLN ("Public Key Cryptography 1:" + arrays.tostring (encrypt));
- Cipher.init (Cipher.decrypt_mode, Privatekey); //private key decryption
- byte[] Decrypt = cipher.dofinal (encrypt);
- System.out.println ("Private key decryption 1:" + new String (decrypt));
- }
- /**
- * Private key encryption, public key decryption using 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 ("2 after private key encryption:" + arrays.tostring (encrypt));
- Cipher.init (Cipher.decrypt_mode, PublicKey); //Public key decryption
- byte[] Decrypt = cipher.dofinal (encrypt);
- SYSTEM.OUT.PRINTLN ("Public key decryption 2:" + new String (decrypt));
- }
- /**
- * Private key encryption, public key decryption using 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 ("Public key decryption 3:" + new String (decrypt));
- }
- }
Java Encryption Algorithm RSA