将Java RSA非对称加密转换为Flutter Dart [英] Convert Java RSA Asymmetric encryption to Flutter Dart
问题描述
我正在尝试将我们的Android移动应用程序移植到Flutter.它是用Java编写的.但是,在此部分中,我需要先使用RSA加密对登录凭据和卡详细信息进行加密,然后再将其发布到服务器上,但我仍然无法正确操作.
I'm trying to port our android mobile app to Flutter. It was written in Java. However, there is this part where I need to encrypt the login credential and card details with RSA encryption before posting to the server which I've not been able to get right.
我尝试了几种无效的flutter程序包.根据Java开发人员的说法,有一个使用base64编码的公钥,需要在加密密码时使用它.
I've tried several flutter packages which doesn't work. According to the Java developer, there is a public key which is base64 encoded that needs to be used in encrypting the password.
这是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;
}
}
如果能将其转换为dart以便能在flutter代码中使用它,我将不胜感激.
I'll appreciate if I can get any help to convert this to dart so I can use it in the flutter code.
更新
我尝试了@Richard Heap pointycastle加密,该加密似乎可以正常工作,但服务器无法解密该字符串.抛出异常
I tried @Richard Heap pointycastle encryption which seems to work fine but the server was unable to decrypt the string. Exception thrown
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#编写的
The decryption method on the server is written with C#
更新2
经过数小时的搜索后,终于使它工作了,我登陆了PointyCastle上的这个 github问题和duncanhoggan的解决方案.原来,我只需要使用PKCS1Encoding.
Finally got this to work after hours of googling I landed on this github issue on PointyCastle and the solution by duncanhoggan. Turned out I just needed to use 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,感谢您的帮助.
@Richard Heap, thanks for helping.
推荐答案
尝试一下:
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),
);
}
这篇关于将Java RSA非对称加密转换为Flutter Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!