首页 > 解决方案 > AES/GCM/NoPadding 解密需要 TAG 加密时如何找到 Tag

问题描述

这是我的加密方法也是解密方法它与标签工作正常基本上服务器需要标签服务器是否可以通过我的十六进制字符串找到标签,因为我向服务器发送标签的每个字符串以及使用十六进制,它是否有任何其他最佳解决方案

aesKey=context.getString(R.string.aes_key).getBytes();
iv=context.getString(R.string.input_vector).getBytes();


public Map encrypt(String string){
    byte[] plainText= new byte[0];
    try {
        plainText = string.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("AES/GCM/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey, "AES"), new IvParameterSpec(iv));
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    byte[] cipherText = new byte[0];
    byte[] tag = new byte[0];
    try {

        cipherText = cipher.doFinal(plainText);
        tag= Arrays.copyOfRange(cipherText, cipherText.length -16, cipherText.length);
        cipherText= Arrays.copyOfRange(cipherText, 0, cipherText.length -16);

        Log.d("testing", "encrypt: "+byteArrayToHexString(tag));
        Map<String, Object> map=new HashMap();
        map.put("content",byteArrayToHexString(cipherText));
        map.put("tag",tag);
        Log.d("testing", "encrypt: "+new Gson().toJson(map));
        return map;
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    return new HashMap();
}

服务器需要十六进制,所以我将其转换为十六进制

public  String byteArrayToHexString(final byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for(byte b : bytes){
        sb.append(String.format("%02x", b&0xff));
    }
    return sb.toString();
}

我的解密函数是返回我从服务器接收到的标签和十六进制字符串的字符串

    public String decrypt(String content,byte[] tag){


    byte[] cipherText = hexStringToByteArray(content+byteArrayToHexString(tag));


    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("AES/GCM/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey, "AES"), new IvParameterSpec(iv));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    byte[] plainText = new byte[0];
    try {
        plainText = cipher.doFinal(cipherText);
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }
    try {
        Log.d("testing", "encrypt: plain text:"+new String(plainText,"UTF-8"));
        return new String(plainText,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

标签: androidnode.jscaesar-cipheraes-gcm

解决方案


推荐阅读