Skip to content

Instantly share code, notes, and snippets.

@TeWu
Last active May 18, 2020 17:09
Show Gist options
  • Save TeWu/364f65a03ded93eccaaa69c78bf70bc1 to your computer and use it in GitHub Desktop.
Save TeWu/364f65a03ded93eccaaa69c78bf70bc1 to your computer and use it in GitHub Desktop.
Shift Ciphers Challenge
## Here values of secret_offset, secret_key1, secret_key2, plaintext_01, plaintext_02 and plaintext_03 are set
require 'shift_ciphers' # Library's code at: https://github.com/TeWu/shift-ciphers (version 1.0.1)
caesar = ShiftCiphers::Caesar.new(offset: secret_offset)
vigenere = ShiftCiphers::Vigenere.new(secret_key1)
strong_vigenere = ShiftCiphers::HardenedVigenere.new(secret_key2)
p caesar.encrypt(plaintext_01) # => "!M;BL;AtKw;MH;ytBEd;uNM;BM;BL;PHKLx;GxOxK;MH;AtOx;MKBxw;MH;LNvvxxwe;1_AxHwHKx;)HHLxOxEM"
p vigenere.encrypt(plaintext_02) # => "qit$b#D1R-js+ntrt'-7U-hy+bImv-Ehw-Hv/z9$T:l6&qty\".{Go{m1@p"
p strong_vigenere.encrypt(plaintext_03) # => "cc-9qPhWD>)CB;;cJ%+.q!ZmN(+9es9P!;+7;JqM$38'3}{ioQOyvxovZI9 >"
### The Challenge
## The challenge is to crack the ciphers and obtain values of plaintext_01, plaintext_02 and plaintext_03
@TeWu
Copy link
Author

TeWu commented Oct 19, 2018

@TeWu
Copy link
Author

TeWu commented May 17, 2020

Super naïve bruteforce:

require 'shift_ciphers'

N = 5
CIPHERTEXT = "KL*OR@wX;0tf )DYRh0g,0BLwwIR5P7GO;(FjQ8a[WtJ)liF*>Bf"


def for_all_keys(len, &block)
  return if len <= 0
  if len == 1
    ShiftCiphers::Alphabets::DEFAULT.chars.each do |c|
      yield c
    end
  else
    ShiftCiphers::Alphabets::DEFAULT.chars.each do |c|
      for_all_keys(len - 1) do |key|
        yield c + key
      end
    end
  end
end


prev = "";
for_all_keys(N) do |key|
  p key if prev[2] != key[2]
  ShiftCiphers::HardenedVigenere.decrypt(CIPHERTEXT, key)
  prev = key
end

puts "DONE"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment