[C#] Hướng Dẫn Sử Dụng Thuật Toán Mã Hóa RC4 ...

Xin chào các bạn, hôm nay mình sẽ tiếp tục hướng dẫn các bạn sử dụng thuật toán mã hóa RC4 trong C#.

[C#] Tutorial RC4 algorithm encrypt and decrypt in csharp

Trong các bài viết trên lập trình VB.NET mình đã hướng dẫn các bạn rất nhiều loại mã hóa như: MD5, SHA1, DES, AES, RSA, ASCII,...

Thuật toán mã hóa RC4, là thuật toán mã hóa đối xứng, nghĩa là chúng ta có thể mã hóa và giải mã thông qua key bảo mật.

Dưới đây là hình ảnh demo của ứng dụng:

mã hóa RC4 Csharp

Source code C#:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace RC4_cypher_Csharp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string RC4(string input, string key) { StringBuilder result = new StringBuilder(); int x, y, j = 0; int[] box = new int[256]; for (int i = 0; i < 256; i++) { box[i] = i; } for (int i = 0; i < 256; i++) { j = (key[i % key.Length] + box[i] + j) % 256; x = box[i]; box[i] = box[j]; box[j] = x; } for (int i = 0; i < input.Length; i++) { y = i % 256; j = (box[y] + j) % 256; x = box[y]; box[y] = box[j]; box[j] = x; result.Append((char)(input[i] ^ box[(box[y] + box[j]) % 256])); } return result.ToString(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { txtEncrypt.Text = RC4(txtInput.Text, "123456"); } private void button2_Click(object sender, EventArgs e) { txtDecrypt.Text = RC4(txtEncrypt.Text, "123456"); } } }

HAVE FUN :)

Tags: thuật toán mã hóamã hóagiải mã

Từ khóa » Giải Thuật Mã Hóa Rc4