杂项

当前位置:首页>技术博客>杂项
全部 15 TFrame框架 2 游戏渲染 0 编辑器扩展 0 性能优化 3 SDK 4 数据结构和算法 1 杂项 5

Android上使用AES-128/GCM + BASE64加解密

时间:2021-08-29   访问量:1353

AES-128/GCM + BASE64加解密之前在C#上实现了。现在想自己做个聚合SDK,数据与后台进行加密传输,所以需要实现一个Android的java版的。

package com.tyeryuan.tsdk.Common;

import android.util.Base64;


import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class AesGcm {
    
    private static final String KEY_ALGORITHM = "AES";
    
    private static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
    
    private static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";

    
    public static String encrypt(String plainText, String secretKey) {
        try {

            Random random = new Random();
            byte[] nonce = new byte[12];
            random.nextBytes(nonce);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(secretKey), new GCMParameterSpec(128, nonce));
            byte[] encryptByte = cipher.doFinal(plainText.getBytes(CHARSET_UTF8));
            byte[] cipherData = new byte[12+encryptByte.length];
            System.arraycopy(nonce,0,cipherData,0,nonce.length);
            System.arraycopy(encryptByte,0,cipherData,nonce.length,encryptByte.length);
            return base64Encode(cipherData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static String decrypt(String cipherText, String secretKey) {
        try {
            byte[] data = base64Decode(cipherText);

            byte[] nonce = new  byte[12];
            byte[] cipherData = new byte[data.length-nonce.length];
            System.arraycopy(data,0,nonce,0,nonce.length);
            System.arraycopy(data,nonce.length,cipherData,0,cipherData.length);

            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(secretKey), new GCMParameterSpec(128, nonce));
            byte[] result = cipher.doFinal(cipherData);
            return new String(result, CHARSET_UTF8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static SecretKeySpec getSecretKey(String secretKey) {
        return new SecretKeySpec(hexToByte(secretKey), KEY_ALGORITHM);
    }

    static byte[] base64Decode(String data) {
        return Base64.decode(data, Base64.NO_WRAP);
    }

    static String base64Encode(byte[] data) {
        return Base64.encodeToString(data, Base64.NO_WRAP);
    }

    public static byte[] hexToByte(String hex){
        int m = 0, n = 0;
        int byteLen = hex.length() / 2;
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }
        return ret;
    }
}


上一篇:Unity中EXE弹出Windows提示框

下一篇:没有了!

发表评论:

评论记录:

未查询到任何数据!