将Java RSA非对称加密转换为颤振镖

将Java RSA非对称加密转换为颤振镖,java,encryption,flutter,dart,Java,Encryption,Flutter,Dart,我正在尝试移植我们的android移动应用程序。它是用Java编写的。但是,在这一部分中,我需要使用RSA加密对登录凭证和卡的详细信息进行加密,然后才能将其发送到我无法正确获取的服务器 我试过几个不起作用的颤振软件包。根据Java开发者的说法,有一个经过base64编码的公钥需要用于加密密码 下面是Java代码 public static String Encrypt(String plaintext, String publicKey ) throws Exception { try

我正在尝试移植我们的android移动应用程序。它是用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;

  }

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

更新

我尝试了@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

经过数小时的谷歌搜索,我终于找到了邓肯霍根的解决方案。结果我只需要使用PKCS1编码

  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;
var pubKey=RSAPublicKey(模数、指数);
var cipher=PKCS1编码(RSAEngine());
init(true,PublicKeyParameter(pubKey));
Uint8List输出=cipher.process(utf8.encode(text));
var base64EncodedText=base64Encode(输出);
返回base64EncodedText;
@理查德·希普,谢谢你的帮助。

试试这个:

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),
  );
}
import'dart:math';
导入“dart:convert”;
导入“dart:键入的_数据”;
导入“package:convert/convert.dart”;
导入“包:pointycastle/api.dart”;
导入“包:pointycastle/asymetric/api.dart”;
导入“包:pointycastle/asymetric/rsa.dart”;
字符串加密(字符串明文、字符串公钥){
var modulesBytes=base64.decode(公钥);
var module=BigInt.parse(十六进制编码(modulesBytes),基数:16);
var exponent=BigInt.parse(十六进制编码(base64.decode('AQAB')),基数:16);
var engine=RSAEngine()
…初始化(
是的,
PublicKeyParameter(RSAPublicKey(模数、指数)),
);
//PKCS1.5填充
var k=modulesBytes.length;
var plainBytes=utf8.encode(明文);
var paddingLength=k-3-plainBytes.length;
var eb=Uint8List(paddingLength+3+plainBytes.length);
var r=Random.secure();
eb.setRange(paddingLength+3,eb.length,纯字节);
eb[0]=0;
eb[1]=2;
eb[填充长度+2]=0;
对于(int i=2;i
你试过尖头城堡吗?你在哪里卡住了?谢谢Richard。听说过PointyCastle,但还没有真正尝试过使用它。我想dart会有一些内置的加密服。将尝试您的解决方案。我没有任何加密问题,但服务器无法解密我的加密字符串。我进行了调试,它在解密时抛出了一个异常。我更新了我的问题,但有一个例外。Dart,按设计,很少有内置的。这种附加库是在包中添加的,比如pointy castle。使用PKCSv1.5填充实现更新了问题。