试图在.NET中使用RSA实现加密方法?

时间:2015-10-05 17:44:46

标签: c# wpf encryption rsa

但是我收到了这个错误:

非静态字段,方法或属性rsa需要对象引用

    public string FingerPrint
    {
        get
        {
            byte[] bytes = Utility.GetBytes(Imagin.FingerPrint.CPUId);
            byte[] encrypted_bytes = this.Encrypt(bytes, RSA.ExportParameters(false), false);
            return Utility.GetString(encrypted_bytes);
        }
    }

    public byte[] Encrypt(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.ImportParameters(RSAKey); encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
            }
            return encryptedData;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
    }

    public byte[] Decrypt(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
    {
        try
        {
            byte[] decryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.ImportParameters(RSAKey);
                decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
            }
            return decryptedData;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.ToString());
            return null;
        }
    }

我在另一篇文章中读到它是因为我需要使用一个新的对象实例:RSA.ExportParameters(false)。什么的实例?我尝试在我的MainWindow构造函数中放置获取FingerPrint的代码,但也没有运气。如果我将FingerPrint设为静态,则只会产生更多问题。

我做得不好?

0 个答案:

没有答案