📄 crypttest.cs
字号:
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EncryptDecrypt
{
public class CryptTest
{
public static string Encrypt(string data, string password)
{
if (String.IsNullOrEmpty(data))
throw new ArgumentException("No data given");
if (String.IsNullOrEmpty(password))
throw new ArgumentException("No password given");
// setup the encryption algorithm
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, 8);
Rijndael aes = Rijndael.Create();
aes.IV = keyGenerator.GetBytes(aes.BlockSize / 8);
aes.Key = keyGenerator.GetBytes(aes.KeySize / 8);
// encrypt the data
byte[] rawData = Encoding.Unicode.GetBytes(data);
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
memoryStream.Write(keyGenerator.Salt, 0, keyGenerator.Salt.Length);
cryptoStream.Write(rawData, 0, rawData.Length);
cryptoStream.Close();
byte[] encrypted = memoryStream.ToArray();
return Convert.ToBase64String(encrypted);
}
}
public static string Decrypt(string data, string password)
{
if (String.IsNullOrEmpty(data))
throw new ArgumentException("No data given");
if (String.IsNullOrEmpty(password))
throw new ArgumentException("No password given");
byte[] rawData = Convert.FromBase64String(data);
if (rawData.Length < 8)
throw new ArgumentException("Invalid input data");
// setup the decryption algorithm
byte[] salt = new byte[8];
for (int i = 0; i < salt.Length; i++)
salt[i] = rawData[i];
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt);
Rijndael aes = Rijndael.Create();
aes.IV = keyGenerator.GetBytes(aes.BlockSize / 8);
aes.Key = keyGenerator.GetBytes(aes.KeySize / 8);
// decrypt the data
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(rawData, 8, rawData.Length - 8);
byte[] decrypted = memoryStream.ToArray();
cryptoStream.Close();
return Encoding.Unicode.GetString(decrypted);
}
}
public static string Encrypt2(string data, string password)
{
if (String.IsNullOrEmpty(data))
throw new ArgumentException("No data given");
if (String.IsNullOrEmpty(password))
throw new ArgumentException("No password given");
SymmetricAlgorithm symm = new RijndaelManaged();
// encrypt the data
byte[] rawData = Encoding.Unicode.GetBytes(data);
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symm.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(rawData, 0, rawData.Length);
cryptoStream.Close();
byte[] encrypted = memoryStream.ToArray();
return Convert.ToBase64String(encrypted);
}
}
public static string Decrypt2(string data, string password)
{
if (String.IsNullOrEmpty(data))
throw new ArgumentException("No data given");
if (String.IsNullOrEmpty(password))
throw new ArgumentException("No password given");
byte[] rawData = Convert.FromBase64String(data);
if (rawData.Length < 8)
throw new ArgumentException("Invalid input data");
SymmetricAlgorithm symm = new RijndaelManaged();
// decrypt the data
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symm.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(rawData, 0, rawData.Length);
byte[] decrypted = memoryStream.ToArray();
cryptoStream.Close();
return Encoding.Unicode.GetString(decrypted);
}
}
public static void Execute()
{
MemoryStream inputStream = new MemoryStream();
MemoryStream outputStream = new MemoryStream();
byte[] buffer = new byte[] { 1, 2, 3 };
inputStream.Write(buffer, 0, buffer.Length);
inputStream.Position = 0;
outputStream.Position = 0;
CryptEncryptor encryptor = new CryptEncryptor(inputStream, outputStream);
encryptor.Execute();
inputStream = new MemoryStream();
outputStream.Position = 0;
CryptDecryptor decryptor = new CryptDecryptor(outputStream, inputStream);
decryptor.Execute();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -