本文整理汇总了C#中System.Security.Cryptography.Rfc2898DeriveBytes.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Rfc2898DeriveBytes.GetBytes方法的具体用法?C# Rfc2898DeriveBytes.GetBytes怎么用?C# Rfc2898DeriveBytes.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.Rfc2898DeriveBytes
的用法示例。
在下文中一共展示了Rfc2898DeriveBytes.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Criptografar
public static string Criptografar(string texto)
{
if(string.IsNullOrEmpty(texto))
return String.Empty;
string outStr;
RijndaelManaged aesAlg = null;
try
{
var key = new Rfc2898DeriveBytes(Segredo, Complemento);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(texto);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
示例2: AES_Encrypt
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;
}
示例3: 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);
}
示例4: Descriptografar
public static string Descriptografar(string codigo)
{
try
{
if (string.IsNullOrEmpty(codigo))
return String.Empty;
string retorno;
var chave = new Rfc2898DeriveBytes(Segredo, Complemento);
var algoritimo = new RijndaelManaged();
algoritimo.Key = chave.GetBytes(algoritimo.KeySize / 8);
algoritimo.IV = chave.GetBytes(algoritimo.BlockSize / 8);
var descriptografor = algoritimo.CreateDecryptor(algoritimo.Key, algoritimo.IV);
var bytes = Convert.FromBase64String(codigo);
using (var memoryStream = new MemoryStream(bytes))
using (var cryptoStream = new CryptoStream(memoryStream, descriptografor, CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream))
retorno = streamReader.ReadToEnd();
algoritimo.Clear();
return retorno;
}
catch (Exception)
{
return "DEU PAU";
}
}
示例5: DecryptAes
public static void DecryptAes(Stream inStream, Stream outStream, string password)
{
if (inStream == null)
{
throw new ArgumentNullException("inStream");
}
if (outStream == null)
{
throw new ArgumentNullException("outStream");
}
if (password == null)
{
throw new ArgumentNullException("password");
}
// generate an encryption key with the shared secret and salt
using (var key = new Rfc2898DeriveBytes(password, Salt))
{
using (var aesAlg = new RijndaelManaged())
{
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
using (var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
{
using (var csEncrypt = new CryptoStream(inStream, decryptor, CryptoStreamMode.Read))
{
csEncrypt.CopyTo(outStream);
}
}
}
}
}
示例6: Decrypt
/// <summary>
/// Decrypts the value <paramref name="toDecrypt"/> using the <paramref name="password"/>.
/// </summary>
/// <param name="password">The password.</param>
/// <param name="toDecrypt">The value to decrypt.</param>
/// <returns>Decrypted value</returns>
public static string Decrypt(string password, string toDecrypt)
{
Rijndael rinedal = null;
byte[] toDecBytes = Convert.FromBase64String(toDecrypt);
try
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, GenerateSalt(password));
rinedal = Rijndael.Create();
rinedal.Padding = PaddingMode.ISO10126;
ICryptoTransform tx = rinedal.CreateDecryptor(pdb.GetBytes(32), pdb.GetBytes(16));
byte[] decrypted = tx.TransformFinalBlock(toDecBytes, 0, toDecBytes.Length);
return Encoding.Default.GetString(decrypted);
}
finally
{
if (rinedal != null)
{
rinedal.Clear();
}
}
}
示例7: Encrypt
public static string Encrypt(this string text)
{
if (string.IsNullOrEmpty(text))
{
return null;
}
byte[] rawPlaintext = System.Text.Encoding.Unicode.GetBytes(text);
byte[] cipherText = null;
using (Aes aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = AesKeySizeInBits;
int KeyStrengthInBytes = aes.KeySize / 8;
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
new System.Security.Cryptography.Rfc2898DeriveBytes(Password, Salt, Rfc2898KeygenIterations);
aes.Key = rfc2898.GetBytes(KeyStrengthInBytes);
aes.IV = rfc2898.GetBytes(KeyStrengthInBytes);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(rawPlaintext, 0, rawPlaintext.Length);
}
cipherText = ms.ToArray();
}
return ByteArrayToString(cipherText);
}
}
示例8: CreateAes
private static Aes CreateAes(ProgramOptions options, byte[] iv = null)
{
if (!options.EncryptionEnabled)
return null;
var salt = Convert.FromBase64String("hkuDTnecxj+oDytliJ69BQ==");
using (var kdf = new Rfc2898DeriveBytes(options.EncryptionPassword, salt))
{
var aes = new AesCryptoServiceProvider();
var keyLen = aes.KeySize/8;
if (iv != null)
{
aes.Key = kdf.GetBytes(keyLen);
aes.IV = iv;
return aes;
}
var ivLength = aes.BlockSize/8;
var bytes = kdf.GetBytes(keyLen + ivLength);
aes.Key = bytes.SubArray(0, keyLen);
aes.IV = bytes.SubArray(keyLen, ivLength);
return aes;
}
}
示例9: DecryptFile
public void DecryptFile(string sourceFilename, string destinationFilename, string password)
{
AesManaged aes = new AesManaged();
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
// NB: Rfc2898DeriveBytes initialization and subsequent calls to GetBytes must be eactly the same, including order, on both the encryption and decryption sides.
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
using (FileStream destination = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
{
try
{
using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
source.CopyTo(cryptoStream);
}
}
catch (CryptographicException exception)
{
if (exception.Message == "Padding is invalid and cannot be removed.")
throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);
else
throw;
}
}
}
}
示例10: Encrypt
/// <summary>
/// Encrypts the specified string.
/// </summary>
/// <param name="clearText">The string to be encrypted.</param>
/// <param name="key">The key.</param>
/// <returns></returns>
public static string Encrypt(this string clearText, string key)
{
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
示例11: Decrypt
public static string Decrypt(string ciphertext, string key)
{
if (string.IsNullOrEmpty(ciphertext))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
var allTheBytes = Convert.FromBase64String(ciphertext);
var saltBytes = allTheBytes.Take(_saltSize).ToArray();
var ciphertextBytes = allTheBytes.Skip(_saltSize).Take(allTheBytes.Length - _saltSize).ToArray();
using (var keyDerivationFunction = new Rfc2898DeriveBytes(key, saltBytes))
{
var keyBytes = keyDerivationFunction.GetBytes(32);
var ivBytes = keyDerivationFunction.GetBytes(16);
using (var aesManaged = new AesManaged())
using (var decryptor = aesManaged.CreateDecryptor(keyBytes, ivBytes))
using (var memoryStream = new MemoryStream(ciphertextBytes))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
示例12: Encrypt
/// <summary>
/// Encrypts the plainText input using the given Key.
/// A 128 bit random salt will be generated and prepended to the ciphertext before it is returned.
/// </summary>
/// <param name="plainText">The plain text to encrypt.</param>
/// <param name="password">The plain text encryption key.</param>
/// <returns>The salt and the ciphertext</returns>
public static byte[] Encrypt(byte[] plainText, string password)
{
if (plainText == null || plainText.Length == 0) throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");
// Derive a new Salt, Key, and IV from the password
using (var keyDerivationFunction = new Rfc2898DeriveBytes(password, SaltSize))
{
var saltBytes = keyDerivationFunction.Salt;
var keyBytes = keyDerivationFunction.GetBytes(32);
var ivBytes = keyDerivationFunction.GetBytes(16);
using (var aesManaged = new AesManaged())
using (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new BinaryWriter(cryptoStream))
{
streamWriter.Write(plainText);
}
var cipherTextBytes = memoryStream.ToArray();
Array.Resize(ref saltBytes, saltBytes.Length + cipherTextBytes.Length);
Array.Copy(cipherTextBytes, 0, saltBytes, SaltSize, cipherTextBytes.Length);
return saltBytes;
}
}
}
示例13: Encrypt
//http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
public string Encrypt(string data)
{
byte[] bytesToBeEncrypted = Encoding.ASCII.GetBytes(data);
byte[] passwordBytes = Encoding.ASCII.GetBytes(_configManager.Get("encryptionPassword"));
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = Encoding.ASCII.GetBytes(_configManager.Get("encryptionSalt"));
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;
return ASCIIEncoding.ASCII.GetString(encryptedBytes);
}
示例14: Decrypt
private static string Decrypt(byte[] cipher, string passPhrase, string salt, SymmetricAlgorithm algorithm)
{
var saltBytes = Encoding.UTF8.GetBytes(salt);
algorithm.Padding = PaddingMode.None;
using (algorithm)
{
using (var password = new Rfc2898DeriveBytes(passPhrase, saltBytes))
{
algorithm.Key = password.GetBytes(algorithm.KeySize / 8);
algorithm.IV = password.GetBytes(algorithm.BlockSize / 8);
using (var memStream = new MemoryStream(cipher))
{
using (
var cryptoStream = new CryptoStream(memStream, algorithm.CreateDecryptor(),
CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cryptoStream))
{
return sr.ReadToEnd();
}
}
}
}
}
}
示例15: DecryptFile
internal static void 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
var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
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);
}
}
catch (CryptographicException)
{
throw new InvalidDataException("Please supply a correct password");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
input.Close();
output.Close();
}