加密

AES

加密
  • 密钥生成
  • 数据填充至16的倍数
  • AES 加密
  • BASE64 加密
private final static String ALGO = "AES"; // AES加密
private final static String ALGO_MODE = "AES/CBC/NoPadding"; // (算法/模式/填充)
//private final static String ALGO_MODE = "AES"; // (算法)
private static String key = "qweasdzxcqweasdz"; // 固定的密钥
private static String iv = "qweasdzxcqweasdz"; 

public static String encrypt(String text) {
        try {
            Cipher cipher = Cipher.getInstance(ALGO_MODE);
            // Cipher块的大小 16/32/...
            int blockSize = cipher.getBlockSize();
            // 将待加密的数据转成字节
            byte[] dataBytes = data.getBytes();

            // 将加密字节数组填充至Cipher的倍数,
            // 填充规则16 - (70 % 16) = 10 ,在字节数组末尾添加10个10,
            // 具体的填充规则可以根据需要修改
            int dataLength = dataBytes.length;
            int paddingLength = blockSize - dataLength % 16;
            int encryptDataLength = dataLength + paddingLength;
            // 待填充的新数组
            byte[] encryptDataBytes = new byte[encryptDataLength];
            // copy
            System.arraycopy(dataBytes, 0, encryptDataBytes, 0, dataBytes.length);
            // 填充数组至16的倍数
            byte padding = Byte.valueOf(String.valueOf(paddingLength));
            for (int i = encryptDataLength - 1; i >= dataLength; i--){
                encryptDataBytes[i] = padding;
            }

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("utf-8"), ALGO);
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("utf-8"));
            // 加密
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(encryptDataBytes);
            // 加密后的字节码通过BASE64处理
            String EncStr = Base64.encodeToString(encrypted, Base64.DEFAULT);
            Log.d(TAG, "encrypt: before" + Arrays.toString(encryptDataBytes));
            Log.d(TAG, "encrypt: encrypt" + Arrays.toString(encrypted));
            Log.d(TAG, "encrypt: BASE " + EncStr);
            return EncStr ;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
解密
  • BASE64 解密
  • AES 解密
  • 去除填充数据
public static String decrypt(String text) {
        try {
            // BASE64 解密
            byte[] encrypt = Base64.decode(text, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance(ALGO_MODE);

            // AES 解密
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("utf-8"), ALGO);
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("utf-8"));
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] encryptData = cipher.doFinal(encrypt);
            int encryptDataLength = encryptData.length;

            //
            int removePadding = encryptData[encryptData.length-1];
            int dataLength = encryptData.length - removePadding;
            byte[] dataBytes = new byte[dataLength];
            for (int i = 0; i< dataLength; i++){
                dataBytes[i] = encryptData[i];
            }

            String data = new String(dataBytes);

            Log.d(TAG, "decrypt: " + Arrays.toString(encryptData));
            Log.d(TAG, "decrypt: " + Arrays.toString(dataBytes));
            Log.d(TAG, "decrypt: " + data);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

你可能感兴趣的:(加密)