本文介绍了我得到这个错误“输入字符串不包含'RSA''Modulus'参数的有效编码”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.IO;
使用 System.Xml;
使用 System.Xml.Linq;
使用 System.Security.Cryptography;

namespace CryptographicClasses
{
class BigEncryption
{
public static void PubToXml()
{
var txt = string .Empty;
使用 var stream = File.OpenText( public_key.txt))
{
txt = stream.ReadToEnd();
}

var xml = new XDocument( new XElement( Data new XElement( info,txt)));
xml.Save(Path.ChangeExtension( public_key.txt 。xml));
}

public static void PriToXml()
{
var txt = string .Empty;
使用 var stream = File.OpenText( private_key.txt))
{
txt = stream.ReadToEnd();
}

var xml = new XDocument( new XElement( Data new XElement( info,txt)));
xml.Save(Path.ChangeExtension( private_key.txt 。xml));
}

public static 字符串加密()
{
尝试
{
PubToXml();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
StreamReader readme = File.OpenText( public_key.xml);
string rsaXml = readme.ReadToEnd();
readme.Close();
rsa.FromXmlString(rsaXml);
Console.WriteLine( 在此输入文字);
string message = Console.ReadLine();

byte [] encrypted = rsa.Encrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(message),);

FileStream steam = new FileStream( Lmessage.dat,FileMode.Create);
steam.Write(加密, 0 ,encrypted.Length);
steam.Close();

}

catch (例外情况)
{
Console.WriteLine(ex 。信息);
}

return 0;
}

public static 字符串 Decrypt()
{
PriToXml();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
StreamReader readme = File.OpenText( private_key.xml);
string rsaXml = readme.ReadToEnd();
readme.Close();
rsa.FromXmlString(rsaXml);
string message = Console.ReadLine();

byte [] decrypted = rsa.Decrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(message),);

FileStream steam = new FileStream( Lmessage.dat,FileMode.Create);
steam.Write(解密, 0 ,decrypted.Length);
steam.Close();
return 0 ;
}
}
}
解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Security.Cryptography;

namespace CryptographicClasses
{
    class BigEncryption
    {
        public static void PubToXml()
        {
            var txt = string.Empty;
            using (var stream = File.OpenText("public_key.txt"))
            {
                txt = stream.ReadToEnd();
            }

            var xml = new XDocument(new XElement("Data", new XElement("info", txt)));
            xml.Save(Path.ChangeExtension("public_key.txt", ".xml"));
        }

        public static void PriToXml()
        {
            var txt = string.Empty;
            using (var stream = File.OpenText("private_key.txt"))
            {
                txt = stream.ReadToEnd();
            }

            var xml = new XDocument(new XElement("Data", new XElement("info", txt)));
            xml.Save(Path.ChangeExtension("private_key.txt", ".xml"));
        }

        public static string Encrypt()
        {
            try
            {
                PubToXml();
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                StreamReader readme = File.OpenText("public_key.xml");
                string rsaXml = readme.ReadToEnd();
                readme.Close();
                rsa.FromXmlString(rsaXml);
                Console.WriteLine("Enter text here");
                string message = Console.ReadLine();

                byte[] encrypted = rsa.Encrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(message), false);

                FileStream steam = new FileStream("Lmessage.dat", FileMode.Create);
                steam.Write(encrypted, 0, encrypted.Length);
                steam.Close();
                
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            return "0";
        }

        public static string Decrypt()
        {
            PriToXml();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            StreamReader readme = File.OpenText("private_key.xml");
            string rsaXml = readme.ReadToEnd();
            readme.Close();
            rsa.FromXmlString(rsaXml);
            string message = Console.ReadLine();

            byte[] decrypted = rsa.Decrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(message), false);

            FileStream steam = new FileStream("Lmessage.dat", FileMode.Create);
            steam.Write(decrypted, 0, decrypted.Length);
            steam.Close();
            return "0";
        }
    }
}
解决方案


这篇关于我得到这个错误“输入字符串不包含'RSA''Modulus'参数的有效编码”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 18:41