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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

$key = "E4HD9h4DhS23DYfhHemkS3Nf";// 24 bit Key sample
//$iv = "fYfhHeDm";// 8 bit IV
$iv = base64_decode("5Fl6lc4xjwA=");
$input = $_POST["a"];  //"Text to encrypt";// text to encrypt
//echo "text " .  $input . " comand: ". $_POST["e"];
$bit_check=8;// bit amount for diff algor.

if($_POST["e"]=="e"){
	$str= encrypt($input,$key,$iv,$bit_check);
	//echo "encripted: " . $str;
	echo $str;
}else{
	$str= decrypt($input,$key,$iv,$bit_check);
	//echo "decripted: " . $str;
	echo $str;
}
//echo "Start: $input - Excrypted: $str - Decrypted: ".decrypt($str,$key,$iv,$bit_check);


function encrypt($text,$key,$iv,$bit_check) {
	$text_num =str_split($text,$bit_check);
	$text_num = $bit_check-strlen($text_num[count($text_num)-1]);
	
	for ($i=0;$i<$text_num; $i++) {
		$text = $text . chr($text_num);		
	}
	$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
	mcrypt_generic_init($cipher, $key, $iv);
	$decrypted = mcrypt_generic($cipher,$text);
	mcrypt_generic_deinit($cipher);
	return base64_encode($decrypted);
}

function decrypt($encrypted_text,$key,$iv,$bit_check){
	$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
	mcrypt_generic_init($cipher, $key, $iv);
	$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted_text));
	mcrypt_generic_deinit($cipher);
	$last_char=substr($decrypted,-1);
	for($i=0;$i<$bit_check-1; $i++){
	    if(chr($i)==$last_char){ 
	        $decrypted=substr($decrypted,0,strlen($decrypted)-$i);
	        break;
	    }
	}
	return $decrypted;
}



private void button1_Click(object sender, EventArgs e)
        {
            //encript
            phpCrypt("e");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //dencript
            phpCrypt("d");
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //encript
            richTextBox1.Text = Encrypt(richTextBox1.Text, "E4HD9h4DhS23DYfhHemkS3Nf", true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //decript
            richTextBox1.Text = Decrypt(richTextBox1.Text, "E4HD9h4DhS23DYfhHemkS3Nf", true);
        }




        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            tdes.Key = keyArray;
            tdes.IV = Convert.FromBase64String("5Fl6lc4xjwA=");
            tdes.Mode = CipherMode.CBC;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }


        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.IV = Convert.FromBase64String("5Fl6lc4xjwA=");

            tdes.Mode = CipherMode.CBC;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }