public string Encrypt(string message) { LegacyCryptoKey ck = new LegacyCryptoKey(); ck.BuildKey(this.cryptoKey); byte[] data = UTF8Encoding.UTF8.GetBytes(message); MemoryStream ms = new MemoryStream(); DESCryptoServiceProvider csp = new DESCryptoServiceProvider(); CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(ck.Key, ck.IV), CryptoStreamMode.Write); cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); byte[] result = new byte[ms.Position]; ms.Position = 0; ms.Read(result, 0, result.Length); cs.Close(); ms.Close(); string encrypted = Convert.ToBase64String(result); return encrypted; }
public string Decrypt(string message) { if (message == "" || message == null) { return String.Empty; } byte[] bData = Convert.FromBase64String(message); LegacyCryptoKey ck = new LegacyCryptoKey(); ck.BuildKey(this.cryptoKey); MemoryStream memoryStream = new MemoryStream(bData.Length); DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider(); CryptoStream cryptoStream = new CryptoStream(memoryStream, desCryptoServiceProvider.CreateDecryptor(ck.Key, ck.IV), CryptoStreamMode.Read); memoryStream.Write(bData, 0, bData.Length); memoryStream.Position = 0; string decrypted = new StreamReader(cryptoStream).ReadToEnd(); cryptoStream.Close(); return decrypted; }