solve Challenge 3 of Set 1

This commit is contained in:
Thomas Lindner 2022-02-06 21:31:00 +01:00
parent 19c48f9d58
commit f56c03bf65
2 changed files with 99 additions and 0 deletions

View file

@ -0,0 +1,27 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("challenge3_xor_cipher", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

View file

@ -0,0 +1,72 @@
const std = @import("std");
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
var inputbuffer = std.ArrayList(u8).init(allocator);
defer inputbuffer.deinit();
var resultbuffer = std.ArrayList(u8).init(allocator);
defer resultbuffer.deinit();
while (true) {
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
try resultbuffer.resize(inputbuffer.items.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) 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 stdout = std.io.getStdOut().writer();
while (true) {
const ciphertext = try readHex(allocator, 4096);
defer allocator.free(ciphertext);
var plaintext = try allocator.alloc(u8, ciphertext.len);
defer allocator.free(plaintext);
var bestkey: u8 = 0;
var bestscore: u32 = 0;
var key: u8 = 0;
while (true) {
decrypt(plaintext, ciphertext, key);
const s = score(plaintext);
if (s > bestscore) {
bestkey = key;
bestscore = s;
}
key +%= 1;
if (key == 0) {
break;
}
}
decrypt(plaintext, ciphertext, bestkey);
try stdout.print("key: {x}\n{s}\n", .{ bestkey, plaintext });
}
}