本文整理汇总了C#中Rfc2898DeriveBytes类的典型用法代码示例。如果您正苦于以下问题:C# Rfc2898DeriveBytes类的具体用法?C# Rfc2898DeriveBytes怎么用?C# Rfc2898DeriveBytes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Rfc2898DeriveBytes类属于命名空间,在下文中一共展示了Rfc2898DeriveBytes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
// Use this for initialization
void Start () {
Rfc2898DeriveBytes PBKDF2_hash = new Rfc2898DeriveBytes("qwerty", Encoding.ASCII.GetBytes("rm4fSDh0sofK"), 20000);
HMACSHA256 HMACSHA256_hash = new HMACSHA256();
//System.Text.Encoding.UTF8.GetString(bytes);
string hPassword = Convert.ToBase64String(PBKDF2_hash.GetBytes(32));
Debug.Log("Hash = " + hPassword);
//Debug.Log("Password hash" + Encoding.ASCII.GetString(PBKDF2.GetBytes(256)));
//PBKDF2.Salt =
/*string password = "Hello world";
string hPassword = ComputeHash(password, new SHA256CryptoServiceProvider());
Debug.Log("Hash from: " + password + " = " + hPassword);*/
}
示例2: GenerateUrlToken
public static string GenerateUrlToken(string controllerName, string actionName, RouteValueDictionary argumentParams, string password)
{
//// The URL hash is dynamic by assign a dynamic key in each session. So
//// eventhough your URL is stolen, it will not work in other session
if (HttpContext.Current.Session["url_dynamickey"] == null)
{
HttpContext.Current.Session["url_dynamickey"] = RandomString();
}
// The salt include the dynamic session key and valid for an hour.
var salt = HttpContext.Current.Session["url_dynamickey"] + DateTime.Now.ToShortDateString() + " " + DateTime.Now.Hour;
// generating the partial url
var stringToToken = controllerName + "/" + actionName + "/";
stringToToken = argumentParams.Where(item => item.Key != "controller" && item.Key != "action" && item.Key != "urltoken").Aggregate(stringToToken, (current, item) => current + (item.Value));
// Converting the salt in to a byte array
var saltValueBytes = System.Text.Encoding.ASCII.GetBytes(salt);
// Encrypt the salt bytes with the password
var key = new Rfc2898DeriveBytes(password, saltValueBytes);
// get the key bytes from the above process
var secretKey = key.GetBytes(16);
// generate the hash
var tokenHash = new HMACSHA1(secretKey);
tokenHash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(stringToToken));
// convert the hash to a base64string
var token = Convert.ToBase64String(tokenHash.Hash).Replace("/", "_");
return token;
}
示例3: Encrypt
public static byte[] Encrypt(byte[] data, string password)
{
byte[] result = null;
byte[] passwordBytes = Encoding.Default.GetBytes(password);
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = keySize;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 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(data, 0, data.Length);
cs.Close();
}
result = ms.ToArray();
}
}
return result;
}
示例4: Decrypt
public static string Decrypt(string cipherText)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
示例5: GetSaltHash
internal static SecurePassword GetSaltHash(string password) {
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, 20);
byte[] salt = deriveBytes.Salt;
byte[] pass = deriveBytes.GetBytes(20);
return new SecurePassword(Convert.ToBase64String(pass), Convert.ToBase64String(salt));
}
示例6: DecryptFile
internal static bool DecryptFile(string inputPath, string outputPath, string password)
{
var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write);
// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
// 1.The block size is set to 128 bits
// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
RijndaelManaged algorithm = new RijndaelManaged {KeySize = 256, BlockSize = 128};
algorithm.Mode = CipherMode.CBC;
var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt));
algorithm.Key = key.GetBytes(algorithm.KeySize/8);
algorithm.IV = key.GetBytes(algorithm.BlockSize/8);
try
{
using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write))
{
CopyStream(input, decryptedStream);
return true;
}
}
catch (CryptographicException)
{
throw new InvalidDataException("Please supply a correct password");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
示例7: Decrypt
public static string Decrypt(this string text, string lKey)
{
try
{
using (Aes aes = new AesManaged())
{
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IVa, 0, IVa.Length), Encoding.UTF8.GetBytes(lKey));
aes.Key = deriveBytes.GetBytes(128 / 8);
aes.IV = aes.Key;
using (MemoryStream decryptionStream = new MemoryStream())
{
using (CryptoStream decrypt = new CryptoStream(decryptionStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] encryptedData = Convert.FromBase64String(text);
decrypt.Write(encryptedData, 0, encryptedData.Length);
decrypt.Flush();
}
byte[] decryptedData = decryptionStream.ToArray();
string decryptedText = Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
return decryptedText;
}
}
}
catch
{
return String.Empty;
}
}
示例8: Encrypt
public static string Encrypt(this string text, string lKey)
{
try
{
using (Aes aes = new AesManaged())
{
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IVa, 0, IVa.Length), Encoding.UTF8.GetBytes(lKey));
aes.Key = deriveBytes.GetBytes(128 / 8);
aes.IV = aes.Key;
using (MemoryStream encryptionStream = new MemoryStream())
{
using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] cleanText = Encoding.UTF8.GetBytes(text);
encrypt.Write(cleanText, 0, cleanText.Length);
encrypt.FlushFinalBlock();
}
byte[] encryptedData = encryptionStream.ToArray();
string encryptedText = Convert.ToBase64String(encryptedData);
return encryptedText;
}
}
}
catch
{
return String.Empty;
}
}
示例9: Encrypt
//This method is to encrypt the password given by user.
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);
TripleDES tdes = TripleDES.Create();
//Rijndael aes = Rijndael.Create();
tdes.IV = keyGenerator.GetBytes(tdes.BlockSize / 8);
tdes.Key = keyGenerator.GetBytes(tdes.KeySize / 8);
// encrypt the data
byte[] rawData = Encoding.Unicode.GetBytes(data);
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.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);
}
}
开发者ID:progressiveinfotech,项目名称:PRO_FY13_40_Helpdesk-Support-and-Customization_HT,代码行数:29,代码来源:Login.aspx.cs
示例10: Decrypt
//This method is to decrypt the password given by user.
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.Replace(" ", "+"));
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);
TripleDES tdes = TripleDES.Create();
//Rijndael aes = Rijndael.Create();
tdes.IV = keyGenerator.GetBytes(tdes.BlockSize / 8);
tdes.Key = keyGenerator.GetBytes(tdes.KeySize / 8);
// decrypt the data
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(rawData, 8, rawData.Length - 8);
cryptoStream.Close();
byte[] decrypted = memoryStream.ToArray();
return Encoding.Unicode.GetString(decrypted);
}
}
开发者ID:progressiveinfotech,项目名称:PRO_FY13_40_Helpdesk-Support-and-Customization_HT,代码行数:35,代码来源:Login.aspx.cs
示例11: AES_Encrypt
public static 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;
//end public byte[] AES_Encrypt
}
示例12: TryDecrypt
public bool TryDecrypt(string encryptedItem, string password, out string decryptedItem)
{
if (string.IsNullOrEmpty (encryptedItem) ||
string.IsNullOrEmpty (password)) {
decryptedItem = "";
return false;
}
try {
byte[] cipherBytes = Convert.FromBase64String (encryptedItem);
using (var memoryStream = new MemoryStream(cipherBytes)) {
var des = new DESCryptoServiceProvider ();
byte[] iv = new byte[8];
memoryStream.Read (iv, 0, iv.Length);
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, iv, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream)) {
decryptedItem = streamReader.ReadToEnd ();
return true;
}
}
} catch (Exception ex) {
Logger.instance.Log (new Logger.Message (this, ex));
decryptedItem = "";
return false;
}
}
示例13: Encrypt
public string Encrypt(string item, string password)
{
if (item == null) {
throw new ArgumentNullException ("item");
}
if (string.IsNullOrEmpty (password)) {
throw new ArgumentNullException ("password");
}
var des = new DESCryptoServiceProvider ();
des.GenerateIV ();
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, des.IV, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var memoryStream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write)) {
memoryStream.Write (des.IV, 0, des.IV.Length);
byte[] bytes = Encoding.UTF8.GetBytes (item);
cryptoStream.Write (bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock ();
return Convert.ToBase64String (memoryStream.ToArray ());
}
}
}
示例14: Encrypt
public static string Encrypt(string clearText)
{
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
示例15: PBKDF2
private static byte[] PBKDF2(string password, byte[] salt, int interations,
int outputBytes)
{
Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
pbkdf2.IterationCount = interations;
return pbkdf2.GetBytes(outputBytes);
}