File: foo.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (84 lines) | stat: -rw-r--r-- 2,458 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

using System.Security.Cryptography;
using System.Text;
using System;

public class foo {
		static byte ToHexValue (char c, bool high)
		{
			byte v;
			if (c >= '0' && c <= '9')
				v = (byte) (c - '0');
			else if (c >= 'a' && c <= 'f')
				v = (byte) (c - 'a' + 10);
			else if (c >= 'A' && c <= 'F')
				v = (byte) (c - 'A' + 10);
			else
				throw new ArgumentException ("Invalid hex character");

			if (high)
				v <<= 4;

			return v;
		}
		
		internal static byte [] GetBytes (string key, int len)
		{
			byte [] result = new byte [len / 2];
			for (int i = 0; i < len; i += 2)
				result [i / 2] = (byte) (ToHexValue (key [i], true) + ToHexValue (key [i + 1], false));

			return result;
		}


  static void decrypt () {
    string ans = "NVG3kJkWiFzKHh2UAEDiaY4kvYYUk+z4gEdlv2rZudA=";
    string key = "1E14FC86752772F5DB58B99764D0168106D336563D77CCBA";
    string salt = "xXsrnq4n1jebmRiC/Ty46g==";

    byte[] key_bytes = GetBytes (key, key.Length);
    byte[] ans_bytes = Convert.FromBase64String (ans);
    byte[] salt_bytes = Convert.FromBase64String (salt);

    Console.WriteLine ("encrypted = {0} long, salt = {1} long", ans_bytes.Length, salt_bytes.Length);

    SymmetricAlgorithm alg = Rijndael.Create ();

    ICryptoTransform decryptor = alg.CreateDecryptor (key_bytes, salt_bytes);

    byte[] rv = decryptor.TransformFinalBlock (ans_bytes, 0, ans_bytes.Length);

    Console.WriteLine ("decryption result = {0}", Convert.ToBase64String (rv));

    Console.WriteLine (Encoding.Unicode.GetString (rv));
  }

  static void encrypt () {
    string ans = "green";
    string key = "1E14FC86752772F5DB58B99764D0168106D336563D77CCBA";
    string salt = "xXsrnq4n1jebmRiC/Ty46g==";

    byte[] key_bytes = GetBytes (key, key.Length);
    byte[] ans_bytes = Encoding.Unicode.GetBytes (ans);
    byte[] salt_bytes = Convert.FromBase64String (salt);

    byte[] buf = new byte[ans_bytes.Length + salt_bytes.Length];

    /*Array.Copy (salt_bytes, 0, buf, 0, salt_bytes.Length);*/
    Array.Copy (ans_bytes, 0, buf, salt_bytes.Length, ans_bytes.Length);

    Console.WriteLine ("before encryption {0}", Convert.ToBase64String (buf));

    SymmetricAlgorithm alg = Rijndael.Create ();

    ICryptoTransform encryptor = alg.CreateEncryptor (key_bytes, salt_bytes);

    Console.WriteLine (Convert.ToBase64String (encryptor.TransformFinalBlock (buf, 0, buf.Length)));
  }

  public static void Main (string[] args) {
	decrypt();
	encrypt();
  }
}