Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System;
- using System.Text;
- class Program
- {
- static void Main()
- {
- String input = "data của anh nè";
- String key = "aaa";
- String encryptedStr = Program.Encrypt(input, key);
- String decryptedStr = Program.Decrypt(encryptedStr, key);
- Console.WriteLine("Input string: {0} \nKey: {1}", input, key);
- Console.WriteLine("Encrypted: {0}\nDecrypted: {1}", encryptedStr, decryptedStr);
- }
- static string Encrypt(string input, string key){
- byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(input);
- byte[] encrypted = RC4(plainTextBytes, key);
- return System.Convert.ToBase64String(encrypted);
- }
- static string Decrypt(string input, string key){
- byte[] base64EncodedBytes = System.Convert.FromBase64String(input);
- byte[] bin = RC4(base64EncodedBytes, key);
- return System.Text.Encoding.UTF8.GetString(bin);
- }
- public static byte[] RC4(byte[] data, string pwdKey) {
- int a, i, j, k, tmp;
- int[] key, box;
- byte[] cipher;
- key = new int[256];
- box = new int[256];
- cipher = new byte[data.Length];
- byte[] pwd = System.Text.Encoding.UTF8.GetBytes(pwdKey);
- for (i = 0; i < 256; i++) {
- key[i] = pwd[i % pwd.Length];
- box[i] = i;
- }
- for (j = i = 0; i < 256; i++) {
- j = (j + box[i] + key[i]) % 256;
- tmp = box[i];
- box[i] = box[j];
- box[j] = tmp;
- }
- for (a = j = i = 0; i < data.Length; i++) {
- a++;
- a %= 256;
- j += box[a];
- j %= 256;
- tmp = box[a];
- box[a] = box[j];
- box[j] = tmp;
- k = box[((box[a] + box[j]) % 256)];
- cipher[i] = (byte)(data[i] ^ k);
- }
- return cipher;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment