- 概述
- 加密
- 数据库
- Java
- Python
- 网页 API
适用于“加密”活动包的编码自动化 API,包括对称加密、密钥散列和 PGP。
UiPath.Cryptography.Activities
编码工作流 API,用于对称加密和解密、密钥散列以及 PGP 加密、解密、签名、明文签名、验证和密钥生成。在设计编码自动化时使用这些 API。访问编码自动化,了解编码自动化以及如何使用 API 设计这些自动化。
- 服务访问者:
cryptography(类型ICryptographyService) - 所需的包:
"UiPath.Cryptography.Activities": "*"依赖项中的project.json。
自动导入的命名空间
安装此包后,这些命名空间将自动在编码工作流中可用:
System
System.IO
System.Text
UiPath.Cryptography
UiPath.Cryptography.Activities
UiPath.Cryptography.Activities.API
UiPath.Cryptography.Enums
System
System.IO
System.Text
UiPath.Cryptography
UiPath.Cryptography.Activities
UiPath.Cryptography.Activities.API
UiPath.Cryptography.Enums
服务概述
cryptography服务将所有操作公开为直接方法调用。没有可打开的连接、句柄或作用域。它被注册为无状态单例,因此可以在工作流中共享访问器,并且可以安全地并发调用其方法。直接在服务访问器上调用方法:
var key = PasswordKey.FromPassword("mykey", Encoding.UTF8);
var ciphertext = cryptography.EncryptText("secret", EncryptionAlgorithm.AESGCM, SymmetricEncryptOptions.Classic(key));
var key = PasswordKey.FromPassword("mykey", Encoding.UTF8);
var ciphertext = cryptography.EncryptText("secret", EncryptionAlgorithm.AESGCM, SymmetricEncryptOptions.Classic(key));
字节、文本和文件表单
每个逻辑运算都会公开三种输入/输出表单。选择与您已有数据匹配的数据:
| 表单 | 后缀 | 输入 → 输出 | 何时使用 |
|---|---|---|---|
| 字节 | (基本) | byte[] → byte[] | 二进制或已加载的数据。 |
| 文本 | ...Text | string → string (Base64 或 ASCII-armored) | 数据以文本形式到达(HTTP、配置、环境)。 |
| 文件 | ...File | 文件路径 → 文件路径 | 驻留在磁盘上的数据。 |
密钥材料
对称和密钥哈希操作将密钥材料视为两个具体CryptoKey子类型之一。您选取的类决定了可与密钥一起使用的传输格式,类型系统会在编译时通过格式工厂强制执行该格式。
PasswordKey
要以 PAKD F2 方式拉伸为密码密钥的密码材料。与Classic 、 Owasp2026和OpenSslEnc连线格式一起使用。
| 工厂 | 用途 |
|---|---|
PasswordKey.FromPassword(string password, Encoding encoding) | string形式的密码或密码短语。 |
PasswordKey.FromPassword(SecureString password, Encoding encoding) | 同样,源自密码存储或用户输入。 |
PasswordKey在内部将密码存储为SecureString ( string工厂将输入字符复制到一个 中),并在每次操作时即时具体化密码密钥字节。字节仅存在于操作的堆栈框架上,并且永远不会固定到PasswordKey实例;中间缓冲区在每次实现后都会被清零。
PasswordKey是IDisposable :调用Dispose()会立即将受保护的缓冲区归零,并且后续的KeyBytes访问将引发ObjectDisposedException 。对于长时间存在的工作流,建议处置。
RawKey
算法确切所需长度的文本密码密钥(例如,32 个字节的 AES-256)。与Raw连线格式一起使用。未应用 KFS。RawKey为IDisposable :调用Dispose()会将保留的密钥字节就地归零。
| 工厂 | 用途 |
|---|---|
RawKey.FromBytes(byte[] keyBytes) | 密钥已作为字节加载。 |
RawKey.FromHex(string hex) | 编码为十六进制的密钥。 |
RawKey.FromBase64(string base64) | 编码为 Base64 的密钥。 |
密钥长度未经工厂验证。当您调用加密或解密方法时,系统会对其进行检查,并且所选算法的非法长度会引发ArgumentException列出合法字节长度(例如,对于 AES,为 16、24 或 32)。请准确使用算法所需的密钥大小构建密钥。
键控哈希方法接受任一子类型,因为它们直接采用CryptoKey并且没有连线格式的轴。
对称选项
SymmetricEncryptOptions和SymmetricDecryptOptions捆绑密钥、电汇格式以及任何格式特定于的值( Raw的 PV、 Owasp2026和OpenSslEnc的 KFS 迭代)。通过格式工厂构建它们。工厂的密钥参数类型会在编译时强制执行密钥种类和传输格式配对,因此PasswordKey不能传递给Raw(...) , RawKey不能传递给Classic(...) 。
| 工厂 | 格式 | 密钥类型 | 注意 |
|---|---|---|---|
SymmetricEncryptOptions.Classic(PasswordKey key, Encoding encoding = null) | Classic | PasswordKey | 默认。冻结连线格式,以实现向后兼容性(迭代 10,000 次时的 VBKFS2-HMAC-SHA1)。 |
SymmetricEncryptOptions.Owasp2026(PasswordKey key, int kdfIterations = 1_300_000, Encoding encoding = null) | Owasp2026 | PasswordKey | 与传统布局相同,在 OWASP 的 2026 年建议迭代计数中使用 VBKdF2-HMAC-SHA1。 |
SymmetricEncryptOptions.Raw(RawKey key, byte[] iv = null, Encoding encoding = null) | Raw | RawKey | 调用者提供的密钥和初始化向量,用于实现第三方互操作性。 |
SymmetricEncryptOptions.OpenSslEnc(PasswordKey key, int kdfIterations = 600_000, Encoding encoding = null, AesKeySize aesKeySize = AesKeySize.Aes256) | OpenSslEnc | PasswordKey | openssl enc- 兼容( Salted__幻觉与 VBKF2-HMAC-SHA256)。 aesKeySize选择 AES-128、-192 或 -256 以匹配对等体的openssl enc -aes-N-cbc 。 |
SymmetricDecryptOptions工厂采用相同的形状,不同之处在于解密端没有初始化向量,因为初始化向量是从密文流中自动读取的。可选的encoding:参数设置选项的文本编码(默认为 UTF-8),并且只有EncryptText和DecryptText会查阅该参数。
构造的选项对象是只读的,但可自省: CryptoOptions公开获取方法Key 、 Format 、 KdfIterations和TextEncoding ; SymmetricEncryptOptions还公开了IV和AesKeySize ;并且SymmetricDecryptOptions会公开AesKeySize 。
输入向量和加点值策略
默认情况下,所有对称加密方法都是非确定性的:每次调用时都会生成新的随机 8 字节salt(如果适用)和初始化向量或随机数,并将其嵌入到密文流中。对相同的明文进行两次加密始终会产生不同的密文,并且匹配的解密方法会自动从同一流中重建salt 和初始化向量。
- CMS 系列算法(
AES、Rijndael、DES、TripleDES、RC2)使用 PKCS7 填充、GbE 模式和随机初始化向量。 AESGCM是“带有关联数据的身份验证加密”(AEAD),具有一个 96 位随机数和一个 128 位身份验证标签。推荐用于新工作流。ChaCha20Poly1305是 AES-GCM 的 AEAD 替代方案。
对于Raw ,您可以通过SymmetricEncryptOptions.Raw(key, iv)提供显式初始化变量。传递null (工厂默认设置),让密码生成密码。
PGP 密钥材料
PGP 方法采用强类型密钥句柄。只需构建一次,即可在调用中重复使用。
| 工厂 | 用途 |
|---|---|
PgpPublicKey.FromBytes(byte[] keyBytes) | 内存字节(ASCII 防护或二进制)中的公钥。 |
PgpPublicKey.FromFilePath(string path) | 从.asc或.gpg文件加载公钥。 |
PgpPrivateKey.FromBytes(byte[] keyBytes, string passphrase) | 私钥和密码短语,绑定在一起。 |
PgpPrivateKey.FromBytes(byte[] keyBytes, SecureString passphrase) | 相同,只是使用SecureString密码短语。 |
PgpPrivateKey.FromFilePath(string path, string passphrase) | 文件中的私钥。 |
PgpPrivateKey.FromFilePath(string path, SecureString passphrase) | 文件中的私钥,密码短语为SecureString 。 |
每个密钥句柄还公开两个实例方法:
| 成员 | 用途 |
|---|---|
byte[] ToBytes() | 返回已加载的密钥字节的副本:如果密钥是从防护输入创建,则返回 ASCII 防护;如果从二进制文件加载,则返回二进制。PgpGenerateKeys返回的密钥为 ASCII 密钥。 |
void Save(string filePath, bool overwrite = false) | 将密钥写入磁盘。如果文件存在并且overwrite为false ,则引发InvalidOperationException 。 |
PgpKeyPair (由PgpGenerateKeys返回,或直接使用new PgpKeyPair(publicKey, privateKey)构造)保存匹配的公共/私有对。使用pair.PublicKey和pair.PrivateKey ,或使用var (pub, priv) = pair;进行解构。当您需要磁盘上的文件时,使用pair.PublicKey.Save(path)和pair.PrivateKey.Save(path)保留内容。
将PgpPrivateKey传递给加密方法意味着签名,而将PgpPublicKey传递给解密方法意味着签名验证。不使用单独的bool sign或bool verifySignature标志。
PgpPrivateKey在实例的生命周期内将其密码短语存储为SecureString 。仅在每个加密操作期间,它会具体化为托管string ,因为基础 BuncyCastle 库需要明文string 。PgpPrivateKey为IDisposable :调用Dispose()立即将受保护的缓冲区清零。PgpPublicKey不持有机密材料,也不是IDisposable 。
与活动的关系
编码 API 和加密 XAML 活动在同一加密核心上运行,因此它们在以下方面完全对等:
- 算法和键控哈希算法
- 传输格式(
Classic、Owasp2026、Raw、OpenSslEnc) - PV、KdF 迭代、AES 密钥大小和编码选项
- 每个 PGP 操作
任何可以通过活动计算的内容,都可以使用此服务计算。差异在于输入/输出形状和按键输入形式,与功能无关。
编码 API 执行的不仅仅是以下活动:
| 仅限编码功能 | 详细信息 |
|---|---|
| 每次操作的输入/输出字节数 | EncryptBytes / DecryptBytes 、 KeyedHashBytes 、 PgpEncryptBytes / PgpDecryptBytes 、 PgpSignBytes / PgpClearSignBytes 、 PgpVerifyBytes / PgpVerifyClearSignedBytes 。没有活动具有byte[]表单。 |
原始byte[]键 | RawKey.FromBytes(byte[]) 适用于对称Raw和键控哈希。活动只能提供十六进制或 Base64 字符串形式的原始密钥。 |
| 内存 PGP 密钥材料 | PgpPublicKey.FromBytes 和PgpPrivateKey.FromBytes可让您在磁盘上没有密钥文件的情况下进行操作。这些活动需要密钥文件。 |
| 文本和字节签名/清除签名/验证 | PgpSignText / PgpClearSignText 、 PgpVerifyText / PgpVerifyClearSignedText及其字节形式。这些活动仅公开了基于文件的签名、明文签名和验证。 |
内存中 PgpKeyPair | PgpGenerateKeys 返回可用的密钥对,而不会强制输出文件。 |
这些活动会执行编码 API 无法执行的两项操作,两者都不是加密功能:
- UiPath 资源句柄(
IResource/ILocalResource) 作为输入和输出。编码 API 采用纯string文件路径和byte[]。 - “出错时继续”吞吐并继续行为。在编码工作流中,请改用普通的
try/catch,因为每种方法都会在失败时引发。
文本活动将按键编码和明文编码拆分为两个属性。在编码 API 中,密码编码通过选项PasswordKey.FromPassword(..., encoding)参数设置,明文编码通过选项encoding:参数设置。两个轴仍可独立控制。
从之前的编码 API 迁移
上一版本中引入的编码 API API 已在此版本中整合。每次操作,旧的调用形状(带有SecureString Encoding string byte[]重载,以及基于路径的PgpGenerateKeys )将替换为一个基于选项的形状,因此键-种类和连接格式在编译时强制配对。
没有[Obsolete]填充程序:必须更新针对之前 API 编写的代码,才能根据此包进行编译。
| 之前 | 之后 |
|---|---|
EncryptBytes(input, algo, string key, Encoding enc) | EncryptBytes(input, algo, SymmetricEncryptOptions.Classic(PasswordKey.FromPassword(key, enc))) |
EncryptBytes(input, algo, SecureString key, Encoding enc) | EncryptBytes(input, algo, SymmetricEncryptOptions.Classic(PasswordKey.FromPassword(key, enc))) |
EncryptBytes(input, algo, byte[] keyBytes) | EncryptBytes(input, algo, SymmetricEncryptOptions.Raw(RawKey.FromBytes(keyBytes))) |
EncryptText(input, algo, key, enc) | EncryptText(input, algo, SymmetricEncryptOptions.Classic(PasswordKey.FromPassword(key, enc), enc)) |
EncryptFile(in, out, algo, key, enc, overwrite) | EncryptFile(in, out, algo, SymmetricEncryptOptions.Classic(PasswordKey.FromPassword(key, enc)), overwrite) |
DecryptBytes / DecryptText / DecryptFile | 相同的形状,但带有SymmetricDecryptOptions.<Format>(...) 。 |
KeyedHashBytes(input, algo, string key, Encoding enc) | KeyedHashBytes(input, algo, PasswordKey.FromPassword(key, enc)) |
KeyedHashBytes(input, algo, byte[] keyBytes) | KeyedHashBytes(input, algo, RawKey.FromBytes(keyBytes)) |
KeyedHashText(input, algo, string key, Encoding enc) | KeyedHashText(input, algo, PasswordKey.FromPassword(key, enc), enc) |
KeyedHashFile(inputPath, algo, string key, Encoding enc) | KeyedHashFile(inputPath, algo, PasswordKey.FromPassword(key, enc)) |
PgpGenerateKeys(publicKeyPath, privateKeyPath, userId, passphrase, keySize) | var pair = PgpGenerateKeys(userId, passphrase, keySize); pair.PublicKey.Save(publicKeyPath); pair.PrivateKey.Save(privateKeyPath); |
由于每个键和选项类型都是新的,因此引用旧重载的代码将无法编译,而是会静默地采用不同的重载。默认文本编码仍为 UTF-8,因此除了选项重构之外,依赖于先前 UTF-8 默认值的代码不需要更改任何行为。
对称加密
byte[] EncryptBytes(byte[] input, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options)
加密任意字节。options参数携带密钥和传输格式。根据所选连线格式返回密文。
string EncryptText(string input, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options)
加密字符串,并以 Base64 编码的密文形式返回结果。明文编码从options.TextEncoding读取,默认为 UTF-8。
void EncryptFile(string inputPath, string outputPath, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options, bool overwrite = false)
读取文件,对其进行加密,然后写入结果。如果outputPath存在并且overwrite为false ,则引发InvalidOperationException 。
对称解密
byte[] DecryptBytes(byte[] input, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options)
对EncryptBytes生成的密文进行解密。options.Format必须与加密时使用的格式匹配。返回明文字节。
string DecryptText(string input, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options)
对EncryptText生成的 Base64 编码密文进行解密并返回明文。明文编码从options.TextEncoding读取,必须与加密时使用的编码匹配,默认为 UTF-8。
void DecryptFile(string inputPath, string outputPath, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options, bool overwrite = false)
读取加密文件并写入明文。如果outputPath存在并且overwrite为false ,则引发InvalidOperationException 。
键控散列
密钥哈希方法计算 HMAC(对于非 HMAC 算法,则为明文哈希),并将结果作为大写的十六进制字符串返回。该运算是单向的,没有逆运算。
string KeyedHashBytes(byte[] input, KeyedHashAlgorithms algorithm, CryptoKey key)
string KeyedHashText(string input, KeyedHashAlgorithms algorithm, CryptoKey key, Encoding encoding = null)
string KeyedHashFile(string inputPath, KeyedHashAlgorithms algorithm, CryptoKey key)
KeyedHashText上的可选encoding参数可控制如何在散列之前将输入字符串转码为字节,默认为 UTF-8。KeyedHashBytes对原始字节进行操作,而KeyedHashFile对文件内容进行逐字节哈希处理,因此两者都没有编码轴。
PGP 加密
如果提供了signer ,则加密的有效负载也会使用该私钥进行签名。
byte[] PgpEncryptBytes(byte[] input, PgpPublicKey recipient, PgpPrivateKey signer = null)
string PgpEncryptText(string input, PgpPublicKey recipient, PgpPrivateKey signer = null)
void PgpEncryptFile(string inputPath, string outputPath, PgpPublicKey recipient, PgpPrivateKey signer = null, bool overwrite = false)
PGP 解密
如果提供了verifier ,则将在解密期间验证嵌入式签名。
byte[] PgpDecryptBytes(byte[] input, PgpPrivateKey recipient, PgpPublicKey verifier = null)
string PgpDecryptText(string input, PgpPrivateKey recipient, PgpPublicKey verifier = null)
void PgpDecryptFile(string inputPath, string outputPath, PgpPrivateKey recipient, PgpPublicKey verifier = null, bool overwrite = false)
PGP 签名(二进制签名)
生成二进制签名的有效负载。使用PgpVerify*方法进行验证。
byte[] PgpSignBytes(byte[] input, PgpPrivateKey signer)
string PgpSignText(string input, PgpPrivateKey signer)
void PgpSignFile(string inputPath, string outputPath, PgpPrivateKey signer, bool overwrite = false)
PGP 明文签名
明文签名可让附加签名的原始内容可读。使用PgpVerifyClearSigned*方法进行验证。
byte[] PgpClearSignBytes(byte[] input, PgpPrivateKey signer)
string PgpClearSignText(string input, PgpPrivateKey signer)
void PgpClearSignFile(string inputPath, string outputPath, PgpPrivateKey signer, bool overwrite = false)
PGP 验证
二进制签名
验证PgpSign*方法(或由PgpEncrypt*使用签名者生成)生成的有效负载。
| 方法 | 签名 |
|---|---|
| 字节 | bool PgpVerifyBytes(byte[] input, PgpPublicKey verifier) |
| 文本 | bool PgpVerifyText(string input, PgpPublicKey verifier) |
| 文件 | bool PgpVerifyFile(string inputPath, PgpPublicKey verifier) |
签名有效时返回true ,否则返回false 。
明文签名
验证PgpClearSign*方法生成的有效负载。
| 方法 | 签名 |
|---|---|
| 字节 | bool PgpVerifyClearSignedBytes(byte[] input, PgpPublicKey verifier) |
| 文本 | bool PgpVerifyClearSignedText(string input, PgpPublicKey verifier) |
| 文件 | bool PgpVerifyClearSignedFile(string inputPath, PgpPublicKey verifier) |
公钥格式正确
确认PgpPublicKey实例解析为格式正确的 OpenPGP 公钥。这将反映PGP 验证活动的验证公钥验证类型。
bool PgpVerifyPublicKey(PgpPublicKey key)
当密钥有效时,返回true 。
PGP 密钥对生成
在内存中生成 OpenPGP RSA 密钥对,并返回两边作为匹配的PgpKeyPair 。通过在每一半调用Save(path)来暂留。
PgpKeyPair PgpGenerateKeys(string userId, string passphrase, RsaKeySize keySize = RsaKeySize.Rsa4096)
PgpKeyPair PgpGenerateKeys(string userId, SecureString passphrase, RsaKeySize keySize = RsaKeySize.Rsa4096)
参数:
userId(string) - OpenPGP 用户 ID;传统上为 RFC 2822 邮箱,例如Alice Doe <alice@example.com>。passphrase- 用于保护生成的私钥的密码短语。绑定到返回的PgpPrivateKey。keySize(RsaKeySize) - RSA 密钥大小。默认为Rsa4096。接受Rsa3072和Rsa2048以实现与旧版系统的互操作性。
返回PgpKeyPair ,公开pair.PublicKey和pair.PrivateKey 。
枚举引用
EncryptionAlgorithm
由对称加密和解密方法使用。
| 值 | 注意 |
|---|---|
AESGCM | 具有 96 位随机数和 128 位身份验证标签的 AES-GCM。EAD。推荐用于新工作流。 |
ChaCha20Poly1305 | ChaCha20-Poly1305 AEAD。非 FIPS。AES-GCM 的替代方案。 |
AES | TLS 模式下的 AES。 |
Rijndael | Cube 模式下的 Rijndael。过时且较弱;避免。 |
DES | Cube 模式下的 DES。过时且较弱;避免。 |
TripleDES | Cube 模式下的 3DES。过时且较弱;避免。 |
RC2 | Cube 模式下的 RC2。过时且较弱;避免。 |
PGP | 保留。请改用专用的PgpEncrypt*和PgpDecrypt*方法。 |
SymmetricWireFormat
由SymmetricEncryptOptions.Format和SymmetricDecryptOptions.Format使用。
| 值 | 注意 |
|---|---|
Classic | UiPath 的字节稳定布局,迭代次数为 10,000 次的 VBKFS2-HMAC-SHA1。默认。已冻结以向后兼容。 |
Owasp2026 | OWASP 建议的迭代计数 (1,300,000) 的传统布局。调用者可以通过kdfIterations覆盖它。 |
Raw | IV ‖ ciphertext [‖ tag]。调用者提供文本键(也可以提供初始化向量)。为了实现第三方互操作性。 |
OpenSslEnc | Salted__ ‖ salt(8) ‖ ciphertext [‖ tag]、默认情况下在 600,000 次迭代时的 VBKFS2-HMAC-SHA256。兼容openssl enc -pbkdf2 。 |
KeyedHashAlgorithms
由键控哈希方法使用。
| 值 | 类型 | 注意 |
|---|---|---|
HMACSHA256 | 密钥化 HMAC | 建议用于 MAC 和完整性验证。 |
HMACSHA384 | 密钥化 HMAC | |
HMACSHA512 | 密钥化 HMAC | |
SHA256 | 未键入密钥的哈希值 | 该键被忽略;等效于明文的 SHA 哈希值。 |
SHA384 | 未键入密钥的哈希值 | 该密钥将被忽略。 |
SHA512 | 未键入密钥的哈希值 | 该密钥将被忽略。 |
HMACSHA1 | 密钥化 HMAC | 已过时。NIST 已弃用 SHA-1;首选 SHA256 或更高版本。 |
HMACMD5 | 密钥化 HMAC | 已过时。MD5 失效;避免用于任何安全敏感用途。 |
SHA1 | 未键入密钥的哈希值 | 已过时。演示冲突攻击;不要使用。 |
RsaKeySize
PgpGenerateKeys使用。
| 值 | 位 |
|---|---|
Rsa2048 | 2048 |
Rsa3072 | 3072 |
Rsa4096 | 4096(默认) |
常见模式
使用 AES-GCM 加密和解密字符串(传统,默认设置)
[Workflow]
public void Execute()
{
var key = PasswordKey.FromPassword("MySecretKey123!", Encoding.UTF8);
var ciphertext = cryptography.EncryptText("Sensitive data", EncryptionAlgorithm.AESGCM, SymmetricEncryptOptions.Classic(key));
Log($"Encrypted: {ciphertext}");
var plaintext = cryptography.DecryptText(ciphertext, EncryptionAlgorithm.AESGCM, SymmetricDecryptOptions.Classic(key));
Log($"Decrypted: {plaintext}");
}
[Workflow]
public void Execute()
{
var key = PasswordKey.FromPassword("MySecretKey123!", Encoding.UTF8);
var ciphertext = cryptography.EncryptText("Sensitive data", EncryptionAlgorithm.AESGCM, SymmetricEncryptOptions.Classic(key));
Log($"Encrypted: {ciphertext}");
var plaintext = cryptography.DecryptText(ciphertext, EncryptionAlgorithm.AESGCM, SymmetricDecryptOptions.Classic(key));
Log($"Decrypted: {plaintext}");
}
使用调用者提供的原始密钥和 PV 进行加密(第三方互操作性)
[Workflow]
public void Execute()
{
// 32 bytes for AES-256
byte[] rawKeyBytes = Convert.FromBase64String("your-base64-encoded-32-byte-key==");
byte[] iv = Convert.FromHexString("a3f1b2c4d5e6f70819a0b1c2d3e4f506");
var key = RawKey.FromBytes(rawKeyBytes);
byte[] cipher = cryptography.EncryptBytes(
Encoding.UTF8.GetBytes("payload"),
EncryptionAlgorithm.AESGCM,
SymmetricEncryptOptions.Raw(key, iv));
// Decrypt. The IV is read from the ciphertext stream prefix, so there is no need to pass it again.
byte[] plain = cryptography.DecryptBytes(
cipher,
EncryptionAlgorithm.AESGCM,
SymmetricDecryptOptions.Raw(key));
}
[Workflow]
public void Execute()
{
// 32 bytes for AES-256
byte[] rawKeyBytes = Convert.FromBase64String("your-base64-encoded-32-byte-key==");
byte[] iv = Convert.FromHexString("a3f1b2c4d5e6f70819a0b1c2d3e4f506");
var key = RawKey.FromBytes(rawKeyBytes);
byte[] cipher = cryptography.EncryptBytes(
Encoding.UTF8.GetBytes("payload"),
EncryptionAlgorithm.AESGCM,
SymmetricEncryptOptions.Raw(key, iv));
// Decrypt. The IV is read from the ciphertext stream prefix, so there is no need to pass it again.
byte[] plain = cryptography.DecryptBytes(
cipher,
EncryptionAlgorithm.AESGCM,
SymmetricDecryptOptions.Raw(key));
}
解密... 生成的文件 openssl enc
[Workflow]
public void Execute()
{
// openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -md sha256 -salt -k password -in plain.txt -out cipher.bin
var key = PasswordKey.FromPassword("password", Encoding.UTF8);
cryptography.DecryptFile(
inputPath: @"C:\Documents\cipher.bin",
outputPath: @"C:\Documents\plain.txt",
algorithm: EncryptionAlgorithm.AES,
options: SymmetricDecryptOptions.OpenSslEnc(key),
overwrite: true);
}
[Workflow]
public void Execute()
{
// openssl enc -aes-256-cbc -pbkdf2 -iter 600000 -md sha256 -salt -k password -in plain.txt -out cipher.bin
var key = PasswordKey.FromPassword("password", Encoding.UTF8);
cryptography.DecryptFile(
inputPath: @"C:\Documents\cipher.bin",
outputPath: @"C:\Documents\plain.txt",
algorithm: EncryptionAlgorithm.AES,
options: SymmetricDecryptOptions.OpenSslEnc(key),
overwrite: true);
}
使用更多的 KFS 迭代计数 ( Owasp2026 )
[Workflow]
public void Execute()
{
var key = PasswordKey.FromPassword("MySecretKey", Encoding.UTF8);
// Owasp2026(key) defaults to kdfIterations = 1_300_000 (the OWASP 2026 recommendation).
var ciphertext = cryptography.EncryptBytes(
Encoding.UTF8.GetBytes("payload"),
EncryptionAlgorithm.AESGCM,
SymmetricEncryptOptions.Owasp2026(key));
// Decrypt must use the same iteration count. Owasp2026 does not store it in the wire format.
byte[] plain = cryptography.DecryptBytes(
ciphertext,
EncryptionAlgorithm.AESGCM,
SymmetricDecryptOptions.Owasp2026(key));
}
[Workflow]
public void Execute()
{
var key = PasswordKey.FromPassword("MySecretKey", Encoding.UTF8);
// Owasp2026(key) defaults to kdfIterations = 1_300_000 (the OWASP 2026 recommendation).
var ciphertext = cryptography.EncryptBytes(
Encoding.UTF8.GetBytes("payload"),
EncryptionAlgorithm.AESGCM,
SymmetricEncryptOptions.Owasp2026(key));
// Decrypt must use the same iteration count. Owasp2026 does not store it in the wire format.
byte[] plain = cryptography.DecryptBytes(
ciphertext,
EncryptionAlgorithm.AESGCM,
SymmetricDecryptOptions.Owasp2026(key));
}
计算 HMAC-SHA256 以进行数据完整性验证
[Workflow]
public void Execute()
{
byte[] hmacKey = Convert.FromBase64String("your-base64-hmac-key==");
var key = RawKey.FromBytes(hmacKey);
// Keyed-hash methods take a CryptoKey directly. There is no options object, because there is no wire-format axis.
var digest = cryptography.KeyedHashText("payload to verify", KeyedHashAlgorithms.HMACSHA256, key);
Log($"HMAC-SHA256: {digest}");
}
[Workflow]
public void Execute()
{
byte[] hmacKey = Convert.FromBase64String("your-base64-hmac-key==");
var key = RawKey.FromBytes(hmacKey);
// Keyed-hash methods take a CryptoKey directly. There is no options object, because there is no wire-format axis.
var digest = cryptography.KeyedHashText("payload to verify", KeyedHashAlgorithms.HMACSHA256, key);
Log($"HMAC-SHA256: {digest}");
}
PGP 加密和签名,然后解密并验证
[Workflow]
public void Execute()
{
var recipientPublic = PgpPublicKey.FromFilePath(@"C:\Keys\recipient_public.asc");
var senderPrivate = PgpPrivateKey.FromFilePath(@"C:\Keys\sender_private.asc", "senderPassphrase");
// Passing a signer to PgpEncrypt* implies sign-and-encrypt.
byte[] encrypted = cryptography.PgpEncryptBytes(
Encoding.UTF8.GetBytes("Signed and encrypted message"),
recipientPublic,
signer: senderPrivate);
var recipientPrivate = PgpPrivateKey.FromFilePath(@"C:\Keys\recipient_private.asc", "recipientPassphrase");
var senderPublic = PgpPublicKey.FromFilePath(@"C:\Keys\sender_public.asc");
// Passing a verifier to PgpDecrypt* implies verify-while-decrypting.
byte[] decrypted = cryptography.PgpDecryptBytes(
encrypted,
recipientPrivate,
verifier: senderPublic);
Log(Encoding.UTF8.GetString(decrypted));
}
[Workflow]
public void Execute()
{
var recipientPublic = PgpPublicKey.FromFilePath(@"C:\Keys\recipient_public.asc");
var senderPrivate = PgpPrivateKey.FromFilePath(@"C:\Keys\sender_private.asc", "senderPassphrase");
// Passing a signer to PgpEncrypt* implies sign-and-encrypt.
byte[] encrypted = cryptography.PgpEncryptBytes(
Encoding.UTF8.GetBytes("Signed and encrypted message"),
recipientPublic,
signer: senderPrivate);
var recipientPrivate = PgpPrivateKey.FromFilePath(@"C:\Keys\recipient_private.asc", "recipientPassphrase");
var senderPublic = PgpPublicKey.FromFilePath(@"C:\Keys\sender_public.asc");
// Passing a verifier to PgpDecrypt* implies verify-while-decrypting.
byte[] decrypted = cryptography.PgpDecryptBytes(
encrypted,
recipientPrivate,
verifier: senderPublic);
Log(Encoding.UTF8.GetString(decrypted));
}
生成新的 PGP 密钥对
[Workflow]
public void Execute()
{
PgpKeyPair pair = cryptography.PgpGenerateKeys(
userId: "Alice <alice@example.com>",
passphrase: "StrongPassphrase!",
keySize: RsaKeySize.Rsa4096);
pair.PublicKey.Save(@"C:\Keys\my_public.asc");
pair.PrivateKey.Save(@"C:\Keys\my_private.asc");
Log("Key pair generated.");
}
[Workflow]
public void Execute()
{
PgpKeyPair pair = cryptography.PgpGenerateKeys(
userId: "Alice <alice@example.com>",
passphrase: "StrongPassphrase!",
keySize: RsaKeySize.Rsa4096);
pair.PublicKey.Save(@"C:\Keys\my_public.asc");
pair.PrivateKey.Save(@"C:\Keys\my_private.asc");
Log("Key pair generated.");
}
在存储入站公钥之前对其进行验证
[Workflow]
public void Execute()
{
// Armored public key arriving as text from an HTTP response or config.
string armoredPublicKey = LoadFromInbox();
var candidate = PgpPublicKey.FromBytes(Encoding.UTF8.GetBytes(armoredPublicKey));
if (!cryptography.PgpVerifyPublicKey(candidate))
{
throw new InvalidOperationException("Supplied content is not a valid OpenPGP public key.");
}
candidate.Save(@"C:\Keys\trusted_public.asc");
}
[Workflow]
public void Execute()
{
// Armored public key arriving as text from an HTTP response or config.
string armoredPublicKey = LoadFromInbox();
var candidate = PgpPublicKey.FromBytes(Encoding.UTF8.GetBytes(armoredPublicKey));
if (!cryptography.PgpVerifyPublicKey(candidate))
{
throw new InvalidOperationException("Supplied content is not a valid OpenPGP public key.");
}
candidate.Save(@"C:\Keys\trusted_public.asc");
}
- 自动导入的命名空间
- 服务概述
- 字节、文本和文件表单
- 密钥材料
PasswordKeyRawKey- 对称选项
- 输入向量和加点值策略
- PGP 密钥材料
- 与活动的关系
- 从之前的编码 API 迁移
- 对称加密
byte[] EncryptBytes(byte[] input, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options)string EncryptText(string input, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options)void EncryptFile(string inputPath, string outputPath, EncryptionAlgorithm algorithm, SymmetricEncryptOptions options, bool overwrite = false)- 对称解密
byte[] DecryptBytes(byte[] input, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options)string DecryptText(string input, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options)void DecryptFile(string inputPath, string outputPath, EncryptionAlgorithm algorithm, SymmetricDecryptOptions options, bool overwrite = false)- 键控散列
string KeyedHashBytes(byte[] input, KeyedHashAlgorithms algorithm, CryptoKey key)string KeyedHashText(string input, KeyedHashAlgorithms algorithm, CryptoKey key, Encoding encoding = null)string KeyedHashFile(string inputPath, KeyedHashAlgorithms algorithm, CryptoKey key)- PGP 加密
byte[] PgpEncryptBytes(byte[] input, PgpPublicKey recipient, PgpPrivateKey signer = null)string PgpEncryptText(string input, PgpPublicKey recipient, PgpPrivateKey signer = null)void PgpEncryptFile(string inputPath, string outputPath, PgpPublicKey recipient, PgpPrivateKey signer = null, bool overwrite = false)- PGP 解密
byte[] PgpDecryptBytes(byte[] input, PgpPrivateKey recipient, PgpPublicKey verifier = null)string PgpDecryptText(string input, PgpPrivateKey recipient, PgpPublicKey verifier = null)void PgpDecryptFile(string inputPath, string outputPath, PgpPrivateKey recipient, PgpPublicKey verifier = null, bool overwrite = false)- PGP 签名(二进制签名)
byte[] PgpSignBytes(byte[] input, PgpPrivateKey signer)string PgpSignText(string input, PgpPrivateKey signer)void PgpSignFile(string inputPath, string outputPath, PgpPrivateKey signer, bool overwrite = false)- PGP 明文签名
byte[] PgpClearSignBytes(byte[] input, PgpPrivateKey signer)string PgpClearSignText(string input, PgpPrivateKey signer)void PgpClearSignFile(string inputPath, string outputPath, PgpPrivateKey signer, bool overwrite = false)- PGP 验证
- 二进制签名
- 明文签名
- 公钥格式正确
- PGP 密钥对生成
PgpKeyPair PgpGenerateKeys(string userId, string passphrase, RsaKeySize keySize = RsaKeySize.Rsa4096)PgpKeyPair PgpGenerateKeys(string userId, SecureString passphrase, RsaKeySize keySize = RsaKeySize.Rsa4096)- 枚举引用
EncryptionAlgorithmSymmetricWireFormatKeyedHashAlgorithmsRsaKeySize- 常见模式
- 使用 AES-GCM 加密和解密字符串(传统,默认设置)
- 使用调用者提供的原始密钥和 PV 进行加密(第三方互操作性)
- 解密... 生成的文件
openssl enc - 使用更多的 KFS 迭代计数 (
Owasp2026) - 计算 HMAC-SHA256 以进行数据完整性验证
- PGP 加密和签名,然后解密并验证
- 生成新的 PGP 密钥对
- 在存储入站公钥之前对其进行验证