首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >flutter属于前端还是后端(kotlin比较flutter)

flutter属于前端还是后端(kotlin比较flutter)

作者头像
全栈程序员站长
发布2022-07-25 13:16:03
发布2022-07-25 13:16:03
9520
举报

大家好,又见面了,我是你们的朋友全栈君。

尝试了网上多种库和教程都没办法互解,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();

}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127422.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档