吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1123|回复: 0
收起左侧

Flutter(Dart) Aes加密相关

[复制链接]
xubo5200 发表于 2021-1-13 10:35
200吾爱币
本帖最后由 xubo5200 于 2021-1-13 10:36 编辑

我是做安卓开发的,最近在学习flutter,然后碰到一个AES/CFB/NOPADDING加密问题,java加密的内容,无法和flutter中互解,尝试了网上多种库和教程都没办法互解,求个大佬给我用dart语言解析下  


java代码如下
[Java] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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();
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-8-13 13:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表