Before everyone has summed up, based on the key exchange DH algorithm, now for you to introduce a factor decomposition based RSA algorithm, this encryption algorithm has two implementation forms: 1, public key encryption, private key decryption, 2, private key encryption, public key decryption. The following for you to analyze the implementation of code, compared to the DH algorithm, RSA seems a little simple.
Initialize key:
Keypairgenerator keypairgenerator = keypairgenerator.getinstance ("RSA"); Keypairgenerator.initialize (+); = Keypairgenerator.generatekeypair (); = (Rsaprivatekey) keypair.getprivate (); // private key Rsapublickey Rsapublickey = (rsapublickey) keypair.getpublic (); // Public Key System.out.println ("Privatekey:" +base64.encodebase64string (rsaprivatekey.getencoded ())); System.out.println ("PublicKey:" +base64.encodebase64string (rsapublickey.getencoded ()));
1, private key encryption, public key decryption:
//private key encryption, public key decryption--encryptionPkcs8encodedkeyspec Pkcs8encodedkeyspec =NewPkcs8encodedkeyspec (rsaprivatekey.getencoded ()); Keyfactory privatekeyfactory= Keyfactory.getinstance ("RSA"); Privatekey Privatekey=privatekeyfactory.generateprivate (PKCS8ENCODEDKEYSPEC); Cipher Privatecipher= Cipher.getinstance ("RSA"); Privatecipher.init (Cipher.encrypt_mode, Privatekey); byte[] result =privatecipher.dofinal (Src.getbytes ()); System.out.println ("Private key encryption, public key decryption--encryption:" +base64.encodebase64string (result)); //private key encryption, public key decryption--decryptionX509encodedkeyspec X509encodedkeyspec =NewX509encodedkeyspec (rsapublickey.getencoded ()); Keyfactory publickeyfactory= Keyfactory.getinstance ("RSA"); PublicKey PublicKey=publickeyfactory.generatepublic (X509ENCODEDKEYSPEC); Cipher Publiccipher= Cipher.getinstance ("RSA"); Publiccipher.init (Cipher.decrypt_mode, PublicKey); Result=publiccipher.dofinal (Result); System.out.println ("Private key encryption, public key decryption--decryption:" +NewString (result));
2, public key encryption, private key decryption:
//public key encryption, private key decryption---encryptionX509encodedkeyspec =NewX509encodedkeyspec (rsapublickey.getencoded ()); Publickeyfactory= Keyfactory.getinstance ("RSA"); PublicKey=publickeyfactory.generatepublic (X509ENCODEDKEYSPEC); Publiccipher= Cipher.getinstance ("RSA"); Publiccipher.init (Cipher.encrypt_mode, PublicKey); Result=publiccipher.dofinal (Src.getbytes ()); System.out.println ("Public key encryption, private key decryption---encryption:" +base64.encodebase64string (result)); //public key encryption, private key decryption---decryptionPkcs8encodedkeyspec =NewPkcs8encodedkeyspec (rsaprivatekey.getencoded ()); Privatekeyfactory= Keyfactory.getinstance ("RSA"); Privatekey=privatekeyfactory.generateprivate (PKCS8ENCODEDKEYSPEC); Privatecipher= Cipher.getinstance ("RSA"); Privatecipher.init (Cipher.decrypt_mode, Privatekey); Result=privatecipher.dofinal (Result); System.out.println ("Public key encryption, private key decryption---decryption:" +NewString (result));
Depending on the output, it is not difficult to find that the public key and private key are not consistent and the private key is longer than the public key.
To today's information encryption algorithm: BASE64, symmetric encryption algorithm, message digest encryption algorithm, asymmetric encryption algorithm is all summed up.
Asymmetric encryption algorithm of information encryption RSA