Android 加密解密的几种方式总结

经常使用加密算法:DES、3DES、RC4、AES,RSA等;

对称加密:des,3des,aes

非对称加密:rsa

不可逆加密:md5

加密模式:ECB、CBC、CFB、OFB等;

填充模式:NoPadding、PKCS1Padding、PKCS5Padding、PKCS7Padding

一、几种方式的加密和解密(Base64,RSA,DES,AES)

实现类

  • MainActivity
public class HomeActivity extends AppCompatActivity {

    private static final String TAG = "HomeActivity";
    private static final String type = "android";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        setBase64();
        setRSA();
        setDES();
        setAES();

        // DES 加密 - AES 加密 - AES 解密 - DES 解密
        setDesAndAes();


    }
}

测试

  • setBase64()
private void setBase64() {

        String oldWord = "大家要注意身体,不要熬夜写代码";
        try {
            // 编码
            String encode = Base64Util.encodeWord(oldWord);
            System.out.println("编码:" + encode);
            // 解码
            String decode = Base64Util.decodeWord(encode);
            System.out.println("编码:" + decode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

$map效果

  • setRSA()
 private void setRSA() {
        // 获取到密钥对
        KeyPair keyPair = RSAUtil.generateRSAKeyPair(1024);

        // 获取公钥和私钥
        PublicKey aPublic = keyPair.getPublic();
        PrivateKey aPrivate = keyPair.getPrivate();
        ;
        byte[] aPublicEncoded = aPublic.getEncoded();
        byte[] aPrivateEncoded = aPrivate.getEncoded();

        try {

            // 公钥加密
            byte[] bytes = RSAUtil.encryptByPublicKey(type, "123".getBytes(), aPublicEncoded);
            String s1 = Base64Util.encodeWord(bytes.toString());
//            String encode = Base64Utils.encode(bytes);
//            Log.d(TAG, "公钥加密文件: " + encode);
            Log.d(TAG, s1);

            // 私钥解密
            byte[] bytes1 = RSAUtil.decryptByPrivateKey(type, bytes, aPrivateEncoded);
            String s = new String(bytes1);
            Log.d(TAG, "私钥解密文件: " + s);

        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("");

        try {
            // 私钥加密
            byte[] bytes = RSAUtil.encryptByPrivateKey(type, "456".getBytes(), aPrivateEncoded);
            String encode = Base64Utils.encode(bytes);
            Log.d(TAG, "私钥加密文件: " + encode);

            // 公钥解密
            byte[] bytes1 = RSAUtil.decryptByPublicKey(type, bytes, aPublicEncoded);
            String s = new String(bytes1);
            Log.d(TAG, "公钥解密文件: " + s);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
  • setDES()
  private void setDES() {

        /// ---------------------------------------- 静态 ------------------------------------------

        String key = "2012j214";        // 键值必须大于8位
        try {
            // 加密
            String s = DESUtil.desEncrypt("欧拉蕾", key);
            Log.d(TAG, "静态加密: " + s);

            // 解密
            String s1 = DESUtil.desDecrypt(s, key);
            Log.d(TAG, "静态解密: " + s1);

        } catch (Exception e) {
            e.printStackTrace();
        }

        // ----------------------------------------- 动态 ------------------------------------------

        // 动态生成秘钥
        String key2 = DESUtil.generateKey();

        try {

            // 动态加密
            String encode = DESUtil.encode(key2, "风里来,雨里去");
            Log.d(TAG, "动态加密: " + encode);

            // 动态解密
            String decode = DESUtil.decode(key2, encode);
            Log.d(TAG, "动态解密: " + decode);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
  • setAES()
private void setAES() {

        // 动态生成秘钥
        String key = AESUtil.generateKey();

        try {

            // 加密
            String encrypt = AESUtil.encrypt(key, "人有悲欢离合,月有阴晴圆缺");
            Log.d(TAG, "加密: " + encrypt);

            // 解密
            String decrypt = AESUtil.decrypt(key, encrypt);
            Log.d(TAG, "解密: " + decrypt);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
  • setDesAndAes()
 private void setDesAndAes() {
        String key = DESUtil.generateKey();
        String key2 = AESUtil.generateKey();
        String message = "风里来,雨里去";
        try {

            // DES 动态加密
            String encode = DESUtil.encode(key, message);
            Log.d(TAG, "DES加密: " + encode);

            // AES 加密
            String encrypt = AESUtil.encrypt(key2, encode);
            Log.d(TAG, "AES加密: " + encrypt);

            // AES 解密
            String decrypt = AESUtil.decrypt(key2, encrypt);
            Log.d(TAG, "AES解密: " + decrypt);

            // DES 解密
            String decode = DESUtil.decode(key, decrypt);
            Log.d(TAG, "DES解密: " + decode);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

二、以下为我根据网上所总结的工具类(包含Base64,RSA,DES,AES)

工具类

  • Base64
public class Base64Util {
    
    /**
     * 编码
     *
     * @param message 需编码的信息
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeWord(String message) throws UnsupportedEncodingException {

        return Base64.encodeToString(message.getBytes("utf-8"), Base64.NO_WRAP);

    }

    /**
     * 解码
     *
     * @param encodeWord 编码后的内容
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decodeWord(String encodeWord) throws UnsupportedEncodingException {

        return new String(Base64.decode(encodeWord, Base64.NO_WRAP), "utf-8");

    }
    
}
  • RSA
public class RSAUtil {

    // 非对称加密密钥算法
    public static final String RSA = "RSA";
    // 加密填充方式,android 的
    public static final String ECB_NO_PADDING = "RSA/None/NoPadding";
    // 加密填充方式,标准jdk 的
    public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
    //秘钥默认长度
    public static final int DEFAULT_KEY_SIZE = 2048;
    // 当要加密的内容超过bufferSize,则采用partSplit进行分块加密
    public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();
    // 当前秘钥支持加密的最大字节数
    public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;
    
    /**
     * 随机生成RSA密钥对
     *
     * @param keyLength 密钥长度,范围:512~2048
     *                  一般1024
     * @return
     */
    public static KeyPair generateRSAKeyPair(int keyLength) {
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
            kpg.initialize(keyLength);
            return kpg.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // ------------------------------------------------ 公钥 ---------------------------------------

    /**
     * 用公钥对字符串进行加密
     *
     * @param data 原文
     */
    public static byte[] encryptByPublicKey(String type, byte[] data, byte[] publicKey) throws Exception {
        // 得到公钥
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PublicKey keyPublic = kf.generatePublic(keySpec);

        // 加密数据
        if (type.equalsIgnoreCase("java")) {
            Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
            cp.init(Cipher.ENCRYPT_MODE, keyPublic);
            return cp.doFinal(data);
        } else {
            Cipher cp = Cipher.getInstance(ECB_NO_PADDING);
            cp.init(Cipher.ENCRYPT_MODE, keyPublic);
            return cp.doFinal(data);
        }


    }

    /**
     * 公钥解密
     *
     * @param data      待解密数据
     * @param publicKey 密钥
     * @return byte[] 解密数据
     */
    public static byte[] decryptByPublicKey(String type, byte[] data, byte[] publicKey) throws Exception {
        // 得到公钥
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PublicKey keyPublic = kf.generatePublic(keySpec);

        // 数据解密
        if (type.equalsIgnoreCase("java")) {
            Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
            cipher.init(Cipher.DECRYPT_MODE, keyPublic);
            return cipher.doFinal(data);
        } else {
            Cipher cipher = Cipher.getInstance(RSA);
            cipher.init(Cipher.DECRYPT_MODE, keyPublic);
            return cipher.doFinal(data);
        }

    }
    
    // ------------------------------------------------ 私钥 ---------------------------------------

    /**
     * 私钥加密
     *
     * @param data       待加密数据
     * @param privateKey 密钥
     * @return byte[] 加密数据
     */
    public static byte[] encryptByPrivateKey(String type, byte[] data, byte[] privateKey) throws Exception {
        // 得到私钥
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PrivateKey keyPrivate = kf.generatePrivate(keySpec);

        // 数据加密
        if (type.equalsIgnoreCase("java")) {
            Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
            cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
            return cipher.doFinal(data);
        } else {
            Cipher cipher = Cipher.getInstance(ECB_NO_PADDING);
            cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
            return cipher.doFinal(data);
        }

    }


    /**
     * 使用私钥进行解密
     */
    public static byte[] decryptByPrivateKey(String type, byte[] encrypted, byte[] privateKey) throws Exception {
        // 得到私钥
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory kf = KeyFactory.getInstance(RSA);
        PrivateKey keyPrivate = kf.generatePrivate(keySpec);

        // 解密数据
        if (type.equalsIgnoreCase("java")) {
            Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
            cp.init(Cipher.DECRYPT_MODE, keyPrivate);
            return cp.doFinal(encrypted);
        } else {

            Cipher cp = Cipher.getInstance(RSA);
            cp.init(Cipher.DECRYPT_MODE, keyPrivate);
            return cp.doFinal(encrypted);
        }

    }
    
}
  • DES
public class DESUtil {

    private final static String HEX = "0123456789ABCDEF";
    //DES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
    private final static String TRANSFORMATION = "DES/CBC/PKCS5Padding";
    //初始化向量参数,AES 为16bytes. DES 为8bytes.
    private final static String IVPARAMETERSPEC = "01020304";
    //DES是加密方式
    private final static String ALGORITHM = "DES";
    // SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
    private static final String SHA1PRNG = "SHA1PRNG";

    // ------------------------------------- 静态 --------------------------------------------------

    /**
     * DES 加密
     *
     * @param message 原文
     * @param key     密钥,长度不能够小于8位
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String message, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        byte[] encryptbyte = cipher.doFinal(message.getBytes());
        return new String(Base64.encode(encryptbyte, Base64.DEFAULT)).trim();
    }

    /**
     * DES解密
     *
     * @param message 密文
     * @param key     密钥,长度不能够小于8位
     * @return
     * @throws Exception
     */
    public static String desDecrypt(String message, String key) throws Exception {

        byte[] bytesrc = Base64.decode(message.getBytes(), Base64.DEFAULT);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }


    // ------------------------------------- 动态 --------------------------------------------------


    /*
     * 生成随机数,可以当做动态的密钥 加密和解密的密钥必须一致,不然将不能解密
     */
    public static String generateKey() {
        try {
            SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
            byte[] bytes_key = new byte[20];
            localSecureRandom.nextBytes(bytes_key);
            String str_key = toHex(bytes_key);
            return str_key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 对密钥进行处理(方式一)
    @SuppressLint("ObsoleteSdkInt")
    private static Key getRawKey(String key) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        //for android
        SecureRandom sr = null;
        // 在4.2以上版本中,SecureRandom获取方式发生了改变
        if (android.os.Build.VERSION.SDK_INT >= 17) {
            sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
        } else {
            sr = SecureRandom.getInstance(SHA1PRNG);
        }
        // for Java
        // secureRandom = SecureRandom.getInstance(SHA1PRNG);
        sr.setSeed(key.getBytes());
        kgen.init(64, sr); //DES固定格式为64bits,即8bytes。
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new SecretKeySpec(raw, ALGORITHM);
    }

    // 对密钥进行处理(方式二)
    private static Key getRawKey2(String key) throws Exception {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }

    //二进制转字符
    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

    /**
     * DES算法,加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥,长度不能够小于8位
     * @return 加密后的字节数组,一般结合Base64编码使用
     */
    public static String encode(String key, String data) {
        return encode(key, data.getBytes());
    }


    /**
     * DES算法,加密
     *
     * @param data 待加密字符串
     * @param key  加密私钥,长度不能够小于8位
     * @return 加密后的字节数组,一般结合Base64编码使用
     */
    public static String encode(String key, byte[] data) {
        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, getRawKey(key), iv);
            byte[] bytes = cipher.doFinal(data);
            return Base64.encodeToString(bytes, Base64.DEFAULT);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取编码后的值
     *
     * @param key
     * @param data
     * @return
     */
    public static String decode(String key, String data) {
        return decode(key, Base64.decode(data, Base64.DEFAULT));
    }

    /**
     * DES算法,解密
     *
     * @param data 待解密字符串
     * @param key  解密私钥,长度不能够小于8位
     * @return 解密后的字节数组
     */
    public static String decode(String key, byte[] data) {
        try {
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, getRawKey(key), iv);
            byte[] original = cipher.doFinal(data);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            return null;
        }
    }

}
  • AES
public class AESUtil {

    private final static String HEX = "0123456789ABCDEF";
    //AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
    private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
    //AES 加密
    private static final String AES = "AES";
    // SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
    private static final String SHA1PRNG = "SHA1PRNG";

    /**
     * @return 动态生成秘钥
     */
    public static String generateKey() {
        try {
            SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
            byte[] bytes_key = new byte[20];
            localSecureRandom.nextBytes(bytes_key);
            String str_key = toHex(bytes_key);
            return str_key;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 对秘钥进行处理
     *
     * @param seed 动态生成的秘钥
     * @return
     * @throws Exception
     */
    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(AES);
        //for android
        SecureRandom sr = null;
        // 在4.2以上版本中,SecureRandom获取方式发生了改变
        int sdk_version = android.os.Build.VERSION.SDK_INT;
        // Android  6.0 以上
        if (sdk_version > 23) {
            sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider());
            //4.2及以上
        } else if (android.os.Build.VERSION.SDK_INT >= 17) {
            sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
        } else {
            sr = SecureRandom.getInstance(SHA1PRNG);
        }


        // for Java
        // secureRandom = SecureRandom.getInstance(SHA1PRNG);
        sr.setSeed(seed);
        //256 bits or 128 bits,192bits
        kgen.init(128, sr);
        //AES中128位密钥版本有10个加密循环,192比特密钥版本有12个加密循环,256比特密钥版本则有14个加密循环。
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return raw;
    }

    /**
     * 加密
     *
     * @param key
     * @param cleartext
     * @return
     */
    public static String encrypt(String key, String cleartext) {
        if (TextUtils.isEmpty(cleartext)) {
            return cleartext;
        }
        try {
            byte[] result = encrypt(key, cleartext.getBytes());
            return new String(Base64.encode(result, Base64.DEFAULT));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] encrypt(String key, byte[] clear) throws Exception {
        byte[] raw = getRawKey(key.getBytes());
        SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
        Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    /**
     * 解密
     *
     * @param key
     * @param encrypted
     * @return
     */
    public static String decrypt(String key, String encrypted) {
        if (TextUtils.isEmpty(encrypted)) {
            return encrypted;
        }
        try {
            byte[] enc = Base64.decode(encrypted, Base64.DEFAULT);
            byte[] result = decrypt(key, enc);
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] decrypt(String key, byte[] encrypted) throws Exception {
        byte[] raw = getRawKey(key.getBytes());
        SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
        Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }


    // ----------------------------------------------- 辅助方法 ------------------------------------


    //二进制转字符
    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

    // 增加  CryptoProvider  类

    public static class CryptoProvider extends Provider {
        /**
         * Creates a Provider and puts parameters
         */
        public CryptoProvider() {
            super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
            put("SecureRandom.SHA1PRNG",
                    "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
            put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
        }
    }

}

你可能感兴趣的:(Android 加密解密的几种方式总结)