Skip to content

Instantly share code, notes, and snippets.

@YoukaiCat
Last active April 14, 2016 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoukaiCat/1eaf2e9af7bb067cc70a7b01e701508f to your computer and use it in GitHub Desktop.
Save YoukaiCat/1eaf2e9af7bb067cc70a7b01e701508f to your computer and use it in GitHub Desktop.
#!/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
@YoukaiCat
Copy link
Author

m = 1000000, a = 10257, c = 644052
m = 1000000, a = 60257, c = 494052
m = 1000000, a = 110257, c = 344052
m = 1000000, a = 160257, c = 194052
m = 1000000, a = 210257, c = 44052
m = 1000000, a = 260257, c = 894052
m = 1000000, a = 310257, c = 744052
m = 1000000, a = 360257, c = 594052
m = 1000000, a = 410257, c = 444052
m = 1000000, a = 460257, c = 294052
m = 1000000, a = 510257, c = 144052
m = 1000000, a = 560257, c = 994052
m = 1000000, a = 610257, c = 844052
m = 1000000, a = 660257, c = 694052
m = 1000000, a = 710257, c = 544052
m = 1000000, a = 760257, c = 394052
m = 1000000, a = 810257, c = 244052
m = 1000000, a = 860257, c = 94052
m = 1000000, a = 910257, c = 944052
m = 1000000, a = 960257, c = 794052
20

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