-
Notifications
You must be signed in to change notification settings - Fork 534
/
SimpleEncoder.1sc
142 lines (132 loc) · 4.84 KB
/
SimpleEncoder.1sc
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
131
132
133
134
135
136
137
138
139
140
141
142
//------------------------------------------------
//--- 010 Editor Script File
//
// File: SimpleEncoder.1sc
// Authors: Didier Stevens
// Version: 1.0
// Purpose: Encode current selection by shifting characters. Supported encodings: ROT-5, ROT-13, ROT-47, Caesar and custom shifts. The complete file is encoded when nothing is selected.
// Category: Binary
// History:
// 1.0 2018-12-25 Didier Stevens: start
//
// Source code put in public domain by Didier Stevens, no Copyright
// https://DidierStevens.com
// Use at your own risk
//------------------------------------------------
#define TITLE "SimpleEncoder"
#define CODE_ROT5_ID 0
#define CODE_ROT5_STR "ROT-5"
#define CODE_ROT13_ID 1
#define CODE_ROT13_STR "ROT-13"
#define CODE_ROT47_ID 2
#define CODE_ROT47_STR "ROT-47"
#define CODE_CAESAR_ENCODE_ID 3
#define CODE_CAESAR_ENCODE_STR "Caesar (encode)"
#define CODE_CAESAR_DECODE_ID 4
#define CODE_CAESAR_DECODE_STR "Caesar (decode)"
#define CODE_CUSTOM_DIGIT_ID 5
#define CODE_CUSTOM_DIGIT_STR "Custom shift (characters: 0-9)"
#define CODE_CUSTOM_AZ_ID 6
#define CODE_CUSTOM_AZ_STR "Custom shift (characters: a-zA-Z)"
#define CODE_CUSTOM_PRT_ID 7
#define CODE_CUSTOM_PRT_STR "Custom shift (characters: !-~)"
int CalculateShift(ubyte bRead, ubyte bStart, ubyte bEnd, int bShift)
{
if (bRead >= bStart && bRead <= bEnd)
{
if (bShift < 0)
bShift = (bEnd - bStart + 1) + bShift;
return bStart + ((bRead - bStart + bShift) % (bEnd - bStart + 1)) - bRead;
}
else
return 0;
}
void Main(void)
{
int iIter, iStart, iSize;
int iChoice;
ubyte bRead;
int iShift;
int iShiftParameter;
// Check that a file is open
if(FileCount() == 0)
{
MessageBox(idOk, TITLE, TITLE + " can only be executed when a file is loaded.");
return;
}
// Initializes the variables
iSize = GetSelSize();
iStart = GetSelStart();
// Check that bytes were selected
if(iSize == 0)
{
iSize = FileSize();
iStart = 0;
}
iChoice = InputRadioButtonBox(TITLE, "Choose encoding:", 0, CODE_ROT5_STR, CODE_ROT13_STR, CODE_ROT47_STR, CODE_CAESAR_ENCODE_STR, CODE_CAESAR_DECODE_STR, CODE_CUSTOM_DIGIT_STR, CODE_CUSTOM_AZ_STR, CODE_CUSTOM_PRT_STR);
if (-1 == iChoice)
return;
if (iChoice >= CODE_CUSTOM_DIGIT_ID && iChoice <= CODE_CUSTOM_PRT_ID)
{
iShiftParameter = InputNumber(TITLE, "Choose shift value:", "");
if (BAD_VALUE == iShiftParameter || 0 == iShiftParameter)
{
MessageBox(idOk, TITLE, "Please provide a valid shift value.");
return;
}
}
// Modify the selection
for (iIter = 0; iIter < iSize; iIter++)
{
bRead = ReadUByte(iStart + iIter);
// Modify the current byte
switch (iChoice)
{
case CODE_ROT5_ID:
if (0 != iShift = CalculateShift(bRead, 48, 57, 5))
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_ROT13_ID:
iShift = CalculateShift(bRead, 65, 90, 13);
if (0 == iShift)
iShift = CalculateShift(bRead, 97, 122, 13);
if (0 != iShift)
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_ROT47_ID:
if (0 != iShift = CalculateShift(bRead, 33, 126, 47))
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_CAESAR_ENCODE_ID:
iShift = CalculateShift(bRead, 65, 90, 3);
if (0 == iShift)
iShift = CalculateShift(bRead, 97, 122, 3);
if (0 != iShift)
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_CAESAR_DECODE_ID:
iShift = CalculateShift(bRead, 65, 90, -3);
if (0 == iShift)
iShift = CalculateShift(bRead, 97, 122, -3);
if (0 != iShift)
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_CUSTOM_DIGIT_ID:
if (0 != iShift = CalculateShift(bRead, 48, 57, iShiftParameter))
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_CUSTOM_AZ_ID:
iShift = CalculateShift(bRead, 65, 90, iShiftParameter);
if (0 == iShift)
iShift = CalculateShift(bRead, 97, 122, iShiftParameter);
if (0 != iShift)
WriteUByte(iStart + iIter, bRead + iShift);
break;
case CODE_CUSTOM_PRT_ID:
if (0 != iShift = CalculateShift(bRead, 33, 126, iShiftParameter))
WriteUByte(iStart + iIter, bRead + iShift);
break;
}
}
}
Main();