-
Notifications
You must be signed in to change notification settings - Fork 1
/
keycrypto.c
66 lines (52 loc) · 1.89 KB
/
keycrypto.c
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
/*--------------------------------------------------------------------*/
/* keycrypto.c */
/* Author: Gerry Wan */
/*--------------------------------------------------------------------*/
#include "keycrypto.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define KEYLEN 8 // bytes
#define INTBUFLEN (sizeof(int) * 8 + 1)
#define ARRBUFLEN (sizeof(unsigned char) * 64 + 1)
/*--------------------------------------------------------------------*/
void xor_encrypt(unsigned char *pucInput,
unsigned char *pucOutput,
unsigned int uiLength,
unsigned char *pucKey)
{
unsigned int i;
assert(pucInput != NULL);
assert(pucOutput != NULL);
assert(pucKey != NULL);
assert(uiLength % KEYLEN == 0);
memcpy(pucOutput, pucInput, uiLength);
for (i = 0; i < uiLength; i++)
pucOutput[i] ^= pucKey[i % KEYLEN];
}
/*--------------------------------------------------------------------*/
void xor_decrypt(unsigned char *pucInput,
unsigned char *pucOutput,
unsigned int uiLength,
unsigned char *pucKey)
{
// symmetric to encrypt
xor_encrypt(pucInput, pucOutput, uiLength, pucKey);
}
/*--------------------------------------------------------------------*/
void intToString(int i, char *pcBuf)
{
assert(pcBuf != NULL);
snprintf(pcBuf, INTBUFLEN, "%d", i);
}
/*--------------------------------------------------------------------*/
void arrToString(unsigned char *pucArr, char *pcBuf, int iLen)
{
int i;
assert(pucArr != NULL);
assert(pcBuf != NULL);
for (i = 0; i < iLen; i++)
sprintf(pcBuf + i*2, "%.2x", pucArr[i]);
pcBuf[iLen*2] = '\0';
}