Simple insecure two-way data “obfuscation”?

后端 未结 17 1384
别跟我提以往
别跟我提以往 2020-11-22 01:10

I\'m looking for very simple obfuscation (like encrypt and decrypt but not necessarily secure) functionality for some data. It\'s not mission critical. I need something to k

17条回答
  •  执念已碎
    2020-11-22 01:45

    Using TripleDESCryptoServiceProvider in System.Security.Cryptography :

    public static class CryptoHelper
    {
        private const string Key = "MyHashString";
        private static TripleDESCryptoServiceProvider GetCryproProvider()
        {
            var md5 = new MD5CryptoServiceProvider();
            var key = md5.ComputeHash(Encoding.UTF8.GetBytes(Key));
            return new TripleDESCryptoServiceProvider() { Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
        }
    
        public static string Encrypt(string plainString)
        {
            var data = Encoding.UTF8.GetBytes(plainString);
            var tripleDes = GetCryproProvider();
            var transform = tripleDes.CreateEncryptor();
            var resultsByteArray = transform.TransformFinalBlock(data, 0, data.Length);
            return Convert.ToBase64String(resultsByteArray);
        }
    
        public static string Decrypt(string encryptedString)
        {
            var data = Convert.FromBase64String(encryptedString);
            var tripleDes = GetCryproProvider();
            var transform = tripleDes.CreateDecryptor();
            var resultsByteArray = transform.TransformFinalBlock(data, 0, data.Length);
            return Encoding.UTF8.GetString(resultsByteArray);
        }
    }
    

提交回复
热议问题