首页
社区
课程
招聘
[旧帖] C#的DES EBC转成 C++、C都可以。。 0.00雪花
发表于: 2010-9-2 00:39 4689

[旧帖] C#的DES EBC转成 C++、C都可以。。 0.00雪花

2010-9-2 00:39
4689
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace DESDemo
{
    class Program
    {
        static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("12345678");

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Original String: ");

                string originalString = Console.ReadLine();
                string cryptedString = Encrypt(originalString);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nEncrypt Result: {0}", cryptedString);
                Console.WriteLine("Decrypt Result: {0}", Decrypt(cryptedString));
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("From: {0}.\nDetail: {1}", ex.Source, ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }

        /// <summary>
        /// Encrypt a string.
        /// </summary>
        /// <param name="originalString">The original string.</param>
        /// <returns>The encrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be thrown when the original string is null or empty.</exception>
        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            cryptoProvider.Mode = CipherMode.ECB;
            cryptoProvider.Padding = PaddingMode.Zeros;
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);

            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();

            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }

        /// <summary>
        /// Decrypt a crypted string.
        /// </summary>
        /// <param name="cryptedString">The crypted string.</param>
        /// <returns>The decrypted string.</returns>
        /// <exception cref="ArgumentNullException">This exception will be thrown when the crypted string is null or empty.</exception>
        public static string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            cryptoProvider.Mode = CipherMode.ECB;
            cryptoProvider.Padding = PaddingMode.Zeros;
            MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
            

            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);

            return reader.ReadToEnd();
        }
    }
}



哪位大牛帮忙转一下上面的C#代码到C++或者C的。。。
谢谢了。
这个是DES EBC模式吧?。。对算法一窍不通。。网上下载了一下解出来不正确呢。。

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 31
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
深夜发帖、深夜等待…………
2010-9-2 00:42
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
C#到C++的托管代码
2010-9-2 11:21
0
雪    币: 998
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
ECB模式,不是EBC模式。提示你一下,网上的des c的代码有一个通病,字节顺序不正确。劝你还是先好好看看算法吧。
2010-9-3 00:37
0
游客
登录 | 注册 方可回帖
返回
//