android - 如何在 Dart 中使用 encrypt 实现 rsa 加密
问题描述
我有一个用 Koltin 编写的包,我想用 Dart 重写它。我一直在尝试使用encrypt和pointycastle。但我有问题
我将如何在 Dart 中编写这个实现。
我开始尝试对公钥进行编码
var modulusBytes = base64.decode(publicKey!);
import android.util.Base64
import java.security.KeyFactory
import java.security.NoSuchAlgorithmException
import java.security.PublicKey
import java.security.spec.InvalidKeySpecException
import java.security.spec.X509EncodedKeySpec
import javax.crypto.Cipher
object Crypto {
private const val PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADfafwfegRHqfkBiKGn/rrgrgrgrrgg" +
"2wkeSokw2OJrCI+d6YGJPrHHx+nmb/Qn885/R01Gw6d7M824qofmCvkCAwEAAQ=="
private const val ALGORITHM = "RSA"
private const val CIPHER = "RSA/ECB/PKCS1Padding"
private fun encrypt(text: String, key: PublicKey): ByteArray? {
var cipherText: ByteArray? = null
try {
// get an RSA cipher object
val cipher = Cipher.getInstance(CIPHER)
//init cipher and encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key)
cipherText = cipher.doFinal(text.toByteArray())
} catch (e: Exception) {
e.printStackTrace()
}
return cipherText
}
我如何在 Dart 中使用字节数组?
作为尝试在 Koltin 中加密数据的一部分,我也有这个。
@Throws(SecurityException::class)
fun encrypt(text: String): String {
return String(Base64.encode(encrypt(text, getPublicKeyFromString(PUBLIC_KEY)), Base64.NO_WRAP))
}
@Throws(SecurityException::class)
private fun getPublicKeyFromString(pubKey: String): PublicKey {
val key: PublicKey
try {
//init keyFactory
val kf = KeyFactory.getInstance(ALGORITHM)
//decode the key into a byte array
val keyBytes = Base64.decode(pubKey, Base64.NO_WRAP)
//create spec
val spec = X509EncodedKeySpec(keyBytes)
//generate public key
key = kf.generatePublic(spec)
} catch (e: InvalidKeySpecException) {
throw SecurityException("Invalid public key: " + e.message)
} catch (e: NoSuchAlgorithmException) {
throw SecurityException("Invalid public key: " + e.message)
}
return key
}
}
解决方案
我使用了两个插件basic_utils
和 pointycastle
.
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:basic_utils/basic_utils.dart';
import 'package:flutter/services.dart';
import 'package:pointycastle/export.dart';
class Cryptom {
/// String Public Key
String publickey =
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIsL+RHqfkBiKGn/D1y1QnNrMkKzxWP" +
"2wkeSokw2OJrCI+d6YGJPrHHx+nmb/Qn885/R01Gw6d7M824qofmCvkCAwEAAQ==";
String encrypt(String plaintext, String publicKey) {
/// After a lot of research on how to convert the public key [String] to [RSA PUBLIC KEY]
/// We would have to use PEM Cert Type and the convert it from a PEM to an RSA PUBLIC KEY through basic_utils
var pem =
'-----BEGIN RSA PUBLIC KEY-----\n$publickey\n-----END RSA PUBLIC KEY-----';
var public = CryptoUtils.rsaPublicKeyFromPem(pem);
/// Initalizing Cipher
var cipher = PKCS1Encoding(RSAEngine());
cipher.init(true, PublicKeyParameter<RSAPublicKey>(public));
/// Converting into a [Unit8List] from List<int>
/// Then Encoding into Base64
Uint8List output =
cipher.process(Uint8List.fromList(utf8.encode(plaintext)));
var base64EncodedText = base64Encode(output);
return base64EncodedText;
}
String text(String text) {
return encrypt(text, publickey);
}
}
然后运行:
String? result = Cryptom().text("TEXT TO BE ENCRYPTED");
print(result);
推荐阅读
- java - 无法处理全局、每个主机或特定于上下文的 context.xml 文件,因此无法启动 [/AMWeb] 上下文
- php - PHP表单未向指定地址发送电子邮件
- angular - Angular 8 - 每个应用程序设置一次第 3 方组件的最佳方式?
- protractor - 无法在代理后面启动 selenium 服务器以触发量角器测试用例?
- html - 如何在 CSS 中更改按钮值文本
- javascript - 在字符串中用粉笔标记模板文字
- c - OpenMP 任务的随机值生成器错误
- machine-learning - 如何找到 2 个向量之间的距离,其中一些字段是字符串(名称、地址等),而其他字段是数字
- html - 如何放置带有背景监视器图像的视频(使用 html5 视频标签)?
- corda - 如果corda将私钥和公钥保存在Nodes数据库表中,corda如何确保corda-account的隐私?