x509证书签名

dennis99948 2010-08-11 11:11:04
使用pfx证书文件,签名怎么做
...全文
290 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dennis99948 2010-08-12
  • 打赏
  • 举报
回复
我是要用银联提供的证书,http post 提交报文,对银行卡扣款。
就需要对报文进行消息签名呀。
策略当然是提取证书pfx的私钥,对报文进行签名。
应怎么处理呢
dennis99948 2010-08-11
  • 打赏
  • 举报
回复
但以上方法得到的结果值与其他站点接口的验证结果不一致
dennis99948 2010-08-11
  • 打赏
  • 举报
回复
我的代码是这样
X509Certificate2 myX509Certificate2 = new X509Certificate2(@"D:\pdscert\TEST1.pfx", "123456", X509KeyStorageFlags.Exportable);
str_DataToSign="111
byte[] DataToSign = Encoding.GetEncoding("GB2312").GetBytes(str_DataToSign);
RSACryptoServiceProvider provider = (RSACryptoServiceProvider)myX509Certificate2.PrivateKey;
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(provider);
////Set the hash algorithm to SHA1.
//RSAFormatter.SetHashAlgorithm("SHA1");
//SHA1Managed sha1 = new SHA1Managed();
//byte[] hash = sha1.ComputeHash(DataToSign);
////Create a signature for HashValue and return it.
//byte[] SignedHash = RSAFormatter.CreateSignature(hash);
// Convert.ToBase64String(SignedHash);
return Convert.ToBase64String(provider.SignData(DataToSign, new SHA1CryptoServiceProvider()));
malun666 2010-08-11
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace testCer
{
class Program
{
static void Main(string[] args)
{

//生成pfx证书文件
string MakeCert = "E:\\个人\\usbkey\\iKey1000\\makecert.exe";
string x509Name = "CN=111";
string param = " -pe -ss my -n \"" + x509Name + "\" ";
Process p = Process.Start(MakeCert, param);
p.WaitForExit();
p.Close();

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
foreach (X509Certificate2 x509 in storecollection)
{
if (x509.Subject == "CN=111")
{
Debug.Print(string.Format("certificate name: {0}", x509.Subject));
byte[] pfxByte = x509.Export(X509ContentType.Pfx, "111");
FileStream fileStream = new FileStream(Path.Combine("E:\\个人\\usbkey\\iKey1000", "111.pfx"), FileMode.Create);

// Write the data to the file, byte by byte.
for (int i = 0; i < pfxByte.Length; i++)
fileStream.WriteByte(pfxByte[i]);
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for (int i = 0; i < fileStream.Length; i++)
{
if (pfxByte[i] != fileStream.ReadByte())
{
return;
}
}
fileStream.Close();
}
}
store.Close();
store = null;
storecollection = null;
}
}
}
如题。

1.需要包含公私钥
2.证书用来加密、解密、签名、验证签名
3.pfx格式(或P12)

这几个是显然满足的
不满足的话能叫pfx的证书么?
但是这个公私钥
是随机产生的
你使用
X509Certificate2 pc = new X509Certificate2(labelPfxPath.Text.Trim(), textBoxPfxKey.Text.Trim(), X509KeyStorageFlags.Exportable);
使用一个证书文件名、一个用于访问该证书的密码和一个密钥存储标志初始化 X509Certificate2 类的新实例。
你把 证书文件名改成证书的目录加文件名就行了

pc.PublicKey.Key.ToXmlString(false);
pc.PrivateKey.ToXmlString(true);

参考这个:
http://www.luminji.com/post/2010/03/21/Ce5889be5bbbae695b0e5ad97e8af81e4b9a6e5b9b6e5afbce587bae4b8bapfxefbc8ce5b9b6e4bdbfe794a8pfxe8bf9be8a18ce
husanbo110 2010-08-11
  • 打赏
  • 举报
回复
看你需要什么样的签名了
vs 中有 makecert.exe
signcode.exe 等几个文件
你双击打开signcode.exe
按提示就可以对文件签名了
不过你自己生成的pfx 签名没什么用
在网络上还是不安全的
一、在基于Laravel8.x实现API接口签名认证系统课程里: 我将带领大家基于laravel 8.x来开发用户认证系统与接口签名验证系统以解决API接口请求中的安全问题,我将带领同学们认识Laravel用户认证的两大核心要素,守卫者与数据提供者,并从源码层面分析用户认证中涉及到的核心概念,通过基于接口签名的认证逻辑,带领同学们实现自定义守卫者以及签名认证器,实现基于签名认证的用户登陆逻辑,并基于该守卫者实现一个接口签名认证中间件对接口请求进行拦截处理。 在实战过程中,涉及到的核心概念我们会在源码层面对其原理进行阐述,以帮助同学们更好地掌握这些知识。 在完成上述功能后,我们会带领大家将我们实现的基于接口签名认证的用户认证与接口认证逻辑封装成Laravel扩展包,从而使得我们的代码与Laravel核心框架解耦,以保证功能上的独立性和可复用性。 二、在基于Laravel 7.x的后台权限验证API课程里: 以后台权限验证API的开发为载体,带领大家使用Laravel 7.x进行权限扩展包的开发,你将学习到如下知识: 1、如何使用laravel编写Restful api接口 2、如何使用composer进行项目依赖管理,laravel常用扩展的安装与使用,如dingo/api 以及repository 3、如何使用jwt进行实现后台用户认证机制 4、学习使用laravel扩展包的形式进行后台权限验证API的开发 5、如何编写Seeders帮助我们在新系统里实现数据的初始化 6、理解和使用Laravel核心概念和面向接口的编程思想 三、这两套课程分别解决的是API接口请求的安全问题与接口权限问题

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧