【问题标题】:c# - PKCS - Exporting to readable formatc# - PKCS - 导出为可读格式
【发布时间】:2016-08-30 06:11:44
【问题描述】:

我必须使用 PKCS 对消息进行签名和加密:

EnvelopedCms envelopedCms = ...
// Add recipients
envelopedCms.Encrypt(recip1);
//
msg = envelopedCms.Encode();

我需要以下格式的结果:

-----BEGIN PKCS7-----
base64 encoded msg
-----END PKCS7-----

你知道如何在c#/.net中以这种格式导出吗?

谢谢

【问题讨论】:

  • 你不能只是 Base64 编码 msg 并将其包装在 BEGIN/END 文本中吗?

标签: c# pkcs#7


【解决方案1】:

这是你需要的吗? (来自this post)

SignedCms signedCms = new SignedCms();
resultString = resultString.Replace("\n", "").Replace("-----BEGIN PKCS7-----", "").Replace("-----END PKCS7-----", "");
signedCms.Decode(Convert.FromBase64String(resultString));

要在 C# .NET 中将字符串编码为 base64 字符串,您可以执行以下操作: (我添加了这个,因为你的问题不清楚,但说明了“base64 编码的味精”)

// Get the bytes.
var bytes = System.Text.Encoding.UTF8.GetBytes(yourString);

return System.Convert.ToBase64String(bytes);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2016-01-15
    • 2016-11-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多