Welcome to OverStack Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OverStack Developer Community-Open, Learning and Share
...