Ejemplo n.º 1
0
        public string TripleDesEncrypt(string source, string keystr)
        {
            byte[] data = Encoding.UTF8.GetBytes(source);
            byte[] key = GetByteKey(keystr);
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Padding = PaddingMode.PKCS7;

            //Decrypt
            Type t = Type.GetType("System.Security.Cryptography.CryptoAPITransformMode");
            object obj = t.GetField("Encrypt", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).GetValue(t);

            MethodInfo mi = des.GetType().GetMethod("_NewEncryptor", BindingFlags.Instance | BindingFlags.NonPublic);
            ICryptoTransform desCrypt = (ICryptoTransform)mi.Invoke(des, new object[] { key, CipherMode.ECB, null, 0, obj });

            byte[] result = desCrypt.TransformFinalBlock(data, 0, data.Length);

            return BitConverter.ToString(result).Replace("-", "");
        }