需要先引入pointycastle和basic_utils flutter pub add pointycastle flutter pub add basic_utils 封装pemString 转换成 RSAPublicKey方法 RSAPublicKey _parsePublicKey(String publicKey) { var pem = '-----BEGIN RSA PUBLIC KEY-----\n$publicKey\n-----END RSA PUBLIC KEY-----'; return CryptoUtils.rsaPublicKeyFromPem(pem); } 实现公钥加密 String _encryptRSA(String data, RSAPublicKey publicKey) { Uint8List bytes = Uint8List.fromList(utf8.encode(data)); var engine = PKCS1Encoding(RSAEngine()); engine.init(true, PublicKeyParameter<RSAPublicKey>(publicKey)); Uint8List encrypted = engine.process(bytes); String encryptedBase64 = base64.encode(encrypted); return encryptedBase64; }