如何实现 可逆加密

jason20002000 2009-04-27 11:47:41
如何实现 可逆加密
...全文
246 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ld1201 2009-04-28
  • 打赏
  • 举报
回复
base64加密,解密

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
///Base64Encoder 的摘要说明
/// </summary>
public class Base64Encoder
{
byte[] source;
int length, length2;
int blockCount;
int paddingCount;
public static Base64Encoder Encoder = new Base64Encoder();

public Base64Encoder()
{
}

private void init(byte[] input)
{
source = input;
length = input.Length;
if ((length % 3) == 0)
{
paddingCount = 0;
blockCount = length / 3;
}
else
{
paddingCount = 3 - (length % 3);
blockCount = (length + paddingCount) / 3;
}
length2 = length + paddingCount;
}

public string GetEncoded(byte[] input)
{
//初始化
init(input);

byte[] source2;
source2 = new byte[length2];

for (int x = 0; x < length2; x++)
{
if (x < length)
{
source2[x] = source[x];
}
else
{
source2[x] = 0;
}
}

byte b1, b2, b3;
byte temp, temp1, temp2, temp3, temp4;
byte[] buffer = new byte[blockCount * 4];
char[] result = new char[blockCount * 4];
for (int x = 0; x < blockCount; x++)
{
b1 = source2[x * 3];
b2 = source2[x * 3 + 1];
b3 = source2[x * 3 + 2];

temp1 = (byte)((b1 & 252) >> 2);

temp = (byte)((b1 & 3) << 4);
temp2 = (byte)((b2 & 240) >> 4);
temp2 += temp;

temp = (byte)((b2 & 15) << 2);
temp3 = (byte)((b3 & 192) >> 6);
temp3 += temp;

temp4 = (byte)(b3 & 63);

buffer[x * 4] = temp1;
buffer[x * 4 + 1] = temp2;
buffer[x * 4 + 2] = temp3;
buffer[x * 4 + 3] = temp4;

}

for (int x = 0; x < blockCount * 4; x++)
{
result[x] = sixbit2char(buffer[x]);
}


switch (paddingCount)
{
case 0: break;
case 1: result[blockCount * 4 - 1] = '='; break;
case 2: result[blockCount * 4 - 1] = '=';
result[blockCount * 4 - 2] = '=';
break;
default: break;
}
return new string(result);
}
private char sixbit2char(byte b)
{
char[] lookupTable = new char[64]{
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};

if ((b >= 0) && (b <= 63))
{
return lookupTable[(int)b];
}
else
{

return ' ';
}
}

}


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
///Base64Decoder 的摘要说明
/// </summary>
public class Base64Decoder
{
char[] source;
int length, length2, length3;
int blockCount;
int paddingCount;
public static Base64Decoder Decoder = new Base64Decoder();

public Base64Decoder()
{
}

private void init(char[] input)
{
int temp = 0;
source = input;
length = input.Length;

for (int x = 0; x < 2; x++)
{
if (input[length - x - 1] == '=')
temp++;
}
paddingCount = temp;

blockCount = length / 4;
length2 = blockCount * 3;
}

public byte[] GetDecoded(string strInput)
{
//初始化
init(strInput.ToCharArray());

byte[] buffer = new byte[length];
byte[] buffer2 = new byte[length2];

for (int x = 0; x < length; x++)
{
buffer[x] = char2sixbit(source[x]);
}

byte b, b1, b2, b3;
byte temp1, temp2, temp3, temp4;

for (int x = 0; x < blockCount; x++)
{
temp1 = buffer[x * 4];
temp2 = buffer[x * 4 + 1];
temp3 = buffer[x * 4 + 2];
temp4 = buffer[x * 4 + 3];

b = (byte)(temp1 << 2);
b1 = (byte)((temp2 & 48) >> 4);
b1 += b;

b = (byte)((temp2 & 15) << 4);
b2 = (byte)((temp3 & 60) >> 2);
b2 += b;

b = (byte)((temp3 & 3) << 6);
b3 = temp4;
b3 += b;

buffer2[x * 3] = b1;
buffer2[x * 3 + 1] = b2;
buffer2[x * 3 + 2] = b3;
}

length3 = length2 - paddingCount;
byte[] result = new byte[length3];

for (int x = 0; x < length3; x++)
{
result[x] = buffer2[x];
}

return result;
}

private byte char2sixbit(char c)
{
char[] lookupTable = new char[64]{
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y', 'Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/'};
if (c == '=')
return 0;
else
{
for (int x = 0; x < 64; x++)
{
if (lookupTable[x] == c)
return (byte)x;
}

return 0;
}

}
//解码类结束

}

xufzu123 2009-04-28
  • 打赏
  • 举报
回复
up~~
yilanwuyu123 2009-04-28
  • 打赏
  • 举报
回复
算法要求强一些,去网上搜一些资料参考参考.
a1015154569 2009-04-28
  • 打赏
  • 举报
回复
想不出
ChrisAK 2009-04-28
  • 打赏
  • 举报
回复
System.Security.Cryptography
有一堆加密算法可以用.
比如RSA,DES,AES之类的...
JackLeiMM 2009-04-28
  • 打赏
  • 举报
回复
自己写算法
蓝海D鱼 2009-04-28
  • 打赏
  • 举报
回复
这个不错

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));
}
}


}
冷月孤峰 2009-04-28
  • 打赏
  • 举报
回复
编程有钱人了 2009-04-28
  • 打赏
  • 举报
回复

#region 3DES加密和解密 DES的第三版本
/// <summary>
/// 3DES一个加密的方法
/// </summary>
/// <param name="M_Value">要加密的字符串</param>
/// <returns></returns>
public static string Encrypt3DES(string M_Value)
{
//构造一个对称算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
string M_sKey = "JEFHTWkrTAUXpfO8NJWXdGJIoQz0/Jfw";//密钥
string M_sIV = "HrxSqiWlb+A=";//矢量
ICryptoTransform ct;//
MemoryStream ms=new MemoryStream();;
CryptoStream cs;
byte[] byt=Encoding.UTF8.GetBytes(M_Value);
mCSP.Key= Convert.FromBase64String(M_sKey);
mCSP.IV = Convert.FromBase64String(M_sIV);
//指定加密的运算模式
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//获取或设置加密算法的填充模式
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// 3DES一个解密的方法
/// </summary>
/// <param name="Value">要加密的字符串</param>
/// <returns></returns>
public static string Decrypt3DES(string M_Value)
{
//构造一个对称算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
string M_sKey = "JEFHTWkrTAUXpfO8NJWXdGJIoQz0/Jfw";//密钥
string M_sIV = "HrxSqiWlb+A=";//矢量
ICryptoTransform ct;
MemoryStream ms=new MemoryStream();;
CryptoStream cs;
byte[] byt=Convert.FromBase64String(M_Value);
mCSP.Key = Convert.FromBase64String(M_sKey);
mCSP.IV = Convert.FromBase64String(M_sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}

#endregion
zgke 2009-04-28
  • 打赏
  • 举报
回复
FantasyFreed 2009-04-28
  • 打赏
  • 举报
回复
DES算法
        #region   对数据进行加密
/// <summary>
/// 对数据进行加密
/// </summary>
/// <param name="encryptstring">需要加密的数据</param>
/// <returns></returns>
public string DESEncrypt(string encryptstring)
{
string strRtn;
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);
MemoryStream ms = new MemoryStream();//存储加密后的数据
CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//进行加密
cs.FlushFinalBlock();
strRtn = Convert.ToBase64String(ms.ToArray());
return strRtn;
}

catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}
#endregion



#region 对数据进行解密
/// <summary>
/// 对数据进行解密
/// </summary>
/// <param name="decryptstring">需要解密的数据</param>
/// <returns></returns>
public string DESDecrypt(string decryptstring)
{
string strRtn;
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
byte[] data = Convert.FromBase64String(decryptstring);
MemoryStream ms = new MemoryStream();//存储解密后的数据
CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);//解密数据
cs.FlushFinalBlock();
strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray());
return strRtn;
}
catch (Exception ex)
{
MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return null;
}
}
#endregion

Sysping1 2009-04-28
  • 打赏
  • 举报
回复
自己写,可逆公式: X -> KEY = Y
zhgroup 2009-04-28
  • 打赏
  • 举报
回复
异或最简单
柳晛 2009-04-28
  • 打赏
  • 举报
回复
想可逆就能可逆,凯撒、异或、DES等都是可逆的。
好像不可逆比可逆难,而且还要兼顾密文的唯一性。
sushou2009 2009-04-28
  • 打赏
  • 举报
回复
自己加入加密算法,加密算法有很多的你可以去网上搜一下~另外6L的朋友的写的不错~

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧