本文整理汇总了C#中System.Security.Cryptography.PasswordDeriveBytes.CryptDeriveKey方法的典型用法代码示例。如果您正苦于以下问题:C# PasswordDeriveBytes.CryptDeriveKey方法的具体用法?C# PasswordDeriveBytes.CryptDeriveKey怎么用?C# PasswordDeriveBytes.CryptDeriveKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.PasswordDeriveBytes
的用法示例。
在下文中一共展示了PasswordDeriveBytes.CryptDeriveKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TripleDESDecode
public string TripleDESDecode(string str)
{
try
{
if (str.Length <= 0)
return null;
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
Byte[] iv = new Byte[8]; ;
des.IV = iv;
PasswordDeriveBytes pdb = new PasswordDeriveBytes("St$$#ef)!en", Encoding.ASCII.GetBytes("St$$#ef)!en".Length.ToString()));
des.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, des.IV);
Byte[] encryptedbytes = Convert.FromBase64String(str);
MemoryStream ms = new MemoryStream(str.Length);
CryptoStream decstream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
decstream.Write(encryptedbytes, 0, encryptedbytes.Length);
decstream.FlushFinalBlock();
Byte[] plainbytes = ms.ToArray();
ms.Position = 0;
ms.Read(plainbytes, 0, (int)(ms.Length - 1));
decstream.Close();
return System.Text.Encoding.UTF8.GetString(plainbytes);
}
catch
{
return null;
}
}
示例2: Decrypt
public byte[] Decrypt(byte[] encrypted, byte[] publicKey)
{
RijndaelManaged objCrypRij = new RijndaelManaged();
byte[] bytIV = GetMD5EncodeBytes(publicKey);
objCrypRij.IV = bytIV;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(GetDecodeBytes(publicKey), new byte[0]);
objCrypRij.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
byte[] encryptedBytes = encrypted;
MemoryStream msText = new MemoryStream(encrypted.GetLength(0));
CryptoStream decStream = new CryptoStream(msText, objCrypRij.CreateDecryptor(), CryptoStreamMode.Write);
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
byte[] plainBytes = new byte[msText.Length];
msText.Position = 0;
msText.Read(plainBytes, 0, Convert.ToInt32(msText.Length));
decStream.Close();
return plainBytes;
}
示例3: PasswordDerivedBytes_Test
public void PasswordDerivedBytes_Test()
{
byte[] randBytes = new byte[5];
new Random(10032010).NextBytes(randBytes);
var tdes = new TripleDESCryptoServiceProvider();
var pwddb = new PasswordDeriveBytes("1", new byte[] { 1 });
tdes.Key = pwddb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);
//string s = Convert.ToBase64String(tdes.Key);
}
示例4: CreateTranformer
private ICryptoTransform CreateTranformer(bool forEncryption)
{
byte[] key = null;
byte[] pdbsalt = null;
byte[] iv = null;
try
{
// Salt byte array.
pdbsalt = GenerateSalt();
// Create PasswordDeriveBytes object that will generate
// a Key for TripleDES algorithm.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Key, pdbsalt);
iv = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
// Create a private key for TripleDES algorithm.
// The iv parameter is not currently used.
// * http://blogs.msdn.com/shawnfa/archive/2004/04/14/113514.aspx
key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv);
if (forEncryption)
{
return TripleDES.Create().CreateEncryptor(key, iv);
}
else
{
return TripleDES.Create().CreateDecryptor(key, iv);
}
}
catch (CryptographicException)
{
return null;
}
finally
{
if (key != null)
{
Array.Clear(key, 0, key.Length);
}
if (pdbsalt != null)
{
Array.Clear(pdbsalt, 0, pdbsalt.Length);
}
if (iv != null)
{
Array.Clear(iv, 0, iv.Length);
}
}
}
示例5: EncryptionTransform
public EncryptionTransform(String password) {
PasswordDeriveBytes passwordBytes =
new PasswordDeriveBytes(password + paranoidSaltString,salt);
// Create a TripleDESCryptoServiceProvider object.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Mode = CipherMode.ECB;
// Create the key and add it to the Key property.
tdes.Key = passwordBytes.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);
decrypter = tdes.CreateDecryptor();
encrypter = tdes.CreateEncryptor();
}
示例6: decriptUrl
public static string decriptUrl(string Valor)
{
string Chave = "#[email protected]!s#";
Valor = Valor.Replace("MAIS", "+");
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Chave, new byte[-1 + 1]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
byte[] encryptedBytes = Convert.FromBase64String(Valor);
System.IO.MemoryStream MS = new System.IO.MemoryStream(Valor.Length);
CryptoStream decStreAM = new CryptoStream(MS, des.CreateDecryptor(), CryptoStreamMode.Write);
decStreAM.Write(encryptedBytes, 0, encryptedBytes.Length);
decStreAM.FlushFinalBlock();
byte[] plainBytes = new byte[Convert.ToInt32(MS.Length - 1) + 1];
MS.Position = 0;
MS.Read(plainBytes, 0, Convert.ToInt32(MS.Length));
decStreAM.Close();
return System.Text.Encoding.UTF8.GetString(plainBytes);
}
示例7: SimpleSymmetricDecrypt
public static string SimpleSymmetricDecrypt(this string data, string key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[0]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
byte[] encryptedBytes = Convert.FromBase64String(data);
MemoryStream ms = new MemoryStream(data.Length);
CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
byte[] plainBytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(plainBytes, 0, (int)ms.Length);
decStream.Close();
return Encoding.UTF8.GetString(plainBytes);
}
示例8: criptUrl
public static string criptUrl(string valor)
{
string chave = "#[email protected]!s#";
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(chave, new byte[-1 + 1]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
System.IO.MemoryStream ms = new System.IO.MemoryStream((valor.Length * 2) - 1);
CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(valor);
encStream.Write(plainBytes, 0, plainBytes.Length);
encStream.FlushFinalBlock();
byte[] encryptedBytes = new byte[Convert.ToInt32(ms.Length - 1) + 1];
ms.Position = 0;
ms.Read(encryptedBytes, 0, Convert.ToInt32(ms.Length));
encStream.Close();
string c = null;
c = Convert.ToBase64String(encryptedBytes).Replace("+", "MAIS");
return c;
}
示例9: EncryptBytes
private static byte[] EncryptBytes(byte[] SecureBytes, string PrivateKey)
{
byte[] array = new byte[]
{
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};
PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(PrivateKey, array);
byte[] array2 = new byte[]
{
0,
0,
0,
0,
0,
0,
0,
0
};
byte[] key = passwordDeriveBytes.CryptDeriveKey("TripleDES", "MD5", 0, array2);
TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
tripleDESCryptoServiceProvider.Key = key;
tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;
return tripleDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(SecureBytes, 0, SecureBytes.Length);
}
示例10: Decrypt
public static string Decrypt(string datastr)
{
PasswordDeriveBytes bytes = new PasswordDeriveBytes("qianxun8", null);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
byte[] rgbKey = bytes.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
byte[] buffer = Encoding.Unicode.GetBytes(datastr);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
return Encoding.Unicode.GetString(stream.ToArray());
}
示例11: TRIPLEDESDecrypt
public static string TRIPLEDESDecrypt(byte[] Data, string Password, byte[] Key, byte[] IV)
{
try
{
var pdb = new PasswordDeriveBytes(Password, Key);
Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV);
//Key = pdb.GetBytes(Key.Length);
// Create a new MemoryStream using the passed
// array of encrypted data.
var msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
var csDecrypt = new CryptoStream(msDecrypt,
new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
var fromEncrypt = new byte[Data.Length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
//Convert the buffer into a string and return it.
return new UnicodeEncoding().GetString(fromEncrypt);
}
catch (CryptographicException e)
{
Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
示例12: decryptTripleDes
private string decryptTripleDes(string encryptedBase64, string Password)
{
string password = Password;
shiftLeft(ref password, 7);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.IV = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);
MemoryStream ms = new MemoryStream(encryptedBase64.Length);
CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
byte[] plainBytes = new byte[ms.Length];
ms.Position = 0;
ms.Read(plainBytes, 0, (int)ms.Length);
decStream.Close();
return Encoding.UTF8.GetString(plainBytes);
}
示例13: Encryptor
/// <summary>
/// Public constructor.
/// </summary>
public Encryptor()
{
// FIXME: AAA - need support for key and salt changing. What's best interface?
byte[] salt = Esapi.SecurityConfiguration().MasterSalt;
string pass = Esapi.SecurityConfiguration().MasterPassword;
// setup algorithms
encryptAlgorithm = Esapi.SecurityConfiguration().EncryptionAlgorithm;
signatureAlgorithm = Esapi.SecurityConfiguration().DigitalSignatureAlgorithm;
randomAlgorithm = Esapi.SecurityConfiguration().RandomAlgorithm;
hashAlgorithm = Esapi.SecurityConfiguration().HashAlgorithm;
try
{
// Set up encryption and decryption
SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create(encryptAlgorithm);
symmetricAlgorithm.GenerateIV();
iv = symmetricAlgorithm.IV;
symmetricAlgorithm.Padding = PaddingMode.PKCS7;
PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(pass, salt);
// FIXME: We are using SHA1 hardcoded here, because for some reason CryptDeriveKey doesn't
// like other hash algorithms. Also, it appears to not like Rijndael as a encryption algorithm.
secretKey = passwordDeriveBytes.CryptDeriveKey(encryptAlgorithm, "SHA1", symmetricAlgorithm.KeySize, iv);
encoding = Esapi.SecurityConfiguration().CharacterEncoding;
// 13 is the code for DSA
asymmetricKeyPair = new CspParameters(13);
// The asymmetric key will be stored in the key container using the name ESAPI.
asymmetricKeyPair.KeyContainerName = "ESAPI";
// Set up signing keypair using the master password and salt
// FIXME: Enhance - make DSA configurable
RandomNumberGenerator randomNumberGenerator = RNGCryptoServiceProvider.Create(randomAlgorithm);
}
catch (Exception e)
{
// can't throw this exception in initializer, but this will log it
new EncryptionException("Encryption failure", "Error creating Encryptor", e);
}
}
示例14: CryptDeriveKey_TooLongKey
public void CryptDeriveKey_TooLongKey ()
{
PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
pd.CryptDeriveKey ("AlgName", "MD5", -256, new byte [8]);
}
示例15: TRIPLEDESEncrypt
public static byte[] TRIPLEDESEncrypt(string Data, string Password, byte[] Key, byte[] IV)
{
try
{
var pdb = new PasswordDeriveBytes(Password, Key);
Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV);
//Key = pdb.GetBytes(Key.Length);
// Create a MemoryStream.
var mStream = new MemoryStream();
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
var cStream = new CryptoStream(mStream,
new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
// Convert the passed string to a byte array.
var toEncrypt = new UnicodeEncoding().GetBytes(Data);
// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
var ret = mStream.ToArray();
// Close the streams.
cStream.Close();
mStream.Close();
// Return the encrypted buffer.
return ret;
}
catch (CryptographicException e)
{
Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message);
return null;
}
}