当前位置: 代码迷 >> .NET相关 >> C# 3DES加密解密 3DES加密解密 3DES加密解密解决思路
  详细解决方案

C# 3DES加密解密 3DES加密解密 3DES加密解密解决思路

热度:174   发布时间:2016-04-24 03:03:29.0
C# 3DES加密解密 3DES加密解密 3DES加密解密
3DE的加密解密 
密钥:0123456789ABCDEF0123456789ABCDEF
密文:DC8E659612F5E0AD
明文:48B85E63C34EE31F
如上:密文经密钥解密后得到明文

找了几段代码,总是得不出这样的结果啊
哪位大侠以前用过,帮忙看看,给段加解密代码,能够得出这样的结果的。

实在不行,C代码也行,用C先编个动态库也用着,急啊

多谢多谢  
------解决方案--------------------
3DES分二倍长和三倍长,微软封装的是三倍长,两倍长得自已写

/// <summary>
        /// ECB解密
        /// </summary>
        /// <param name="encryptedDataBytes"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static byte[] DecryptECB(byte[] encryptedDataBytes, byte[] keys, Byte[] iv)
        {
            //Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //Byte[] keys = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, key.Length));

            //Byte[] encryptedDataBytes = System.Convert.FromBase64String(sourceData);
            MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);
            DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
            decryptor.Mode = CipherMode.ECB;
            decryptor.Padding = PaddingMode.None;
            CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(keys, iv), CryptoStreamMode.Read);
            //StreamReader allDataReader = new StreamReader(decryptionStream);
            byte[] data = new byte[encryptedDataBytes.Length];
            decryptionStream.Read(data, 0, data.Length);
            decryptionStream.Close();
            tempStream.Close();
            return data;

        }
 /// <summary>
        /// ECB加密
        /// </summary>
        /// <param name="sourceDataBytes"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static byte[] EncryptECB(byte[] sourceDataBytes, byte[] keys, Byte[] iv)
        {

            //Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            MemoryStream tempStream = new MemoryStream();
            //get encryptor and encryption stream
            DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
            encryptor.Mode = CipherMode.ECB;
            encryptor.Padding = PaddingMode.None;
            CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(keys, iv), CryptoStreamMode.Write);
            encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
            encryptionStream.FlushFinalBlock();
            encryptionStream.Close();
            byte[] encryptedDataBytes = tempStream.ToArray();
            tempStream.Close();
            return encryptedDataBytes;
        }
 /// <summary>
        /// 3des二倍长加密