c#加密解密
2007-10-24 10:55
274 查看
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace CryptoUtil
{
/// <summary>
/// CryptoUtil 的摘要说明。
/// </summary>
public class CryptoUtil
{
//随机选8个字节既为密钥也为初始向量
Byte[] byKey64={42, 16, 93, 156, 78, 4, 218, 32};
Byte[] Iv64={55, 103, 246, 79, 36, 99, 167, 3};
Byte[] byKey192={42, 16, 93, 156, 78, 4, 218, 32,15, 167, 44, 80, 26, 250, 155, 112,2, 94, 11, 204, 119, 35, 184, 197};
Byte[] Iv192={55, 103, 246, 79, 36, 99, 167, 3,42, 5, 62, 83, 184, 7, 209, 13, 145, 23, 200, 58, 173, 10, 121, 222};
public CryptoUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 标准的DES加密
/// </summary>
/// <param name="str_Sql">标准的DES加密</param>
private String Encrypt(String strText)
{
//Byte[] byKey = {};
//Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
//byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 标准的DES解密
/// </summary>
/// <param name="str_Sql">标准的DES解密</param>
private String Decrypt(String strText)
{
//Byte[] byKey = {};
//Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Byte[] inputByteArray = new byte[strText.Length];
try
{
//byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
}
}
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace CryptoUtil
{
/// <summary>
/// CryptoUtil 的摘要说明。
/// </summary>
public class CryptoUtil
{
//随机选8个字节既为密钥也为初始向量
Byte[] byKey64={42, 16, 93, 156, 78, 4, 218, 32};
Byte[] Iv64={55, 103, 246, 79, 36, 99, 167, 3};
Byte[] byKey192={42, 16, 93, 156, 78, 4, 218, 32,15, 167, 44, 80, 26, 250, 155, 112,2, 94, 11, 204, 119, 35, 184, 197};
Byte[] Iv192={55, 103, 246, 79, 36, 99, 167, 3,42, 5, 62, 83, 184, 7, 209, 13, 145, 23, 200, 58, 173, 10, 121, 222};
public CryptoUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 标准的DES加密
/// </summary>
/// <param name="str_Sql">标准的DES加密</param>
private String Encrypt(String strText)
{
//Byte[] byKey = {};
//Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
//byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey64, Iv64), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 标准的DES解密
/// </summary>
/// <param name="str_Sql">标准的DES解密</param>
private String Decrypt(String strText)
{
//Byte[] byKey = {};
//Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Byte[] inputByteArray = new byte[strText.Length];
try
{
//byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey64, Iv64), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex)
{
return ex.Message;
}
}
}
}
相关文章推荐
- Aes使用Java加密C#解密
- C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密
- C# 加密解密业务类
- C# 加密与解密
- C#之AES256位加密解密
- C#使用RSA进行私钥加密公钥解密
- C# 3DES加密解密
- C# DES 加密/解密,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- c#中MD5的加密解密
- JAVA和C# 3DES加密解密
- C# 字符串加密解密方法
- C#加密解密方法汇集
- C# 常用加密解密方法
- C#开发中常用加密解密方法解析
- 【转】用 Java 解密 C# 加密的数据(DES)
- c#中base64加密解密
- C# RSA加密、解密、加签、验签、支持JAVA格式公钥私钥、PEM格式公钥私钥、.NET格式公钥私钥、一般模式【支持公钥加密,私钥解密】(一)
- [C#] 加密解密的寫法
- c#加密解密文件