all repos — cryptopals-challenges @ d85d53bb13c5a97eb82467a884b52014a2110759

cryptopals-challenges

set1/src/char-freq-analyze.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	size_t i;
	int8_t ciphertext[] = "\x1b\x37\x37\x33\x31\x36\x3f\x78\x15\x1b\x7f\x2b"
	                      "\x78\x34\x31\x33\x3d\x78\x39\x78\x28\x37\x2d\x36"
	                      "\x3c\x78\x37\x3e\x78\x3a\x39\x3b\x37\x36";
	uint8_t byte_freq[256];
	memset(byte_freq, 0, sizeof(byte_freq));

	// get histogram (count byte occurences)
	for (i = 0; i < sizeof(ciphertext)-1; ++i) {
		byte_freq[ciphertext[i]] += 1;
	}

	for (i = 0; i < sizeof(byte_freq); ++i) {
		if (byte_freq[i] != 0) {
			printf("%lx occurs %d times\n", i, byte_freq[i]);
		}
	}

	return EXIT_SUCCESS;
}