【转】Android 字符串的加密和解密

Android 字符串的加密和解密

  • 代码
  • 转载

代码

java

package com.rdwl.rdfeibilib;

import android.util.Base64;

import java.nio.charset.StandardCharsets;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * 字符串加密工具
 *
 * @author D10NG
 * @date on 2019-06-27 10:10
 */
public class EncryptUtils {

    /** 加密key */
    private static final String PASSWORD_ENC_SECRET = "test";

    /**
     * 加密
     * @param clearText
     * @return
     */
    public static String encryptPassword(String clearText) {
        try {
            DESKeySpec keySpec = new DESKeySpec(
                    PASSWORD_ENC_SECRET.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return Base64.encodeToString(cipher.doFinal(clearText
                    .getBytes(StandardCharsets.UTF_8)), Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clearText;
    }

    /**
     * 解密
     * @param encryptedPwd
     * @return
     */
    public static String decryptPassword(String encryptedPwd) {
        try {
            DESKeySpec keySpec = new DESKeySpec(
                    PASSWORD_ENC_SECRET.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
            return new String(plainTextPwdBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedPwd;
    }
}

kotlin


import android.util.Base64
import java.nio.charset.Charset

import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

/**
 * 字符串加密工具
 *
 * @author D10NG
 * @date on 2019-06-27 10:10
 */
object EncryptUtils {

    /** 加密key  */
    private var PASSWORD_ENC_SECRET = "test"

    //private static final String CipherMode = "AES/ECB/PKCS5Padding";使用ECB加密,不需要设置IV,但是不安全
    private const val CipherMode = "AES/CFB/NoPadding"//使用CFB加密,需要设置IV

    /**
     * 对字符串加密
     * @param data  源字符串
     * @return  加密后的字符串
     */
    fun encrypt(data: String): String {
        checkKeyLength()
        val cipher = Cipher.getInstance(CipherMode)
        val keySpec = SecretKeySpec(PASSWORD_ENC_SECRET.toByteArray(), "AES")
        cipher.init(
            Cipher.ENCRYPT_MODE, keySpec, IvParameterSpec(
                ByteArray(cipher.blockSize)
            )
        )
        val encrypted = cipher.doFinal(data.toByteArray())
        return Base64.encodeToString(encrypted, Base64.DEFAULT)
    }

    /**
     * 对字符串解密
     * @param data  已被加密的字符串
     * @return  解密得到的字符串
     */
    fun decrypt(data: String): String {
        checkKeyLength()
        val encrypted1 = Base64.decode(data.toByteArray(), Base64.DEFAULT)
        val cipher = Cipher.getInstance(CipherMode)
        val keySpec = SecretKeySpec(PASSWORD_ENC_SECRET.toByteArray(), "AES")
        cipher.init(
            Cipher.DECRYPT_MODE, keySpec, IvParameterSpec(
                ByteArray(cipher.blockSize)
            )
        )
        val original = cipher.doFinal(encrypted1)
        return String(original, Charset.forName("UTF-8"))
    }

    /**
     * 检查设定的key值长度
     */
    private fun checkKeyLength() {
        if (PASSWORD_ENC_SECRET.length < 32) {
            val a = 32 - PASSWORD_ENC_SECRET.length
            val builder = StringBuilder(PASSWORD_ENC_SECRET)
            for (i in 0 until a) {
                builder.append("A")
            }
            PASSWORD_ENC_SECRET = builder.toString()
        }
    }
}


转载

@mythmayor :Android - 字符串的加密和解密
https://blog.csdn.net/mythmayor/article/details/75257981

你可能感兴趣的:(Android知识点,Android,加密,解密,字符串)