- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[C#] Hướng dẫn sử dụng thuật toán mã hóa và giải mã Atom-128 algorithm
Trong các bài viết trước mình đã hướng dẫn các bạn sử dụng rất nhiều thuật toán mã hóa: Md5, RSA, DES, AES, Binary, SHA...
Bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn sử dụng một thuật toán mã hóa nữa là thuật toán mã hóa Atom-128 C#.
Mình sẽ chia sẽ hai hàm cho các bạn sử dụng:
1. Source code hàm Encode Atom-128
public static string Atom128Encode(string input)
{
string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC";
StringBuilder result = new StringBuilder();
int i = 0;
int[] indexes = new int[4];
int[] chars = new int[3];
do
{
chars[0] = i + 1 > input.Length ? 0 : (int)input[i++];
chars[1] = i + 2 > input.Length ? 0 : (int)input[i++];
chars[2] = i + 3 > input.Length ? 0 : (int)input[i++];
indexes[0] = chars[0] >> 2;
indexes[1] = ((chars[0] & 3) << 4) | (chars[1] >> 4);
indexes[2] = ((chars[1] & 15) << 2) | (chars[2] >> 6);
indexes[3] = chars[2] & 63;
if ((char)chars[1] == 0)
{
indexes[2] = 64;
indexes[3] = 64;
}
else if ((char)chars[2] == 0)
{
indexes[3] = 64;
}
foreach (int index in indexes)
{
result.Append(key[index]);
}
}
while (i < input.Length);
return result.ToString();
}
2. Hàm giải mã thuật toán Atom-128
public static string Atom128Decode(string input)
{
string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC";
StringBuilder result = new StringBuilder();
int[] indexes = new int[4];
int[] chars = new int[3];
int i = 0;
do
{
indexes[0] = key.IndexOf(input[i++]);
indexes[1] = key.IndexOf(input[i++]);
indexes[2] = key.IndexOf(input[i++]);
indexes[3] = key.IndexOf(input[i++]);
chars[0] = (indexes[0] << 2) | (indexes[1] >> 4);
chars[1] = (indexes[1] & 15) << 4 | (indexes[2] >> 2);
chars[2] = (indexes[2] & 3) << 6 | indexes[3];
result.Append((char)chars[0]);
if (indexes[2] != 64)
result.Append((char)chars[1]);
if (indexes[3] != 64)
result.Append((char)chars[2]);
}
while (i < input.Length);
return result.ToString();
}
Usage:
3. Cách sử dụng thuật toán
string encodedString = Atom128Encode("http://laptrinhvb.net");
string decodedString = Atom128Decode(encodedString);
HAVE FUN :)