using System; using System.IO; using System.Security.Cryptography; using System.Text; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.IO; using Org.BouncyCastle.Security; namespace DS.Module.Core.Helpers { /// /// 3DES加密 解密 /// public static class EncrypteHelper { /// /// 加密 /// /// /// /// public static string EncryptData(string input, string key) { var inCipher = CreateCipher(true, key); var inputArray = Encoding.UTF8.GetBytes(input); byte[] cipherData = inCipher.DoFinal(inputArray); return Convert.ToBase64String(cipherData); } /// /// 解密 /// /// /// /// public static string DecryptData(string input, string key) { var inputArrary = Convert.FromBase64String(input); var outCipher = CreateCipher(false, key); var encryptedDataStream = new MemoryStream(inputArrary, false); var dataStream = new MemoryStream(); var outCipherStream = new CipherStream(dataStream, null, outCipher); int ch; while ((ch = encryptedDataStream.ReadByte()) >= 0) { outCipherStream.WriteByte((byte)ch); } outCipherStream.Close(); encryptedDataStream.Close(); var dataBytes = dataStream.ToArray(); return Encoding.UTF8.GetString(dataBytes); } static IBufferedCipher CreateCipher(bool forEncryption, string key, string cipMode = "DESede/ECB/PKCS5Padding") { var algorithmName = cipMode; if (cipMode.IndexOf('/') >= 0) { algorithmName = cipMode.Substring(0, cipMode.IndexOf('/')); } var cipher = CipherUtilities.GetCipher(cipMode); var keyBytes = Encoding.UTF8.GetBytes(key); var keyParameter = ParameterUtilities.CreateKeyParameter(algorithmName, keyBytes); cipher.Init(forEncryption, keyParameter); return cipher; } } }