cryptopals-zig/set1/challenge4_detect_xor/src/main.zig

74 lines
2.3 KiB
Zig

const std = @import("std");
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
var resultbuffer = std.ArrayList(u8).init(allocator);
defer resultbuffer.deinit();
while (true) {
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
const hex = line orelse return null;
defer allocator.free(hex);
try resultbuffer.resize(hex.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
std.log.err("not a valid hexadecimal", .{});
continue;
};
return resultbuffer.toOwnedSlice();
}
}
fn decrypt(plaintext: []u8, ciphertext: []const u8, key: u8) void {
std.debug.assert(plaintext.len == ciphertext.len);
var i: usize = 0;
while (i < ciphertext.len) : (i += 1) {
plaintext[i] = ciphertext[i] ^ key;
}
}
fn score(plaintext: []const u8) u32 {
const ranking = [_]u8{ 24, 7, 15, 17, 26, 11, 10, 19, 22, 4, 5, 16, 13, 21, 23, 8, 2, 18, 20, 25, 14, 6, 12, 3, 9, 1 };
var s: u32 = 0;
for (plaintext) |char| {
if (char >= 'a' and char <= 'z') {
s += ranking[char - 'a'];
}
}
return s;
}
pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
var bestplaintext = std.ArrayList(u8).init(allocator);
defer bestplaintext.deinit();
var bestscore: u32 = 0;
while (true) {
const ciphertext = (try readHex(allocator, stdin, 4096)) orelse break;
defer allocator.free(ciphertext);
var plaintext = try allocator.alloc(u8, ciphertext.len);
defer allocator.free(plaintext);
var key: u8 = 0;
while (true) {
decrypt(plaintext, ciphertext, key);
const s = score(plaintext);
if (s > bestscore) {
try bestplaintext.resize(plaintext.len);
std.mem.copy(u8, bestplaintext.items, plaintext);
bestscore = s;
}
key +%= 1;
if (key == 0) {
break;
}
}
}
try stdout.print("{s}\n", .{bestplaintext.items});
}