我得到了:
运行此脚本时出现公共类
中需要公共Main()方法
错误:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class Program {
public static void Main(string[] args) {
//Shared 256 bit Key and IV here
const string sKy = "lkirwf897+22#bbtrm8814z5qq=498j5"; //32 chr shared ascii string (32 * 8 = 256 bit)
const string sIV = "741952hheeyy66#cs!9hjv887mxx7@8y"; //32 chr shared ascii string (32 * 8 = 256 bit)
var sTextVal = "Here is my data to encrypt!!!";
var eText = EncryptRJ256(sKy, sIV, sTextVal);
var dText = DecryptRJ256(sKy, sIV, eText);
Console.WriteLine("key: " + sKy);
Console.WriteLine();
Console.WriteLine(" iv: " + sIV);
Console.WriteLine("txt: " + sTextVal);
Console.WriteLine("encrypted: " + eText);
Console.WriteLine("decrypted: " + dText);
Console.WriteLine("press any key to exit");
Console.ReadKey(true);
}
public static string DecryptRJ256(string prm_key, string prm_iv, string prm_text_to_decrypt) {
var sEncryptedString = prm_text_to_decrypt;
var myRijndael = new RijndaelManaged() {
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 256,
BlockSize = 256
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var decryptor = myRijndael.CreateDecryptor(key, IV);
var sEncrypted = Convert.FromBase64String(sEncryptedString);
var fromEncrypt = new byte[sEncrypted.Length];
var msDecrypt = new MemoryStream(sEncrypted);
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return (Encoding.ASCII.GetString(fromEncrypt));
}
public static string EncryptRJ256(string prm_key, string prm_iv, string prm_text_to_encrypt) {
var sToEncrypt = prm_text_to_encrypt;
var myRijndael = new RijndaelManaged() {
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 256,
BlockSize = 256
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var encryptor = myRijndael.CreateEncryptor(key, IV);
var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
var toEncrypt = Encoding.ASCII.GetBytes(sToEncrypt);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
var encrypted = msEncrypt.ToArray();
return (Convert.ToBase64String(encrypted));
}
}
我仍然无法理解错误的位置。
答案 0 :(得分:0)
.NET Fiddle解决方案 最初发布四年后。.netFiddle上的控制台编译要求包含main方法的类和静态main方法的类都必须标记为public。
由于小提琴应用程序的执行是从“运行”按钮开始的,而不是从在命令行中键入的命令开始的,所以我猜只是忽略了“ args”。但是,命令行输入仍然可以使用Console.ReadLine();
我在一个小提琴上发现了一个简单的例子:
public class Program
{
public static void Main(string[] args)
{
int number;
string inputData = Console.ReadLine();
if(int.TryParse(inputData, out number))
{
if (number > 0)
{
Console.WriteLine("You typed " + number + ". This is a postive value");
Console.ReadLine();
}
else if (number < 0)
{
Console.WriteLine("You typed" + number+ ". This is a negative value");
Console.ReadLine();
}
else
{
Console.WriteLine("You typed" + number + " (zero).");
Console.ReadLine();
}
}
else
{
Console.WriteLine("The input was not a valid integer.");
}
}
}
许多“控制台”(像原始海报一样)期望.net小提琴像控制台应用程序一样工作,希望这会有所帮助!
答案 1 :(得分:-4)
尝试在main
方法string[] args
中删除它对我有用。