将 Java RSA 非对称加密转换为 Flutter Dart

ibn*_*mza 5 java encryption dart flutter

我正在尝试将我们的 Android 移动应用程序移植到 Flutter。它是用 Java 编写的。然而,在这一部分中,我需要在发布到服务器之前使用 RSA 加密来加密登录凭据和卡详细信息,但我无法做到这一点。

\n\n

我已经尝试了几个 flutter 包,但不起作用。根据Java开发人员的说法,有一个base64编码的公钥需要用于加密密码。

\n\n

这是Java代码

\n\n
public  static  String Encrypt(String plaintext, String publicKey ) throws Exception {\n  try\n  {\n\n      if(StringUtils.isEmpty(plaintext)) return "";\n      byte[] modulusBytes = Base64.decode(publicKey.getBytes("UTF-8"),Base64.DEFAULT);\n      byte[] exponentBytes = Base64.decode("AQAB".getBytes("UTF-8"),Base64.DEFAULT);\n      BigInteger modulus = new BigInteger(1, modulusBytes );\n      BigInteger exponent = new BigInteger(1, exponentBytes);\n      RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);\n      KeyFactory fact = KeyFactory.getInstance("RSA");\n      PublicKey pubKey = fact.generatePublic(rsaPubKey);\n\n      Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");\n      cipher.init(Cipher.ENCRYPT_MODE, pubKey);\n\n      byte[] plainBytes = new String(plaintext).getBytes("UTF-8");\n      byte[] cipherData = cipher.doFinal( plainBytes );\n\n\n      String outputEncrypted = Base64.encodeToString(cipherData,Base64.NO_WRAP);\n\n      return  outputEncrypted;\n\n  }catch (Exception ex)\n  {\n      Log.i("Exception", ex.getMessage());\n      throw  ex;\n\n  }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我能得到任何帮助将其转换为 dart,以便我可以在 flutter 代码中使用它,我将不胜感激。

\n\n

更新

\n\n

我尝试了 @Richard Heap pointycastle 加密,它似乎工作正常,但服务器无法解密该字符串。抛出异常

\n\n
Interop+AppleCrypto+AppleCFErrorCryptographicException: The operation couldn\xe2\x80\x99t be completed. (OSStatus error -2147415994 - CSSMERR_CSP_INVALID_DATA)\n   at Interop.AppleCrypto.ExecuteTransform(SecKeyTransform transform)\n   at Interop.AppleCrypto.RsaDecrypt(SafeSecKeyRefHandle privateKey, Byte[] data, RSAEncryptionPadding padding)\n   at System.Security.Cryptography.RSAImplementation.RSASecurityTransforms.Decrypt(Byte[] data, RSAEncryptionPadding padding)\n   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)\n\n\nInternal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The parameter is incorrect\n   at Internal.NativeCrypto.CapiHelper.DecryptKey(SafeKeyHandle safeKeyHandle, Byte[] encryptedData, Int32 encryptedDataLength, Boolean fOAEP, Byte[]& decryptedData)\n   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP\n
Run Code Online (Sandbox Code Playgroud)\n\n

服务端的解密方法是用C#编写的

\n\n

更新2

\n\n

经过几个小时的谷歌搜索后,我终于找到了这个github 问题 PointyCastle和 duncanhoggan 的解决方案。结果我只需要使用 PKCS1Encoding。

\n\n
  var pubKey = RSAPublicKey(modulus, exponent);\n  var cipher = PKCS1Encoding(RSAEngine());\n  cipher.init(true, PublicKeyParameter<RSAPublicKey>(pubKey));\n  Uint8List output = cipher.process(utf8.encode(text));\n  var base64EncodedText = base64Encode(output);\n  return base64EncodedText;\n
Run Code Online (Sandbox Code Playgroud)\n\n

@理查德·希普,感谢您的帮助。

\n

Ric*_*eap 2

尝试这个:

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),
  );
}
Run Code Online (Sandbox Code Playgroud)