C# DES加密码解密类

王朝学院·作者佚名  2009-02-12
窄屏简体版  字體: |||超大  

使用方式

string Value = "我们都是一起的一起的 一起的";

string _Des = Zgke.Test.DES.DESEncoder(Value, Encoding.Default, null, null);

string _AAA = Zgke.Test.DES.DESDecoder(_Des, Encoding.Default, null, null);

MessageBox.Show(_Des, _AAA);

下面是全部的类

view plaincopy to clipboardprint?

using System;

using System.Text;

using System.Security.Cryptography;

namespace Zgke.Test

{

/// <summary>

/// DES加密 解密

/// zgke@sina.com

/// qq:116149

/// </summary>

public class DES

{

/// <summary>

/// 对字节进行DES加密

/// </summary>

/// <param name="p_Value">字节数组</param>

/// <param name="p_Key">钥匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必须是8位 </param>

/// <param name="p_IV">向量 如果为NULL 向量和钥匙是一个</param>

/// <returns>加密后的BYTE</returns>

public static byte[] DESEncoder(byte[] p_Value, byte[] p_Key, byte[] p_IV)

{

byte[] _RgbKey = p_Key;

byte[] _RgbIV = p_IV;

if (_RgbKey == null || _RgbKey.Length!= 8) _RgbKey = new byte[] { 0x7A, 0x67, 0x6B, 0x65, 0x40, 0x73, 0x69, 0x6E };

if (_RgbIV == null) _RgbIV = _RgbKey;

DESCryptoServiceProvider _Desc = new DESCryptoServiceProvider();

ICryptoTransform _ICrypto = _Desc.CreateEncryptor(_RgbKey, _RgbIV);

return _ICrypto.TransformFinalBlock(p_Value, 0, p_Value.Length);

}

/// <summary>

/// 对字节进行DES解密

/// </summary>

/// <param name="p_Value">字节数组</param>

/// <param name="p_Key">钥匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必须是8位 </param>

/// <param name="p_IV">向量 如果为NULL 向量和钥匙是一个</param>

/// <returns>解密后的BYTE</returns>

public static byte[] DESDecoder(byte[] p_Value, byte[] p_Key, byte[] p_IV)

{

byte[] _RgbKey = p_Key;

byte[] _RgbIV = p_IV;

if (_RgbKey == null || _RgbKey.Length != 8) _RgbKey = new byte[] { 0x7A, 0x67, 0x6B, 0x65, 0x40, 0x73, 0x69, 0x6E };

if (_RgbIV == null) _RgbIV = _RgbKey;

DESCryptoServiceProvider _Desc = new DESCryptoServiceProvider();

ICryptoTransform _ICrypto = _Desc.CreateDecryptor(_RgbKey, _RgbIV);

return _ICrypto.TransformFinalBlock(p_Value, 0, p_Value.Length);

}

/// <summary>

/// DES加密

/// </summary>

/// <param name="enStr">原始数据</param>

/// <param name="p_TextEncoding">数据编码</param>

/// <param name="p_Key">钥匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必须是8位 </param>

/// <param name="p_IV">向量 如果为NULL 向量和钥匙是一个</param>

/// <returns>加密后的字符穿 00-00-00</returns>

public static string DESEncoder(string p_TextValue, System.Text.Encoding p_TextEncoding,byte[] p_Key,byte[] p_IV)

{

byte[] _DataByte = p_TextEncoding.GetBytes(p_TextValue);

return BitConverter.ToString(DESEncoder(_DataByte,p_Key,p_IV));

}

/// <summary>

/// DES解密

/// </summary>

/// <param name="p_TextValue">经过加密数据</param>

/// <param name="p_TextEncoding">数据编码</param>

/// <param name="p_Key">钥匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必须是8位 </param>

/// <param name="p_IV">向量 如果为NULL 向量和钥匙是一个</param>

/// <returns>解密后的字符穿</returns>

public static string DESDecoder(string p_TextValue, System.Text.Encoding p_TextEncoding,byte[] p_Key,byte[] p_IV)

{

string[] _ByteText = p_TextValue.Split('-');

byte[] _DataByte =new byte[_ByteText.Length];

for (int i = 0; i != _ByteText.Length; i++)

{

_DataByte[i] = Convert.ToByte(_ByteText[i], 16);

}

return p_TextEncoding.GetString(DESDecoder(_DataByte,p_Key,p_IV));

}

}

}

这样不好,只是90度的呀。

C# code

//测试用

private void button1_Click(object sender, EventArgs e)

{

Bitmap a = new Bitmap(pictureBox1.Image);//得到图片框中的图片

pictureBox1 .Image = Rotate(a, 30);//这里的30为度数,

pictureBox1.Refresh();//最后刷新图片框

}

#region 图片旋转函数

/// <summary>

/// 以逆时针为方向对图像进行旋转

/// </summary>

/// <param name="b">位图流</param>

/// <param name="angle">旋转角度[0,360](前台给的)</param>

/// <returns></returns>

public Bitmap Rotate(Bitmap b, int angle)

{

angle = angle % 360;

//弧度转换

double radian = angle * Math.PI / 180.0;

double cos = Math.Cos(radian);

double sin = Math.Sin(radian);

//原图的宽和高

int w = b.Width;

int h = b.Height;

int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));

int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

//目标位图

Bitmap dsImage = new Bitmap(W, H);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//计算偏移量

Point Offset = new Point((W - w) / 2, (H - h) / 2);

//构造图像显示区域:让图像的中心与窗口的中心点一致

Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);

Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);

g.TranslateTransform(center.X, center.Y);

g.RotateTransform(360 - angle);

//恢复图像在水平和垂直方向的平移

g.TranslateTransform(-center.X, -center.Y);

g.DrawImage(b, rect);

//重至绘图的所有变换

g.ResetTransform();

g.Save();

g.Dispose();

//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

return dsImage;

}

#endregion 图片旋转函数

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航