C# 3DES加密解密

海盗色色 2013-07-10 05:10:33
密钥:0123456789ABCDEF0123456789ABCDEF
密文:DC8E659612F5E0AD
明文:48B85E63C34EE31F
如上:密文经密钥解密后得到明文

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

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

多谢多谢
...全文
861 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangboforce 2013-10-16
  • 打赏
  • 举报
回复
引用 16 楼 hubro 的回复:
为什么我调用代码结果密文为32位啊
Sharpend 2013-07-11
  • 打赏
  • 举报
回复
引用 8 楼 Sharpend 的回复:
DES的加密和解密
看错了...3d的我没研究过...
Sharpend 2013-07-11
  • 打赏
  • 举报
回复
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
是我发的地方不对吗 怎么没人来
  • 打赏
  • 举报
回复
没有做过,不过在网上给你找了个方法,你可以后试试 http://www.oschina.net/code/snippet_153287_4820
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
我来顶一下 呼唤下高手
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
楼上 我要的3DES加密 我的明文密文密钥都有了 您试过了吗?
hubro 2013-07-11
  • 打赏
  • 举报
回复
郑州,你请么
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
哥啊 非常感谢 您在哪里 我要请你吃饭
hubro 2013-07-11
  • 打赏
  • 举报
回复
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
你那里通过这种算法 密文解密后能得出我说的这种明文吗
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
给你留言了 能不能给个联系方式 我忙上去测试 不行的话 还得请教您
hubro 2013-07-11
  • 打赏
  • 举报
回复
就是下面的ECB
海盗色色 2013-07-11
  • 打赏
  • 举报
回复
楼上 呼叫 你二倍长的代码里MAC是你封装的动态库吧
hubro 2013-07-11
  • 打赏
  • 举报
回复
谁把我回的贴子删了,蛋疼 3DES分二倍长和三倍长,微软封装的是三倍长,二倍长得自已写 ECB和CBC结果是一样的


/// <summary>
        /// 3des二倍长加密
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] _3DES2Encrypt(byte[] key, byte[] iv, byte[] data)
        {
            byte[] key1 = new byte[8];
            Array.Copy(key, 0, key1, 0, 8);
            byte[] key2 = new byte[8];
            Array.Copy(key, 8, key2, 0, 8);
            byte[] data1 = MAC.EncryptECB(data,key1,iv);
            data1 = MAC.DecryptECB(data1,key2,iv);
            data1 = MAC.EncryptECB(data1, key1, iv);
            return data1;
        }
        /// <summary>
        /// 3des二倍长解密
        /// </summary>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] _3DES2Descrypt(byte[] key, byte[] iv, byte[] data)
        {

            byte[] key1 = new byte[8];
            Array.Copy(key, 0, key1, 0, 8);
            byte[] key2 = new byte[8];
            Array.Copy(key, 8, key2, 0, 8);

            byte[] data1 = MAC.DecryptECB(data, key1, iv);
            data1 = MAC.EncryptECB(data1, key2, iv);
            data1 = MAC.DecryptECB(data1, key1, iv);
            return data1;
        }
ECB加解密

 /// <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;
        }
BBHor 2013-07-11
  • 打赏
  • 举报
回复
楼主可以给我发私信,咱们交流一下,我已经加关注了; 我跟你一样也在弄3DES 二倍长 CBC模式的加密...
laisui 2013-07-10
  • 打赏
  • 举报
回复

using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyDes
{
    /// <summary>
    /// 字符串加解密
    /// </summary>
    public static class DES
    {
        //默认密钥向量
        private static byte[] IV = { 0x67, 0x51, 0x40, 0x2F, 0xDE, 0x0F, 0x3E,0x1A};
        private static string key = "@yourkey";
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string Encrypt(string toEncryptString)
        {
            try
            {
                if (toEncryptString == null || toEncryptString == "" || toEncryptString == string.Empty)
                {
                    return "";
                }
                byte[] rgbKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));//
                byte[] rgbIV = IV;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(toEncryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return toEncryptString;
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="toDecryptString">待解密的字符串</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string Decrypt(string toDecryptString)
        {
            try
            {
                if (toDecryptString == null || toDecryptString == "" || toDecryptString == string.Empty)
                {
                    return "";
                }
                byte[] rgbKey = Encoding.UTF8.GetBytes(key);
                byte[] rgbIV = IV;
                byte[] inputByteArray = Convert.FromBase64String(toDecryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray(), 0, mStream.ToArray().Length);
            }
            catch
            {
                return toDecryptString;
            }
        }

        /// <summary>
        /// DES加密方法
        /// </summary>
        /// <param name="toEncryptString">明文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>密文</returns>
        public static string Encrypt(string toEncryptString, string Key, string IV)
        {
            //把密钥转换成字节数组
            byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(Key);
            //把向量转换成字节数组
            byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(IV);
            //声明1个新的DES对象
            DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();
            //开辟一块内存流
            MemoryStream msEncrypt = new MemoryStream();
            //把内存流对象包装成加密流对象
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Write);
            //把加密流对象包装成写入流对象
            StreamWriter swEncrypt = new StreamWriter(csEncrypt);
            //写入流对象写入明文
            swEncrypt.WriteLine(toEncryptString);
            //写入流关闭
            swEncrypt.Close();
            //加密流关闭
            csEncrypt.Close();
            //把内存流转换成字节数组,内存流现在已经是密文了
            byte[] bytesCipher = msEncrypt.ToArray();
            //内存流关闭
            msEncrypt.Close();
            //把密文字节数组转换为字符串,并返回
            return Encoding.UTF8.GetString(bytesCipher, 0, bytesCipher.Length);
        }


        /// <summary>
        /// DES解密方法
        /// </summary>
        /// <param name="toDecryptString">密文</param>
        /// <param name="Key">密钥</param>
        /// <param name="IV">向量</param>
        /// <returns>明文</returns>
        public static string Decrypt(string toDecryptString, string Key, string IV)
        {
            //把密钥转换成字节数组
            byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(Key);
            //把向量转换成字节数组
            byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(IV);
            //把密文转换成字节数组
            byte[] bytesCipher = UnicodeEncoding.Unicode.GetBytes(toDecryptString);
            //声明1个新的DES对象
            DESCryptoServiceProvider desDecrypt = new DESCryptoServiceProvider();
            //开辟一块内存流,并存放密文字节数组
            MemoryStream msDecrypt = new MemoryStream(bytesCipher);
            //把内存流对象包装成解密流对象
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, desDecrypt.CreateDecryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Read);
            //把解密流对象包装成读出流对象
            StreamReader srDecrypt = new StreamReader(csDecrypt);
            //明文=读出流的读出内容
            string strPlainText = srDecrypt.ReadLine();
            //读出流关闭
            srDecrypt.Close();
            //解密流关闭
            csDecrypt.Close();
            //内存流关闭
            msDecrypt.Close();
            //返回明文
            return strPlainText;
        }

    }
}
海盗色色 2013-07-10
  • 打赏
  • 举报
回复
已经试过了 不是弱密钥错误就是要解密的数据的长度无效等等 不熟悉 不知道改哪里
yunlovejia 2013-07-10
  • 打赏
  • 举报
回复
System.Security.Cryptography 在这个空间里有加密解密的东西吧。 找找看。我现在没有VS,没法写代码。

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧