Java RSA asymmetric encryption

Keywords: less Android

Android RSA asymmetric encryption

What is Rsa encryption

RSA algorithm is the most popular public key cryptography algorithm, which uses keys with variable length. RSA is the first algorithm that can be used for both data encryption and digital signature.

The principle of RSA algorithm is as follows:

1. Randomly select two prime numbers p and q, p is not equal to q, and calculate N=pq;

2. Select a natural number e greater than 1 and less than N, e must be prime with (p-1)(q-1).

3. Calculate d: D × e = 1 (mod (p-1)(q-1)).

4. Destroy p and q. The resulting N and e are the "public key" and D is the "private key". The sender uses n to encrypt the data, and the receiver only uses d to unlock the data content.  
RSA's security depends on large number decomposition, and N less than 1024 bits has been proved to be unsafe. Moreover, because RSA algorithm performs large number calculation
It makes RSA's fastest speed up to times slower than DES, which is the biggest defect of RSA, so it can only be used to encrypt a small amount of data or encryption key, but RSA is still a high-strength algorithm.

This example is for reference only:

/**
 * Created by Xinghai.Zhao on 17/08/28.
 */
public class RSAUtils {
    private static final String RSA = "RSA";
    private static final String PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmubFxxN1TI7RGpUvaBMOT2QsEcjj2aJF8V4yq9fqBEd50x6i6bPe8A3myBeZILxunSp9n6RXIWgijXH8fktd8CAwEAAQ==";

    /** Asymmetric encryption */
    public static String getEncodeData(String s) {
        try {
            byte[] buffer = Base64.decode(PUBLIC_KEY, Base64.DEFAULT);
            KeyFactory keyFactory = KeyFactory.getInstance(RSA);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            PublicKey publicKey = keyFactory.generatePublic(keySpec);
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] bytes = cipher.doFinal(s.getBytes());
            return Base64.encodeToString(bytes, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /** Asymmetric decryption */
    public static String getDecodeData(String s) {
        try {
            PublicKey publicKey = KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(Base64.decode(PUBLIC_KEY,Base64.DEFAULT)));
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            cipher.doFinal(Base64.decode(s,Base64.NO_CLOSE));
            byte[] result = cipher.doFinal(Base64.decode(s,Base64.DEFAULT));
            return new String(result,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}

Live for the system, die for the framework, and fight for debug for a lifetime; eat the loss of symbols, and die on demand.

Posted by phynias on Sat, 04 Jan 2020 17:15:48 -0800