Created
May 19, 2015 04:47
-
-
Save fantasywind/e52ad5e8845d417af287 to your computer and use it in GitHub Desktop.
雷雷 der
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// var encryptedstring = Crypt.Encrypt("我要加密", "This is password", "Taisugar2015", "SHA1", 3, "1qazxsw23edcvfr4", 256); | |
/// var decryptedstring = Crypt.Decrypt(encryptedstring, "This is password", "Taisugar2015", "SHA1", 3, "1qazxsw23edcvfr4", 256); | |
/// </summary> | |
/// <param name="passtext"></param> | |
/// <param name="passPhrase"></param> | |
/// <param name="saltValue"></param> | |
/// <param name="hashAlgorithm"></param> | |
/// <param name="passwordIterations"></param> | |
/// <param name="initVector"></param> | |
/// <param name="keySize"></param> | |
/// <returns></returns> | |
public static string Encrypt(string passtext, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize) | |
{ | |
string functionReturnValue = null; | |
// Convert strings into byte arrays. | |
// Let us assume that strings only contain ASCII codes. | |
// If strings include Unicode characters, use Unicode, UTF7, or UTF8 | |
// encoding. | |
byte[] initVectorBytes = null; | |
initVectorBytes = Encoding.ASCII.GetBytes(initVector); | |
byte[] saltValueBytes = null; | |
saltValueBytes = Encoding.ASCII.GetBytes(saltValue); | |
// Convert our plaintext into a byte array. | |
// Let us assume that plaintext contains UTF8-encoded characters. | |
byte[] plainTextBytes = null; | |
plainTextBytes = Encoding.UTF8.GetBytes(passtext); | |
// First, we must create a password, from which the key will be derived. | |
// This password will be generated from the specified passphrase and | |
// salt value. The password will be created using the specified hash | |
// algorithm. Password creation can be done in several iterations. | |
PasswordDeriveBytes password = default(PasswordDeriveBytes); | |
password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); | |
// Use the password to generate pseudo-random bytes for the encryption | |
// key. Specify the size of the key in bytes (instead of bits). | |
byte[] keyBytes = null; | |
keyBytes = password.GetBytes(keySize / 8); | |
// Create uninitialized Rijndael encryption object. | |
RijndaelManaged symmetricKey = default(RijndaelManaged); | |
symmetricKey = new RijndaelManaged(); | |
// It is reasonable to set encryption mode to Cipher Block Chaining | |
// (CBC). Use default options for other symmetric key parameters. | |
symmetricKey.Mode = CipherMode.CBC; | |
// Generate encryptor from the existing key bytes and initialization | |
// vector. Key size will be defined based on the number of the key | |
// bytes. | |
ICryptoTransform encryptor = default(ICryptoTransform); | |
encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); | |
// Define memory stream which will be used to hold encrypted data. | |
MemoryStream memoryStream = default(MemoryStream); | |
memoryStream = new MemoryStream(); | |
// Define cryptographic stream (always use Write mode for encryption). | |
CryptoStream cryptoStream = default(CryptoStream); | |
cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); | |
// Start encrypting. | |
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); | |
// Finish encrypting. | |
cryptoStream.FlushFinalBlock(); | |
// Convert our encrypted data from a memory stream into a byte array. | |
byte[] cipherTextBytes = null; | |
cipherTextBytes = memoryStream.ToArray(); | |
// Close both streams. | |
memoryStream.Close(); | |
cryptoStream.Close(); | |
// Convert encrypted data into a base64-encoded string. | |
string cipherText = null; | |
cipherText = Convert.ToBase64String(cipherTextBytes); | |
functionReturnValue = cipherText; | |
return functionReturnValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: https://msdn.microsoft.com/zh-tw/library/system.security.cryptography.passwordderivebytes%28v=vs.110%29.aspx