2008年2月8日 星期五

AzDG可逆加密演算法 for C#

年節感冒,又逢下雨,閒著就再將AzDG可逆加密演算法轉成C#版
相較於Java版本,C#版本已含有MD5及Base64加解密函式
另外也改成靜態方法,不用建構即可使用
使用方式
AzDGCrypt.Crypt("中文測試", "私匙","字元集");
AzDGCrypt.Crypt("中文測試", "私匙"); //使用預設字元集Big5
AzDGCrypt.Crypt("中文測試"); //使用預設私匙及字元集Big5
//記得加入System.Web參考
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;

namespace jIAn.Crypt
{
public class AzDGCrypt
{
static string strCharSet = "big5";
static string strPrivateKey = "abcdefghijk";

public static byte[] PrivateKeyCrypt(byte[] arg_abteSource)
{
byte bteCRCLength = 0;
byte[] abteEncryptKey = Encoding.GetEncoding(strCharSet).GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(strPrivateKey, "MD5").ToLower());
byte[] abteReturn = new byte[arg_abteSource.Length];
for (int i = 0; i < arg_abteSource.Length; ++i)
{
bteCRCLength = (bteCRCLength > 31) ? (byte) 0 : bteCRCLength;
abteReturn[i] = (byte) (arg_abteSource[i] ^ abteEncryptKey[bteCRCLength++]);
}
return abteReturn;
}


public static byte[] DoEncrypt(byte[] arg_strSource)
{
DateTime dttCurrentTime = DateTime.Now;
long lngTimeInMillis = dttCurrentTime.Ticks;
byte bteCRCLength = 0;
byte[] abteCRCKey = Encoding.GetEncoding(strCharSet).GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(lngTimeInMillis.ToString(), "MD5").ToLower());
byte[] abteReturn = new byte[arg_strSource.Length * 2];
for (int i = 0, j = 0; i < arg_strSource.Length; ++i, ++j)
{
bteCRCLength = (bteCRCLength > 31) ? (byte) 0 : bteCRCLength;
abteReturn[j] = abteCRCKey[bteCRCLength];
++j;
abteReturn[j] = (byte)(arg_strSource[i] ^ abteCRCKey[bteCRCLength++]);
}
return PrivateKeyCrypt(abteReturn);
}


public static String Crypt(String arg_strSource)
{
return Convert.ToBase64String(DoEncrypt(Encoding.GetEncoding(strCharSet).GetBytes(arg_strSource)));
}


public static String Crypt(String arg_strSource, String arg_strPrivateKey)
{
strPrivateKey = arg_strPrivateKey;
return Crypt(arg_strSource);
}


public static String Crypt(String arg_strSource, String arg_strPrivateKey, String arg_strCharset)
{
strCharSet = arg_strCharset;
return Crypt(arg_strSource, arg_strPrivateKey);
}

public static byte[] DoDecrypt(byte[] arg_strSource)
{
arg_strSource = PrivateKeyCrypt(arg_strSource);
byte[] abteReturn = new byte[(int) (arg_strSource.Length / 2)];
for (int i = 0, j = 0; i < arg_strSource.Length; ++i, ++j)
{
abteReturn[j] = (byte) (arg_strSource[i] ^ arg_strSource[++i]);
}
return abteReturn;
}


public static String DeCrypt(String arg_strSource)
{
byte[] abteSourceText = Convert.FromBase64String(arg_strSource);
return Encoding.GetEncoding(strCharSet).GetString(DoDecrypt(abteSourceText));
}


public static String DeCrypt(String arg_strSource, String arg_strPrivateKey)
{
strPrivateKey = arg_strPrivateKey;
return DeCrypt(arg_strSource);
}

public static String DeCrypt(String arg_strSource, String arg_strPrivateKey, String arg_strCharset)
{
strCharSet = arg_strCharset;
return DeCrypt(arg_strSource, arg_strPrivateKey);
}
}
}

1 則留言:

匿名 提到...

這段code有程式上bug:
for (int i = 0, j = 0; i < arg_strSource.Length; ++i, ++j)
{
abteReturn[j] = (byte) (arg_strSource[i] ^ arg_strSource[++i]);
}

++i在最後一個item下,會超出陣例範圍。