TEA test files
This commit is contained in:
parent
223b3c81f8
commit
e1351ebd13
27
test/tea-decrypt.c
Normal file
27
test/tea-decrypt.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void decrypt(uint32_t *v, uint32_t *k) {
|
||||||
|
uint32_t v0 = v[0], v1 = v[1], sum = 0xC6EF3720, i; /* set up */
|
||||||
|
uint32_t delta = 0x9e3779b9; /* a key schedule constant */
|
||||||
|
uint32_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */
|
||||||
|
for (i = 0; i < 32; i++) { /* basic cycle start */
|
||||||
|
v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
|
||||||
|
v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
|
||||||
|
sum -= delta;
|
||||||
|
} /* end cycle */
|
||||||
|
v[0] = v0;
|
||||||
|
v[1] = v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc != 3) {
|
||||||
|
printf("Usage: %s text_to_decrypt key", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
decrypt((uint32_t*)argv[1], (uint32_t*)argv[2]);
|
||||||
|
printf("Result: %s", argv[1]);
|
||||||
|
return 0;
|
||||||
|
}
|
39
test/tea-encrypt.c
Normal file
39
test/tea-encrypt.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
void encrypt(uint32_t *v, uint32_t *k) {
|
||||||
|
uint32_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */
|
||||||
|
uint32_t delta = 0x9e3779b9; /* a key schedule constant */
|
||||||
|
uint32_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */
|
||||||
|
for (i = 0; i < 32; i++) { /* basic cycle start */
|
||||||
|
sum += delta;
|
||||||
|
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
|
||||||
|
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
|
||||||
|
} /* end cycle */
|
||||||
|
v[0] = v0;
|
||||||
|
v[1] = v1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
uint8_t * buf;
|
||||||
|
uint32_t buflen, i;
|
||||||
|
|
||||||
|
if (argc != 3) {
|
||||||
|
printf("Usage: %s text_to_encrypt key", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
buflen = strlen(argv[1]);
|
||||||
|
buf = (uint8_t *)malloc(buflen);
|
||||||
|
memcpy(buf, argv[1], buflen);
|
||||||
|
encrypt((uint32_t*)buf, (uint32_t *)argv[2]);
|
||||||
|
printf("Result:\n");
|
||||||
|
for (i = 0; i< buflen; i++) {
|
||||||
|
printf("%02x", buf[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user