Last active
April 14, 2016 18:52
-
-
Save YoukaiCat/1eaf2e9af7bb067cc70a7b01e701508f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: utf-8 | |
module LCG | |
def self.generate prev, a, c, m | |
(a * prev + c) % m | |
end | |
end | |
class LCGBruteForce | |
def initialize sequence, m | |
@sequence = sequence | |
@m = m | |
end | |
def crack | |
0.upto(@m).map do |a| | |
c = @sequence[1] - ((@sequence[0] * a) % @m) | |
c += @m if c < 0 | |
all = @sequence[1..-1].each_index.all? { |index| @sequence[index + 1] == LCG.generate(@sequence[index], a, c, @m) } | |
all ? {m: @m, a: a, c: c} : nil | |
end.compact! | |
end | |
end | |
numbers = File.readlines("/home/natsuo/Development/Current/Cypher/Second/Data/Task2/1").map(&:to_i) | |
alternatives = LCGBruteForce.new(numbers, 1_000_000).crack | |
alternatives.each { |h| puts "m = #{h[:m]}, a = #{h[:a]}, c = #{h[:c]}" } | |
puts alternatives.size |
Author
YoukaiCat
commented
Apr 14, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment