java - 将 Java RSA 非对称加密转换为 Flutter Dart
问题描述
我正在尝试将我们的 android 移动应用程序移植到 Flutter。它是用 Java 编写的。但是,在这部分中,我需要使用 RSA 加密对登录凭据和卡详细信息进行加密,然后再将其发布到我无法正确处理的服务器。
我已经尝试了几个不起作用的颤振包。根据 Java 开发人员的说法,有一个 base64 编码的公钥需要用于加密密码。
这是Java代码
public static String Encrypt(String plaintext, String publicKey ) throws Exception {
try
{
if(StringUtils.isEmpty(plaintext)) return "";
byte[] modulusBytes = Base64.decode(publicKey.getBytes("UTF-8"),Base64.DEFAULT);
byte[] exponentBytes = Base64.decode("AQAB".getBytes("UTF-8"),Base64.DEFAULT);
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, exponentBytes);
RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] plainBytes = new String(plaintext).getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String outputEncrypted = Base64.encodeToString(cipherData,Base64.NO_WRAP);
return outputEncrypted;
}catch (Exception ex)
{
Log.i("Exception", ex.getMessage());
throw ex;
}
}
如果我能得到任何帮助将其转换为飞镖,我将不胜感激,以便我可以在颤振代码中使用它。
更新
我尝试了@Richard Heap pointycastle 加密,它似乎工作正常,但服务器无法解密字符串。抛出异常
Interop+AppleCrypto+AppleCFErrorCryptographicException: The operation couldn’t be completed. (OSStatus error -2147415994 - CSSMERR_CSP_INVALID_DATA)
at Interop.AppleCrypto.ExecuteTransform(SecKeyTransform transform)
at Interop.AppleCrypto.RsaDecrypt(SafeSecKeyRefHandle privateKey, Byte[] data, RSAEncryptionPadding padding)
at System.Security.Cryptography.RSAImplementation.RSASecurityTransforms.Decrypt(Byte[] data, RSAEncryptionPadding padding)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The parameter is incorrect
at Internal.NativeCrypto.CapiHelper.DecryptKey(SafeKeyHandle safeKeyHandle, Byte[] encryptedData, Int32 encryptedDataLength, Boolean fOAEP, Byte[]& decryptedData)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP
服务端的解密方法是用C#写的
更新 2
经过数小时的谷歌搜索后终于让它工作了 原来我只需要使用 PKCS1Encoding。
var pubKey = RSAPublicKey(modulus, exponent);
var cipher = PKCS1Encoding(RSAEngine());
cipher.init(true, PublicKeyParameter<RSAPublicKey>(pubKey));
Uint8List output = cipher.process(utf8.encode(text));
var base64EncodedText = base64Encode(output);
return base64EncodedText;
@Richard Heap,感谢您的帮助。
解决方案
尝试这个:
import 'dart:math';
import 'dart:convert';
import 'dart:typed_data';
import 'package:convert/convert.dart';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'package:pointycastle/asymmetric/rsa.dart';
String encrypt(String plaintext, String publicKey) {
var modulusBytes = base64.decode(publicKey);
var modulus = BigInt.parse(hex.encode(modulusBytes), radix: 16);
var exponent = BigInt.parse(hex.encode(base64.decode('AQAB')), radix: 16);
var engine = RSAEngine()
..init(
true,
PublicKeyParameter<RSAPublicKey>(RSAPublicKey(modulus, exponent)),
);
//PKCS1.5 padding
var k = modulusBytes.length;
var plainBytes = utf8.encode(plaintext);
var paddingLength = k - 3 - plainBytes.length;
var eb = Uint8List(paddingLength + 3 + plainBytes.length);
var r = Random.secure();
eb.setRange(paddingLength + 3, eb.length, plainBytes);
eb[0] = 0;
eb[1] = 2;
eb[paddingLength + 2] = 0;
for (int i = 2; i < paddingLength + 2; i++) {
eb[i] = r.nextInt(254) + 1;
}
print(plainBytes.length);
print(eb);
return base64.encode(
engine.process(eb),
);
}
推荐阅读
- adobe-xd - XD 插件 API 支持什么语言?
- c# - 参数类型“E”不可分配给参数类型“TE”
- push-notification - Actions on Google - C# 中的推送通知
- arrays - 如何制作Account用户生成器c++(问题:制作对象数组并动态扩展)
- visual-studio-code - 如何在vscode中启用非空白字符后的引号自动完成?
- sql - INSERT INTO 语句中的换行符
- python - 使用 Python 从 c++ dll 返回数据
- php - 在日历中显示事件 | 代码点火器
- python - 从基于 Web 的表格中抓取时如何获取更多详细信息?
- javascript - 同一文件上的多个ajax调用以在同一文件中执行不同的代码?