SHA-2 (이 경우 SHA-256) 해시 함수를 사용하여 암호화를 수행하는 작은 루틴을 만들었습니다. 이와 같이 256 비트 (32 바이트) 블록 크기와 임의의 키 길이를 가진 블록 암호입니다. 해시 함수를 대체하고 완전히 새로운 유형의 알고리즘을 얻을 수 있다는 것을 쉽게 알 수 있습니다.SHA-256 해시 함수를 기반으로하는 간단한 블록 암호
이 구성에서는 결함, 특히 결함이 있는지, 그리고이 유형의 알고리즘이 연구되었는지 여부를 알고 싶습니다.
#include <openssl/sha.h>
struct sha_crypt_state {
unsigned char digest[SHA256_DIGEST_LENGTH];
};
void sha_crypt_set_key(sha_crypt_state *state, unsigned char *key, int key_length)
{
SHA256(key, key_length, state->digest);
}
void sha_crypt(sha_crypt_state *state, unsigned char *block)
{
sha_crypt_state temp;
SHA256(state->digest, sizeof(state->digest), temp.digest);
memcpy(state->digest, temp.digest, sizeof(temp.digest));
for (int i = 0; i < sizeof(state->digest); ++i) {
block[i] ^= state->digest[i];
}
}
void sha_crypt_test()
{
const char *key = "secret";
// prepare a test block
char block[SHA256_DIGEST_LENGTH];
memset(block, 0, sizeof(block));
strcpy(block, "Hello, testing encryption!");
// test encrypt
sha_crypt_state state;
set_key(&state, (unsigned char *)key, strlen(key));
sha_crypt(&state, (unsigned char *)block);
// test decrypt
set_key(&state, (unsigned char *)key, strlen(key));
sha_crypt(&state, (unsigned char *)block);
}
이 질문은 http://crypto.stackexchange.com/에서 더 잘 질문해야합니다. 주된 대답은 "휠을 재발 명하지 마십시오." –
감사합니다. 나는 거기에 게시 할 것입니다. . –
크로스 사이트 dupe : http://crypto.stackexchange.com/questions/1703/a-simple-block-cipher-based-on-the-sha-256-hash-function – casperOne