微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AES / GCM / NoPadding解密需要TAG加密时如何查找标签

如何解决AES / GCM / NoPadding解密需要TAG加密时如何查找标签

这是我的加密方法,也是解密方法,它可以与Tag正常工作基本上服务器需要TAG,服务器是否有可能通过我的十六进制字符串找到Tag,因为我发送给服务器的每个字符串以及Hex都具有其他最佳值,解决方

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,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 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。