.NET Symmetric encryption
来源:互联网 发布:顶尖手机数据恢复软件 编辑:程序博客网 时间:2024/05/01 15:28
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptionTester
{
class RijndaelWrapper
{
// Retruns salt from password.
private static byte[] Password2Salt(String password)
{
int saltLength = password.Length * ((int)password[0] % 3 + 2);
saltLength = Math.Max(8, saltLength);
byte[] resSalt = new byte[saltLength];
for (int i = 0; i < saltLength; ++i)
resSalt[i] =
Convert.ToByte(password[i % password.Length] ^ (i + 17));
return resSalt;
}
// Returns symmetric algorithm with key, IV and salt extracted from
// password string.
private static SymmetricAlgorithm GetAlgorithm(string password)
{
// Init key
byte[] saltBytes = Password2Salt(password);
Rfc2898DeriveBytes rfc2898 =
new Rfc2898DeriveBytes(password, saltBytes);
// Init algorithm
SymmetricAlgorithm sa = new RijndaelManaged();
sa.Key = rfc2898.GetBytes(sa.KeySize / 8);
sa.IV = rfc2898.GetBytes(sa.BlockSize / 8);
return sa;
}
// Encrypts text using password, returns encrypted string
// in base64 representation.
public static String Encrypt(String text, String password)
{
SymmetricAlgorithm sa = GetAlgorithm(password);
// Encrypt text
ICryptoTransform ict = sa.CreateEncryptor();
using (MemoryStream mso = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(mso, ict,
CryptoStreamMode.Write))
{
byte[] textByteArray = Encoding.UTF8.GetBytes(text);
cs.Write(textByteArray, 0, textByteArray.Length);
}
// Convert encrypted text from bytes to base64 representation
byte[] encryptedTextBytesArray = mso.ToArray();
string base64EncryptedText =
Convert.ToBase64String(encryptedTextBytesArray);
return base64EncryptedText;
}
}
// Decrypts text in base64 representation (thats what Encrypt() returns)
// using password, returns decrypted string. Throws CryptographicException
// in case of bad password, input text or other errors.
public static String Decrypt(String base64EncryptedText, String password)
{
SymmetricAlgorithm sa = GetAlgorithm(password);
// Decrypt text
ICryptoTransform ict = sa.CreateDecryptor();
using (MemoryStream mso = new MemoryStream())
{
using (CryptoStream sc = new CryptoStream(mso, ict,
CryptoStreamMode.Write))
{
byte[] base64EncryptedTextByteArray =
Convert.FromBase64String(base64EncryptedText);
sc.Write(base64EncryptedTextByteArray, 0,
base64EncryptedTextByteArray.Length);
}
// Return decrypted string
return Encoding.UTF8.GetString(mso.ToArray());
}
}
}
class Program
{
static void Main(string[] args)
{
// Input from user
string text = "a text we want to keep secret";
string password = "ourpass";
// Text to be sent over network, put to database, etc...
string encryptedText = RijndaelWrapper.Encrypt(text, password);
// Now we want to get decrypt text back
string decryptedText = string.Empty;
try
{
decryptedText = RijndaelWrapper.Decrypt(encryptedText, password);
}
catch (CryptographicException)
{
// Wrong password??
decryptedText = "??";
}
// Output results
Console.WriteLine("Text to encrypt : " + text);
Console.WriteLine("Base64 encrypted text : " + encryptedText);
Console.WriteLine("Decrypted text : " + decryptedText);
}
}
}
- .NET Symmetric encryption
- .NET 2.0 Symmetric Encryption Code Sample
- Symmetric encryption padding
- Symmetric encryption principles
- java之Symmetric encryption techniques
- Encryption review-part 1 Symmetric-key algorithm
- SQL SERVER – Introduction to SQL Server Encryption and Symmetric Key Encryption Tutorial with Script
- Encryption
- Public Key RSA Encryption in C# .NET
- 介绍一篇关于 .NET C#中使用 Encryption 的文章
- 使用WinSCP .Net Assembly访问FTP with TLS Explicit Encryption
- XML Encryption
- encryption:gpg
- Encryption Technology
- DATA ENCRYPTION
- AES encryption
- toj3070 Encryption
- Text Encryption
- 昨天 10-22
- Linux启动分析专题 (1)
- netbeans5.5使用技巧
- Linux启动分析专题(2)
- tomcat性能调整
- .NET Symmetric encryption
- 可替代VB自带的Timer控件的Timer类
- 幼学琼林--Spring下的单元测试要点
- asp实现安全退出
- 菜鸟谈流
- 关于上下文
- ASP服务器组件的编程
- appDomain and Context
- Java 初始化过程详解