Introduction to RSA and rsa encryption algorithms
1. What is asymmetric encryption?
1. the encryption key is different from the encryption key. Such an encryption algorithm is called asymmetric encryption.
2. Keys are classified into public keys and private keys.
Public Key: a public key that can be used to encrypt and decrypt passwords for anyone.
Private Key: the public key can be generated through the private key, but the public key cannot be generated from the Public Key (the probability of being exported is small to not considered)
3. When the content to be encrypted is encrypted with the public key, only the private key can be used for decryption.
When the content to be encrypted is encrypted with the private key, only the public key can be used for decryption.
4. Relationship between the public key and the private key. A simple formula is used to generate the public key and the private key. That is, there is a certain formula relationship between the public key and the private key of asymmetric encryption.
5. Common asymmetric encryption algorithms
RSA, DSA
Ii. What is an RSA algorithm?
RSA is an asymmetric cryptographic algorithm. asymmetric encryption means that the algorithm requires a pair of keys. If one of them is used for encryption, the other is used for decryption. The RSA algorithm involves three parameters: n, e1, and e2. Where, n is the product of two mass numbers p and q, and the number of digits occupied by the binary representation of n is the so-called key length. E1 and e2 are a pair of related values, e1 can be any take, but requires e1 and (p-1) * (q-1) mutual quality; then select e2, requirements (e2 * e1) mod (p-1) * (q-1) = 1. (N, e1), (n, e2) is the key pair. Where (n, e1) is the public key and (n, e2) is the private key. [1] RSA encryption and decryption algorithms are identical. If A is set to plain text and B is set to ciphertext, A = B ^ e2 mod n; B = A ^ e1 mod n; (In the public key encryption system, public key encryption is generally used, and Private Key decryption) e1 and e2 can be used interchangeably, that is, A = B ^ e1 mod n; B = A ^ e2 mod n; iii. Use of RSA Encryption Algorithm 1. Generation of RSA keys. A KeyPair object KeyPair is returned for asymmetric encryption. KeyPair contains a public key and a private key.
/*** Use the specified key length to generate an asymmetric key pair * @ param keySize the recommended value is 1024, not lower than * @ return */public static KeyPair generateRSAKeyPair (int keySize) {KeyPair ret = null; try {// 1. Prepare to generate KeyPairGenerator generator = KeyPairGenerator. getInstance ("RSA"); // 2. initialize and set the key length to generator. initialize (keySize); // 3. Generate and return ret = generator. generateKeyPair ();} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} return ret ;}
2. Obtain the private key and Public Key
Both PublicKey and PrivateKey are of the Key type.
KeyPair keyPair = EncryptUtil. generateRSAKeyPair (1024); // obtain the public key and grant it to anyone PublicKey publicKey = keyPair. getPublic ();
// Obtain the private key PrivateKey privateKey = keyPair. getPrivate ();
3. RSA Encryption
/*** RSA encryption ** @ param data the data to be encrypted * @ param key can be PublicKey, it can also be PrivateKey * @ return */public static byte [] rsaEncrypt (byte [] data, Key key) {byte [] ret = null; if (data! = Null & data. length> 0 & key! = Null) {// 1. Create a Cipher and use RSA try {Cipher cipher = Cipher. getInstance ("RSA"); // sets the Key cipher. init (Cipher. ENCRYPT_MODE, key); ret = cipher. doFinal (data);} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (InvalidKeyException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace () ;}} return ret ;}
4. RSA decryption
/*** RSA decryption ** @ param data the data to be decrypted * @ param key can be PublicKey, it can also be PrivateKey * @ return */public static byte [] rsaDecrypt (byte [] data, Key key) {byte [] ret = null; if (data! = Null & data. length> 0 & key! = Null) {// 1. Create a Cipher and use RSA try {Cipher cipher = Cipher. getInstance ("RSA"); // sets the Key cipher. init (Cipher. DECRYPT_MODE, key); ret = cipher. doFinal (data);} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (InvalidKeyException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace () ;}} return ret ;}
5. Storage of public and private keys
After a pair of public and private keys are created, we need to store them before using them.
// Obtain the private key PrivateKey privateKey = keyPair. getPrivate (); PublicKey publicKey = keyPair. getPublic (); // encoding converts the PublicKey and PrivateKey objects into byte arrays. The byte array is the actual data of the public key and private key. byte [] publicKeyEncoded = publicKey. getEncoded (); byte [] privateKeyEncoded = privateKey. getEncoded ();
// Encode the byte array by Base64 encoding algorithm into a visible String publickey = encrypted (publicKeyEncoded, Base64.NO _ WRAP); String privatekey = encrypted (privateKeyEncoded, Base64.NO _ WRAP );
// Perform storage operations
....
6. Loading of public and private keys
Usually 1. Get a key pair 2. Get a public key and a private key 3. Save the public key and the private key in the string format.
In future use, you need to obtain the public key and private key.
You need to know:
6.1. The KeyFactory can load the corresponding public key and private key.
6.2 use X509EncodedKeySpec (byte []) for public key Loading
6.3. Use PCKS8EncodedKeySpec (byte []) for private key loading.
// Obtain the Public Key // str_publickey as the public key String (the Public Key is first encoded as byte [] in the form of a String) // decodes byte [] publicdecode = Base64.decode (new String (str_publickey ), base64.NO _ WRAP); // construct the X509EncodedKeySpec object X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec (publicdecode); // specify the encryption algorithm KeyFactory keyFactory = KeyFactory. getInstance ("RSA"); // obtain the Public Key PublicKey publicKey = keyFactory. generatePublic (publicKeySpec );
// Decoding
// Obtain the Private Key
// Str_private is the Data byte [] privatedecode = Base64.decode (new String (str_private), Base64.NO _ WRAP) in the form of a private key String ); // construct the X509EncodedKeySpec object PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec (privatedecode); // specify the encryption algorithm keyFactory = KeyFactory. getInstance ("RSA"); // obtain the private key PrivateKey privateKey = keyFactory. generatePrivate (privateKeySpec );
Related Knowledge:
DES encryption algorithm