J.E*_*Evs 0 c# encryption .net-core
我在以下代码中收到 VS Code(在 Ubuntu 上运行)中的错误
class Program
{
public static void Main()
{
string originalText = "Test";
try
{
using (Aes myAes = new Aes.Create())
{
string encrypted = StringCipher.Encrypt(originalText, myAes);
string decrypted = StringCipher.Decrypt(encrypted, myAes);
Console.WriteLine(String.Format("Original {0}", originalText));
Console.WriteLine(String.Format("Encrypted {0}", encrypted));
Console.WriteLine(String.Format("Decrypted {0}", decrypted));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
VS Code 智能感知指出类型名称“创建”在 Aes 类型中不存在。
不确定这是否是我的错误编码,但我基本上是按照此处的指南进行操作。
唯一的区别是我已经实现了加密/解密是在一个单独的代码文件中实现的:
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;
namespace EncryptionConsole
{
public static class StringCipher
{
public static string Encrypt(string plainText, Aes aes)
{
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(plainText, aes.Key, aes.IV);
string result = Encoding.UTF8.GetString(encrypted);
return result;
}
public static string Decrypt(string cipherText, Aes aes)
{
byte[] cipherTextArray = Encoding.UTF8.GetBytes(cipherText);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(cipherTextArray, aes.Key, aes.IV);
return roundtrip;
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我很欣赏上面的代码中可能有一些明显的错误,这不是真正的问题,因为我现在只是在探索。
任何帮助是极大的赞赏。
Aes.Create()是一个静态方法。您不需要使用关键字实例化它new。如果您查看文档(https://msdn.microsoft.com/en-us/library/bb351532(v=vs.110).aspx),您将看到此方法实际上为您返回了一个新创建的 AES 对象. 这在编程术语中称为“静态工厂方法”,是初始化对象的常见做法/模式。