-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (114 loc) · 3.85 KB
/
Program.cs
File metadata and controls
123 lines (114 loc) · 3.85 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
namespace XorFileExport
{
class Program
{
static byte[] rawData;
static byte[] encryptedData;
static byte[] decryptedData;
static byte[] key;
static void XorKeygen(int length)
{
Random Rnd = new Random();
for (int i = 0; i < length; i++)
key[i] = (byte)Rnd.Next(0, 255);
}
static bool XorData(string filename)
{
try
{
rawData = File.ReadAllBytes(filename);
encryptedData = rawData;
for (int i = 0; i < rawData.Length; i++)
{
encryptedData[i] ^= key[i % key.Length];
}
return true;
}
catch(Exception e)
{
Console.WriteLine("[-]\tError: {0}", e.Message);
return false;
}
}
static bool ExportFiles()
{
try
{
using (StreamWriter streamData = new StreamWriter("encryptedData.c"))
{
StringBuilder data = new StringBuilder();
data.AppendFormat("unsigned char encryptedData[{0}] = {{\n", encryptedData.Length);
for (int i=0;i < encryptedData.Length; i++)
{
data.AppendFormat("0x{0:x}, ", encryptedData[i]);
if ((i % 16) == 0 && i != 0) data.Append("\n");
}
data.Remove(data.Length - 2, 1);
data.Append("};");
streamData.Write(data);
streamData.Close();
}
using (StreamWriter streamKey = new StreamWriter("key.c"))
{
StringBuilder keyB = new StringBuilder();
keyB.AppendFormat("unsigned char key[{0}] = {{\n", key.Length);
for (int i = 0; i < key.Length; i++)
{
keyB.AppendFormat("0x{0:x}, ", key[i]);
if ((i % 16) == 0 && i != 0) keyB.Append("\n");
}
keyB.Remove(keyB.Length - 2, 1);
keyB.Append("};");
streamKey.Write(keyB);
streamKey.Close();
}
return true;
}
catch (Exception e)
{
Console.WriteLine("[-]\tError: {0}", e.Message);
return false;
}
}
static void Main(string[] args)
{
#if DEBUG
String file = "pupyx86.dll";
int keySize = 10;
#else
if (args.Count() < 2)
{
Console.WriteLine("[-]\tError: specify inputfile and key size");
return;
}
String file = args[0];
int keySize = Convert.ToInt32(args[1]);
#endif
key = new byte[keySize];
try
{
XorKeygen(key.Length);
if (XorData(file))
{
Console.WriteLine("[+]\tEncryption done");
if (ExportFiles())
Console.WriteLine("[+]\tFiles written");
}
return;
}
catch (Exception e)
{
Console.WriteLine("[-]\tError: {0}", e.Message);
return;
}
}
}
}