DES, RSA (segmented encryption and decryption) two commonly used encryption methods in Android

DES encryption and decryption

public class DESEncrypt {

    /**
     * DES Decrypt
     * @param decryptString ciphertext
     * @param ivStr Encryption key
     * @param keyStr Encryption offset
     * @return Plaintext
     * @throws Exception exception
     */
    public static String decrypt(String decryptString, String keyStr, String ivStr)
            throws Exception {
        IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
        SecretKeySpec key = new SecretKeySpec(keyStr.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        return new String(cipher.doFinal(Base64.decode(decryptString, Base64.NO_WRAP)));
    }

    /**
     * DES encryption
     * @param encryptString Plaintext
     * @param keyStr Decrypt key
     * @param ivStr Offset
     * @return ciphertext
     * @throws Exception exception
     */
    public static String encrypt(String encryptString, String keyStr, String ivStr)
            throws Exception {
        IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
        DESKeySpec dks = new DESKeySpec(keyStr.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        return new String(Base64.encode(cipher.doFinal(encryptString.getBytes()), Base64.NO_WRAP));
    }
}

RSA encryption and decryption (supports RSA segmented encryption and decryption). Because RSA encryption and decryption itself has restrictions on the length of plaintext or ciphertext, our encryption algorithm here can support segmented encryption and decryption.

public class RSAEncrypt {

    /**
     * RSA Maximum encrypted clear text size
     */
    private static final int MAX_ENCRYPT_BLOCK = 117;

    /**
     * RSA Maximum decryption ciphertext size
     */
    private static final int MAX_DECRYPT_BLOCK = 128;


    /**
     * Convert the base64 encoded public key string to a PublicKey instance
     * @param publicKey Public key character
     * @return publicKEY
     * @throws Exception exception
     */
    public static PublicKey getPublicKey(String publicKey) throws Exception {
        byte[] keyBytes = Base64.decode(publicKey, Base64.NO_WRAP);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

    /**
     * Convert the base64 encoded private key string to a PrivateKey instance
     * @param privateKey Private key string
     * @return Private key object
     * @throws Exception exception
     */
    public static PrivateKey getPrivateKey(String privateKey) throws Exception {
        byte[] keyBytes = Base64.decode(privateKey, Base64.NO_WRAP);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }

    /**
     * RSA encryption
     * @param content Text to be encrypted
     * @param publicKey Public key
     * @return ciphertext
     * @throws Exception exception
     */
    public static String encrypt(String content, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//java default "RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] data = content.getBytes();
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // Segment encryption of data
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return new String(Base64.encode(encryptedData, Base64.NO_WRAP));
    }

    /**
     * RSA Decrypt
     * @param content ciphertext
     * @param privateKey Private key
     * @return Plaintext
     * @throws Exception exception
     */
    public static String decrypt(String content, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] encryptedData = Base64.decode(content, Base64.NO_WRAP);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // Segment decryption of data
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return new String(decryptedData);
    }
}

RSA uses public key for encryption and private key for decryption. Generally, the public key is saved by the client, and the public key is used to encrypt the data when the network requests. The private key is usually stored in the server, which decrypts the network request using the private key. This can guarantee the data security when the network requests!
RSA's public and private keys can be applied online. RSA key generation online

Tags: network Java

Posted on Tue, 05 May 2020 12:00:42 -0700 by lathifmca