flutter,碰到一个AES/CFB/NOPADDING加密,java加密的内容,无法和flutter中互解,

尝试了网上多种库和教程都没办法互解,JAVA代码如下,求个大佬帮忙用dart(Flutter)实现一个可以互解的aes加密代码

package com.example.lib;

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

public class Test {
    
    static String keyStr = "620F15CFDB5C79C34B3940537B21EDA072E22F5D7151456DEC3932D7A2B22C53";
    static String ivStr = "85D7D7DA41E22C1A66C9C1BFC70A1088";

    public static void main(String[] args) {

        String haha = encrypt("哈哈!你好。");//B7488CC936D5FF626F50900F99CEB2E13D99

        decrypt(haha);

    }

    static String encrypt(String content) {
        try {
            byte[] ivByte = hex2byte(ivStr);
            byte[] key = hex2byte(keyStr);

            Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(ivByte);
            cipher.init(1, keySpec, ivSpec);
            byte[] doFinal = cipher.doFinal(content.getBytes("UTF-8"));
            System.out.println("doFinale:" + byte2hex(doFinal));
            return byte2hex(doFinal);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static void decrypt(String content) {

        try {
            byte[] ivByte = hex2byte(ivStr);
            byte[] contentByte = hex2byte(content);
            byte[] key = hex2byte(keyStr);
            Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(ivByte);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] result = cipher.doFinal(contentByte);
            System.out.println(new String(result, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] hex2byte(String str) {
        if (str == null) {
            return null;
        }
        int length = str.length();
        if (length % 2 == 1) {
            return null;
        }
        byte[] bArr = new byte[length / 2];
        for (int i = 0; i != length / 2; i++) {
            int j = i * 2;
            bArr[i] = (byte) Integer.parseInt(str.substring(j, j + 2), 16);
        }
        return bArr;
    }

    public static String byte2hex(byte[] bArr) {
        String str = "";
        for (byte b : bArr) {
            String hexString = Integer.toHexString(b & 255);
            str = hexString.length() == 1 ? str + "0" + hexString : str + hexString;
        }
        return str.toUpperCase();
    }
}
阅读 3.8k
2 个回答
新手上路,请多包涵

我也遇到了这个问题,楼主解决了没

新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏