在C#中实现RSA [英] Implementing RSA in C#

查看:101
本文介绍了在C#中实现RSA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图实现一个类来使用RSACrytoServiceProveider类我的应用程序实例之间处理安全通信。
第一个问题:这是一个好主意,实行单一的类来处理发送/ reciever角色,或者我应该角色拆分成单独的类?这是我迄今所做的:

I'm currently trying to implement a class to handle secure communications between instances of my app using RSACrytoServiceProveider class. First question : is it a good idea implement a single class to handle sender/reciever roles or should i split the roles into individual classes ?. This is what i have done so far:

using System;
using System.Text;
using System.Security.Cryptography;

namespace Agnus.Cipher
{
    public class RSA
    {
        private byte[] plaintextBytes;
        private byte[] ciphertextBytes;
        private RSACryptoServiceProvider rSAProviderThis;
        private RSACryptoServiceProvider rSAProviderOther;

        public string PublicKey
        {
            get { return rSAProviderThis.ToXmlString(false); }
        }

        public RSA()
        {
            rSAProviderThis = new RSACryptoServiceProvider { PersistKeyInCsp = true }; 
            plaintextBytes = Encoding.Unicode.GetBytes(PublicKey);
        }

        public void InitializeRSAProviderOther(string parameters)
        {
            rSAProviderOther.FromXmlString(parameters);
        }

        public byte[] Encrypt()
        {
            return rSAProviderThis.Encrypt(plaintextBytes, true);
        }
        public byte[] Decrypt()
        {
            return rSAProviderThis.Decrypt(ciphertextBytes, true);
        }
        public byte[] Sign()
        {
            using (SHA1Managed SHA1 = new SHA1Managed())
            {
                byte[] hash = SHA1.ComputeHash(ciphertextBytes);
                byte[] signature = rSAProviderThis.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
                return signature;
            }
        }
        public void Verify()
        {
            throw new NotImplementedException();
        }

    }
}

第二个问题:我如何发送和接收数据送入类?我在这个领域的绿色喇叭,指针将AP preciated。

Second question : how do i send and receive data to be fed into the class ? i'm a green horn in this field, pointers would be appreciated.

推荐答案

我会做加密/签名/解密/验证方法采取所需的数据参数,而不是为他们的成员变量。其成员变量的关键和供应商似乎没事,但。基本上,我期望使用相同的键多次,但不相同的数据。

I would make the encrypt/sign/decrypt/verify methods take parameters for the data rather than having member variables for them. Having member variables for the key and provider seems okay though. Basically I'd expect to use the same key multiple times but not the same data.

我也想使它不可变的 - 使只读所有的变量,以你需要的,而不是有一个单独的初始化方法在构造函数中提供的所有参数

I'd also make it immutable - make all the variables readonly, taking all the parameters you'll need for the providers in the constructor instead of having a separate initialisation method.

除此之外,似乎好为您的需要一个更简单的API中,虽然包的功能,是的。

Beyond that, it seems okay to wrap the functionality in a simpler API for your needs though, yes.

这篇关于在C#中实现RSA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆