0
A
回答
1
是的。您可以使用如下的代碼在http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
您可以使用RIJNDAEL
:
首先,你需要包括using System.Security.Cryptography;
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize/8);
AES.IV = key.GetBytes(AES.BlockSize/8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize/8);
AES.IV = key.GetBytes(AES.BlockSize/8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
public void EncryptFile(string filename, string outfilename, string psw)
{
string file = filename;
string password = psw;
byte[] bytesToBeEncrypted = File.ReadAllBytes(file); //read bytes to encrypt them
byte[] passwordBytes = Encoding.UTF8.GetBytes(password); //read with UTF8 encoding the password.
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); //hash the psw
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
string fileEncrypted = outfilename;
File.WriteAllBytes(fileEncrypted, bytesEncrypted);
}
public void DecryptFile(string filename, string outfilename, string psw)
{
string fileEncrypted = filename;
string password = psw;
byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
string file = outfilename;
File.WriteAllBytes(file, bytesDecrypted);
}
相關問題
- 1. C++ AES加密類
- 2. 使用AES加密數據
- 3. Byte []密鑰加密C#Aes
- 4. AES C#加密解密FIPS
- 5. AES加密C#解密Java?
- 6. AES在C加密解密
- 7. AES加密和解密數據
- 8. AES加密和解密數據丟失
- 9. objective c aes 128加密.net aes
- 10. AES加密/解密 - 動態C到C#
- 11. 密碼加密/數據庫層AES或應用層AES
- 12. 如何在AES數據庫中存儲AES加密密碼
- 13. 解密使用Objective-C與Java進行AES加密的數據
- 14. AES 256加密文件C#
- 15. JS-C#雙AES加密
- 16. Object-C AES 128未加密
- 17. C#PHP AES 256 CBC加密
- 18. Objective-C AES 128位加密
- 19. C中的AES加密和解密
- 20. AES密碼加密
- 21. AES加密/解密
- 22. 加密AES密鑰?
- 23. AES密鑰,加密
- 24. AES解密C#
- 25. c#AES解密
- 26. c#AES解密
- 27. 使用AES加密「數據流」
- 28. varbinary或varchar保存AES加密數據
- 29. 如何使用AES(openssl)加密數據?
- 30. AES與RSA加密大數據量
http://yossi-yakubov.blogspot.co.id/2010/07 /aes-encryption-using-c-short-way.html –