Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...