-
Notifications
You must be signed in to change notification settings - Fork 7
/
059 XOR decryption.sf
46 lines (33 loc) · 1.11 KB
/
059 XOR decryption.sf
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/ruby
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Website: https://github.com/trizen
# Your task has been made easy, as the encryption key consists of three lower case characters.
# Using cipher.txt (right click and 'Save Link/Target As...'), a file containing the encrypted
# ASCII codes, and the knowledge that the plain text must contain common English words, decrypt
# the message and find the sum of the ASCII values in the original text.
# https://projecteuler.net/problem=59
# Runtime: 0.369s
var enc = eval('[' + %f'p059_cipher.txt'.read + ']')
var p_len = 3 # password length
var pass = [] # decoded password
for p in (^p_len) {
for c in ('a'.ord .. 'z'.ord) {
var dec = []
for i in (p..enc.end `by` p_len) {
dec << chr(enc[i] ^ c)
}
# The number of non-alpha and non-space chars
var count = dec.join.count(/[^a-z ]/i)
# The ratio must be less than 10%
if (count/dec.len < 0.1) {
pass << c
break
}
}
}
var dec = []
for i in (^enc) {
dec << enc[i]^pass[i % p_len]
}
say dec.sum