小工具整理
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
-- 使用非对称加密进行加密的函数
|
||||
fn encryptWithPublicKey plaintext publicKey =
|
||||
(
|
||||
rsaProvider = dotnetobject "System.Security.Cryptography.RSACryptoServiceProvider"
|
||||
rsaProvider.FromXmlString(publicKey)
|
||||
methodpublickey = (dotnetclass "System.Security.Cryptography.RSAEncryptionPadding").Pkcs1
|
||||
|
||||
-- 将文本转换为字节数组
|
||||
plaintextBytes = (dotnetclass "System.Text.Encoding").UTF8.GetBytes(plaintext)
|
||||
|
||||
-- 使用公钥加密字节数组
|
||||
encryptedData = rsaProvider.Encrypt plaintextBytes methodpublickey
|
||||
|
||||
|
||||
return encryptedData
|
||||
)
|
||||
|
||||
|
||||
-- 要加密的文字内容
|
||||
plaintext = "我是机密,不能告诉其他人。"
|
||||
|
||||
-- 生成公钥和私钥
|
||||
rsaProvider = dotnetobject "System.Security.Cryptography.RSACryptoServiceProvider"
|
||||
publicKey = rsaProvider.ToXmlString(false)
|
||||
privateKey = rsaProvider.ToXmlString(true)
|
||||
|
||||
-- 将私钥保存到文件
|
||||
privateKeyFilePath = getFilenamePath(getThisScriptFilename()) +"privateKey.xml"
|
||||
streamWriter = dotnetobject "System.IO.StreamWriter" privateKeyFilePath
|
||||
streamWriter.WriteLine(privateKey)
|
||||
streamWriter.Close()
|
||||
|
||||
|
||||
-- 使用公钥加密
|
||||
encryptedData = encryptWithPublicKey plaintext publicKey
|
||||
|
||||
-- 将加密后的字节数组转换为Base64字符串
|
||||
encryptedDataString = (dotnetclass "System.Convert").ToBase64String(encryptedData)
|
||||
|
||||
-- 将加密后的数据保存到文件
|
||||
encryptedDataFilePath = getFilenamePath(getThisScriptFilename()) +"data.txt"
|
||||
streamWriter = dotnetobject "System.IO.StreamWriter" encryptedDataFilePath
|
||||
streamWriter.WriteLine(encryptedDataString)
|
||||
streamWriter.Close()
|
||||
Reference in New Issue
Block a user