-
-
[旧帖]
C#的DES EBC转成 C++、C都可以。。
0.00雪花
-
-
[旧帖] C#的DES EBC转成 C++、C都可以。。
0.00雪花
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模式吧?。。对算法一窍不通。。网上下载了一下解出来不正确呢。。
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课