📄 DNS1.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace des
{
class DNS1
{
public static string main(string args)
{
if (args.Length < 1)
{
Console.WriteLine("usage: des_demo <string-to-encrypt>", args);
return "没有可加密的内容";
}
// 使用utf8函数加密输入参数
UTF8Encoding utf8encoding = new UTF8Encoding();
byte[] inputbytearray = utf8encoding.GetBytes(args);
// 方式一:调用默认的des实现方法des_csp.
DES des = DES.Create();
// 方式二:直接使用des_csp()实现des的实体
//des_csp des = new des_csp();
// 初始化des加密的密钥和一个随机的、8比特的初始化向量(iv)
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
des.Key = key;
des.IV = iv;
// 建立加密流
ICryptoTransform sse = des.CreateEncryptor();
MemoryStream ms = new MemoryStream();
// 使用cryptomemorystream方法获取加密过程的输出
// CryptoStream cms = new CryptoStream(ms, sse, CryptoStreamMode.Write);
// 将symmetricstreamencryptor流中的加密数据输出到cryptomemorystream中
// sse.setsink(cms);
// 加密完毕,将结果输出到控制台
//sse.write(inputbytearray);
//sse.closestream();
using (CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write))
{
cs.Write(inputbytearray, 0, inputbytearray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
// 获取加密数据
//byte[] encrypteddata = cms.data;
// return encrypteddata ;
ms.Close();
return str;
}
/* public static string Decrypt(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
try
{
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
inputByteArray = Convert.FromBase64String(pToDecrypt);
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
des.Key = key;
des.IV = iv;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch (Exception ef)
{
return ef.Message;
}
}
}
// public static UTF8Encoding b=new UTF8Encoding();
//public static byte[] a = Encoding.ASCII.GetBytes(main());
//public static string mailText = System.Text.Encoding.ASCII.GetString(Encoding.ASCII.GetBytes (main()));
// public string mailText = System.Text.Encoding.ASCII.GetString(encrypteddata, 0, encrypteddata.Length);*/
public static string Decrypt(string decryptString)
{
try
{
byte[] rgbKey = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
byte[] inputByteArray = Convert.FromBase64String(decryptString);
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());
}
catch
{
return decryptString;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -