我是c#的新手所以它可能是一个愚蠢的问题,对不起:D
当我尝试使用方法Encrypt("c:\\test\testfile.txt", "testpwd")
它说:
参数“1”:不是从“string”到“System.IO.FileInfo”的转换 可能的。
我不知道为什么它只能将其改为System.IO.FileInfo
。当我使用随机密码(我有一个方法)时,它会说将System.Random
转换为字符串是不可能的。
谁能告诉我为什么?先感谢您。
(抱歉我的英文)
using System;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace Project_Hermes
{
static class Program
{
/* private static Random password = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[password.Next(s.Length)]).ToArray());
}*/
[STAThread]
static void Main()
{
string curFile = "c:\\oot26\\encrypd.txt";
if (File.Exists(curFile))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
} else
{
Encrypt(curFile, "password");
//Console.WriteLine(password);
Directory.CreateDirectory("C:\\oot26");
File.Create(curFile);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
private const int SaltSize = 8;
public static void Encrypt(FileInfo targetFile, string password)
{
var keyGenerator = new Rfc2898DeriveBytes(password, SaltSize);
var rijndael = Rijndael.Create();
rijndael.IV = keyGenerator.GetBytes(rijndael.BlockSize / 8);
rijndael.Key = keyGenerator.GetBytes(rijndael.KeySize / 8);
using (var fileStream = targetFile.Create())
{
fileStream.Write(keyGenerator.Salt, 0, SaltSize);
using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
{
}
}
}
/*public static void Decrypt(FileInfo sourceFile, string password)
{
var fileStream = sourceFile.OpenRead();
var salt = new byte[SaltSize];
fileStream.Read(salt, 0, SaltSize);
var keyGenerator = new Rfc2898DeriveBytes(password, salt);
var rijndael = Rijndael.Create();
rijndael.IV = keyGenerator.GetBytes(rijndael.BlockSize / 8);
rijndael.Key = keyGenerator.GetBytes(rijndael.KeySize / 8);
using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
{
}*/
}
}
}
答案 0 :(得分:2)
当<button id="foo">Click me</button>
方法需要Encrypt
对象时,您将string
方法传递给FileInfo
。试试这个:
Encrypt(new FileInfo(curFile), "password");