From f56c03bf65dabf67ca35951e62e0f7b0929bf05c Mon Sep 17 00:00:00 2001 From: Thomas Lindner Date: Sun, 6 Feb 2022 21:31:00 +0100 Subject: [PATCH] solve Challenge 3 of Set 1 --- set1/challenge3_xor_cipher/build.zig | 27 ++++++++++ set1/challenge3_xor_cipher/src/main.zig | 72 +++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 set1/challenge3_xor_cipher/build.zig create mode 100644 set1/challenge3_xor_cipher/src/main.zig diff --git a/set1/challenge3_xor_cipher/build.zig b/set1/challenge3_xor_cipher/build.zig new file mode 100644 index 0000000..a79c717 --- /dev/null +++ b/set1/challenge3_xor_cipher/build.zig @@ -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); +} diff --git a/set1/challenge3_xor_cipher/src/main.zig b/set1/challenge3_xor_cipher/src/main.zig new file mode 100644 index 0000000..a9193a4 --- /dev/null +++ b/set1/challenge3_xor_cipher/src/main.zig @@ -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 }); + } +}