小编sco*_*ott的帖子

如何使用RSA和SHA256与.NET签署文件?

我的应用程序将采用一组文件并对其进行签名.(我不是要尝试签署程序集.)有一个.p12文件,我从中获取私钥.

这是我试图使用的代码,但我得到了一个System.Security.Cryptography.CryptographicException "Invalid algorithm specified.".

X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
Run Code Online (Sandbox Code Playgroud)

根据这个答案,它无法完成(RSACryptoServiceProvider不支持SHA-256),但我希望可以使用不同的库,如Bouncy Castle.

我是新手,我发现Bouncy Castle非常混乱.我正在将一个Java应用程序移植到C#,我必须使用相同类型的加密来签名文件,所以我坚持使用RSA + SHA256.

我怎么能用Bouncy Castle,OpenSSL.NET,Security.Cryptography或其他我没有听说过的第三方库来做到这一点?我假设,如果它可以在Java中完成,那么它可以在C#中完成.

更新:

这是我从poupou的anwser链接中得到的

        X509Certificate2 cert = new X509Certificate2(KeyStoreFile, password");
        RSACryptoServiceProvider rsacsp = (RSACryptoServiceProvider)cert.PrivateKey;
        CspParameters cspParam = new CspParameters();
        cspParam.KeyContainerName = rsacsp.CspKeyContainerInfo.KeyContainerName;
        cspParam.KeyNumber = rsacsp.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2;
        RSACryptoServiceProvider aescsp = new RSACryptoServiceProvider(cspParam);
        aescsp.PersistKeyInCsp = false;
        byte[] signed = aescsp.SignData(File.ReadAllBytes(file), "SHA256");
        bool isValid …
Run Code Online (Sandbox Code Playgroud)

.net c# openssl cryptography bouncycastle

43
推荐指数
6
解决办法
8万
查看次数

为什么我收到"指定的无效算法"异常

这是我的代码.

X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
Run Code Online (Sandbox Code Playgroud)

在最后一行,我得到了例外:

System.Security.Cryptography.CryptographicException"指定的算法无效."

我究竟做错了什么?

更新:

id = 2.16.840.1.101.3.4.2.1

c# cryptography

17
推荐指数
4
解决办法
2万
查看次数

由于其保护级别,类无法访问

我有三节课.all都是同一命名空间的一部分.这是三个类的基础知识.

//FBlock.cs
namespace StubGenerator.PropGenerator
{
    class FBlock : IDesignRegionInserts, IFormRegionInserts, IAPIRegionInserts,  IConfigurationInserts, ISoapProxyClientInserts, ISoapProxyServiceInserts
    {
        private List<Property> pProperties;
        private List<Method> pMethods;
        public FBlock(string aFBlockName)
        { 
            pProperties = new List<Property>();
            pMethods = new List<Method>();
        }

        public Property AddProperty(string aName)
        {
            Property loProp = new Property(this, aName, pProperties.Count);
            pProperties.Add(loProp);
            return loProp;
         }

         public Method AddMethod(string aName)
         {
             Method loMeth = new Method(this, aName);
             pMethods.Add(loMeth);
             return loMeth;
         }
     }

 //Method.cs
 namespace StubGenerator.PropGenerator
 {
     class Method : IPropertyName
     {
         private List<StubGenerator.PropGenerator.PropertyAttribute> pPropertyAttributes;
         private string …
Run Code Online (Sandbox Code Playgroud)

c# access-modifiers

14
推荐指数
2
解决办法
17万
查看次数

如何在控制台应用程序中获取光标的位置?

我希望能够检查光标的位置是否没有超过某一点.我无法通过谷歌找到任何能给出文本光标位置的东西.我不确定在哪里可以看.

c# console

12
推荐指数
2
解决办法
2万
查看次数

内部带有flowlayout面板且autosize = true的Groupbox会缩小,就像它是空的一样

我有一个包含flowlayout面板的groupbox,flowlayout面板包含一堆控件.我将flowlayout面板设置为与父级对接.由于我不知道面板中有多少控件,我将组框autosize设置为true,autosizemode设置为增长和缩小.当我这样做时,groupbox会缩小,就像它是空的一样.我需要标题,所以我无法删除组框.任何人都知道为什么会这样吗?

c# groupbox autosize flowlayoutpanel

11
推荐指数
2
解决办法
2万
查看次数

如何使用命名管道从c ++调用WCF方法?

更新:在这里查看协议,我无法弄清楚Unsized Envelope Record中的内容.我在网上找不到任何例子.

原版的:

我有以下WCF服务

    static void Main(string[] args)
    {
        var inst = new PlusFiver();
        using (ServiceHost host = new ServiceHost(inst,
            new Uri[] { new Uri("net.pipe://localhost") }))
        {
            host.AddServiceEndpoint(typeof(IPlusFive), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), "PipePlusFive");
            host.Open();
            Console.WriteLine("Service is Available. Press enter to exit.");
            Console.ReadLine();
            host.Close();
        }
    }

   [ServiceContract]
   public interface IPlusFive
   {
       [OperationContract]
       int PlusFive(int value);
   }


   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
   public class PlusFiver : IPlusFive
   {
      public int PlusFive(int value)
      {
          Console.WriteLine("Adding 5 to " + value);
          return value + 5; …
Run Code Online (Sandbox Code Playgroud)

c++ wcf winapi named-pipes

10
推荐指数
1
解决办法
1833
查看次数

我如何解析dtd文件

我想解析一个dtd文件并使用我从中获取的信息来创建一些类.我知道我可以将它转换为xsd然后解析它,但我希望避免这种情况.我通过谷歌找到的一切都是为了验证dtd.所以我想我的问题是:如何使用c#解析dtd文件,或者我可以使用哪些工具或库?我应该补充一点,我正在使用visual studio 2005.

c# code-generation dtd-parsing

6
推荐指数
1
解决办法
1896
查看次数

是否可以将其重构为单一方法

我有一堆看起来像这两个的方法:

   public void SourceInfo_Get()    
   {
        MethodInfo mi = pFBlock.SourceInfo.GetType().GetMethod("SendGet");
        if (mi != null)
        {
            ParameterInfo[] piArr = mi.GetParameters();
            if (piArr.Length == 0)
            {
                mi.Invoke(pFBlock.SourceInfo, new object[0]);
            }
        }
    }
    public void SourceAvailable_Get()
    {
        MethodInfo mi = pFBlock.SourceAvailable.GetType().GetMethod("SendGet");
        if (mi != null)
        {
            ParameterInfo[] piArr = mi.GetParameters();
            if (piArr.Length == 0)
            {
                mi.Invoke(pFBlock.SourceAvailable, new object[0]);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的pFBlock对象中的每个属性都有一个方法.在方法之间变化如此之少,我觉得应该有更好的方法来做到这一点,但我想不出任何方法.我正在使用VS 2005.

c#

6
推荐指数
1
解决办法
176
查看次数

当从c#访问内存映射文件而不是从c ++访问内存映射文件时,为什么会出现访问被拒绝的异常

我有一个使用命名管道的WCF服务.显然不使用url作为名称.它从URL生成GUID,然后将其存储在内存映射文件中.所以我编写了一个C++应用程序,它从内存映射文件中获取管道的名称,这很有用.现在我正在尝试制作ac#app,它从内存映射文件中获取管道的名称.这是我正在使用的代码.

    private static string GetPipeName(string mappedFileName)
    {
        var mappedFile = MemoryMappedFile.OpenExisting(mappedFileName, MemoryMappedFileRights.Read);
        var bytes = new List<byte>();
        using (MemoryMappedViewStream s = mappedFile.CreateViewStream())
        {
            using (var b = new BinaryReader(s))
            {
                bytes = b.ReadBytes((int)b.BaseStream.Length).ToList();
            }

        }
        var sb = new StringBuilder();
        foreach (var b in bytes)
        {
            sb.Append(b.ToString("x2"));
            sb.Append(" ");
        }
        Console.WriteLine(sb.ToString());
        return sb.ToString();
    }
Run Code Online (Sandbox Code Playgroud)

在第一个using语句中,我得到一个UnauthorizedAccessException,表示"拒绝访问路径".

我能够逐步确认文件名与我在c ++应用程序中使用的文件名相同,并显示我何时使用SysInternals工具Handle.exe

为什么C#应用会被拒绝访问?据我所知,两者都是以同一个用户身份运行.

c# c++ wcf memory-mapped-files

6
推荐指数
1
解决办法
2746
查看次数

对定义为无符号字节位范围的数字执行带符号算术运算

我有两个字节.我需要将它们变成两个整数,其中前12位构成一个int而后四个构成另一个.我想我可以&&带有0x0f的第二个字节来获得4位,但我不知道如何将它变成具有正确符号的字节.

更新:只是为了澄清我有2个字节

byte1 = 0xab
byte2 = 0xcd
Run Code Online (Sandbox Code Playgroud)

我需要用它来做这样的事情

var value = 0xabc * 10 ^ 0xd;
Run Code Online (Sandbox Code Playgroud)

对困惑感到抱歉.

感谢所有的帮助.

c#

5
推荐指数
1
解决办法
190
查看次数