Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.138.138.144] |
|
Сообщ.
#1
,
|
|
|
Добрых суток!
VMPC - Variably Modified Permutation Composition KSA - Key Scheduling Algorithm Коротко о проблеме! Нужно зашифровать некие данные (типы данных языка pure C == чистый СИ). Пытался разобраться с VMPC, нарыл с десяток ссылок (также несколько англоязычных) + вики. Итог: не понял даже и на 10% этот алгоритм! Сейчас решил, что главное перегнать с языка алгоритмического на СИ, и, если будет работать, то и хорошо. Перевести на 100% не смог, т к не понимаю нюансов. Вроде KSA закодил, а вот VMPC не получается. Кода получилось немного (пару функций по 10 строк кода). Помогите разобраться с VMPC. Сейчас тупо пытаюсь зашифровать строку, вводимую с клавиатуры. Программа отрабатывает/шифрует, но явно неправильно (как минимум длина исходной строки и зашифрованной не совпадает). Как вариант, помогите ссылкой, где разжеван каждый мм алгоритма VMPC (в идеале пример кода на любом языке, популярном языке)! Во многих статьях опущены нюансы и в итоге понять не получается. P.S. пока пытаюсь шифровать только массив символов (читай строку, т к на вход нужно подать последовательность байт, а sizeof(char) = 1). как кодировать целые типы - вообще не имею никакого понятия |
Сообщ.
#2
,
|
|
|
Здрасьте.
А в чем проблема то ? Поверхностное гугление никаких сложных вещей не выявило, щас даже набросаю как это примерно должно быть. собственно вот : /* * vmpc.c * * Created on: Sep 7, 2016 * Author: vegorov */ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef unsigned char byte; typedef struct{ byte P[256]; byte n,s; }vmpc_context; typedef struct{ byte* bytes; int len; }blob_t; //array of len bytes blob_t* blob_create(int size){ blob_t* ret = (blob_t*) malloc(sizeof(blob_t)); ret->bytes = (byte*) malloc(size); ret->len = size; return ret; } blob_t* blob_from_int(int value){ blob_t* ret = blob_create(sizeof(value)); memcpy(ret->bytes, (byte*)(&value), sizeof(value)); return ret; } blob_t* blob_from_short(short value){ blob_t* ret = blob_create(sizeof(value)); memcpy(ret->bytes, (byte*)(&value), sizeof(value)); return ret; } blob_t* blob_from_long(long long value){ blob_t* ret = blob_create(sizeof(value)); memcpy(ret->bytes, (byte*)(&value), sizeof(value)); return ret; } blob_t* blob_from_cstring(const char* value){ size_t string_len = strlen(value); blob_t* ret = blob_create(string_len); memcpy(ret->bytes, value, string_len); return ret; } #define SWAP(A,B)\ byte TEMP = A;\ A = B;\ B = TEMP; vmpc_context* KSARound(blob_t* blob, vmpc_context* context){ context->n = 0; int i = 0; int step = 0; for (step = 0; step < 768; step++){ context->s = context->P[(context->s + context->P[context->n] + blob->bytes[i])%256]; SWAP(context->P[context->n], context->P[context->s]); i = (i + 1) % blob->len; context->n = (context->n + 1) % 256; } return context; } //KSA3 //don't forget free memory of context after end of work vmpc_context* init_context(blob_t* crypto_key, blob_t* init_vector){ vmpc_context* context = (vmpc_context*) malloc(sizeof(vmpc_context)); int i; for (i = 0; i < 256; i++) context->P[i] = i; context->s = 0; context = KSARound(crypto_key, context); context = KSARound(init_vector, context); context = KSARound(crypto_key, context); return context; } byte vmpc_flow(vmpc_context* context){ context->s = context->P[(context->s + context->P[context->n]) % 256]; byte output = context->P[(context->P[context->P[context->s]] + 1) % 256]; SWAP(context->P[context->n], context->P[context->s]); context->n = (context->n + 1) %256; return output; } const char* KEY = "1234567890ABCDEF"; const char* INIT = "asdbaskf!@_#ddkn"; const char* MSG = "hello world"; void test(){ blob_t* key = blob_from_cstring(KEY); blob_t* init = blob_from_cstring(INIT); blob_t* plain_text = blob_from_cstring(MSG); blob_t* ciphered_text = blob_create(strlen(MSG)); size_t msg_len = strlen(MSG), i; //init context vmpc_context* context = init_context(key, init); //cipher for (i = 0; i < msg_len; i++){ ciphered_text->bytes[i] = plain_text->bytes[i] ^ vmpc_flow(context); } //print printf("plain text in hex:\n"); for (i = 0; i < msg_len; i++){ printf("%X ", plain_text->bytes[i]); } printf("\nciphered text:\n"); for (i = 0; i < msg_len; i++){ printf("%X ", ciphered_text->bytes[i]); } //reinit context free(context); context = init_context(key, init); //decipher for (i = 0; i < msg_len; i++){ ciphered_text->bytes[i] = ciphered_text->bytes[i] ^ vmpc_flow(context); } //print printf("\ndeciphered text:\n"); for (i = 0; i < msg_len; i++){ printf("%X ", ciphered_text->bytes[i]); } } int main(){ test(); return 0; } Добавлено у меня такой вывод: Скрытый текст plain text in hex: 68 65 6C 6C 6F 20 77 6F 72 6C 64 ciphered text: FA 46 59 AA 4F 80 8C 7C A6 B6 AB deciphered text: 68 65 6C 6C 6F 20 77 6F 72 6C 64 |
Сообщ.
#3
,
|
|
|
Я только память не чищу. Блобы удалять надо
|