Int16 Char2Int(char value)
{
if(value>='0' && value<='9')
return (Int16)(value-'0');
return (Int16)(char.ToUpper(value) - 'A' + 10);
}
char Int2Char(Int16 value)
{
if (value < 10)
return (char)('0' + value);
return (char)('A' + value - 10);
}
public string Encode(string plain)
{
plain = "0"+plain.Replace("-", ""); // remove the '-'
StringBuilder sb = new StringBuilder();
for (int i = 0; i < plain.Length; i+=2)
{
char value = (char)((Char2Int(plain[i]) << 8) + Char2Int(plain[i + 1]));
Int16 v1 = (Int16)(value & 0xff);
Int16 v2 = (Int16)((value & 0xff00) >> 8);
sb.Append(value);
}
return sb.ToString();
}
public string Decode(string cipher)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in cipher)
{
Int16 v1 = (Int16)((ch & 0xff00) >> 8);
Int16 v2 = (Int16)(ch & 0xff);
sb.Append(Int2Char(v1));
sb.Append(Int2Char(v2));
}
return sb.ToString().Substring(1, 35);
}
private void button1_Click(object sender, EventArgs e)
{
string str = "FFF80-0EIBB-0CF0F-E6MFG-0A0BB-30221-22009";
string cipher = Encode(str);
string plain = Decode(cipher);
return;
}
|