-
Notifications
You must be signed in to change notification settings - Fork 10
/
knapsack_cipher.rb
65 lines (56 loc) · 1.63 KB
/
knapsack_cipher.rb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'prime'
require './modular_arithmetic.rb'
class SuperKnapsack
attr_accessor :knapsack
def self.array_sum(arr)
arr.reduce (:+)
end
def initialize(arr)
arr.each.with_index do |a, i|
unless i==0
if (a <= self.class.array_sum(arr[0..i-1])) then
raise(ArgumentError, "not superincreasing at index #{i}")
end
end
@knapsack = arr
end
end
def primes?(m,n)
return Prime.prime?(m) && Prime.prime?(n)
end
def to_general(m, n)
argError = "arguments must both be prime" if (!primes?(m,n))
argError = "#{n} is smaller than superincreasing knapsack" if n <= @knapsack.last
raise(ArgumentError, argError) unless argError.nil?
@knapsack.map {|a| (a*m)%n }
end
end
class KnapsackCipher
# Default values of knapsacks, primes
M = 41
N = 491
DEF_SUPER = SuperKnapsack.new([2, 3, 7, 14, 30, 57, 120, 251])
DEF_GENERAL = DEF_SUPER.to_general(M, N)
# Encrypts plaintext
# Params:
# - plaintext: String object to be encrypted
# - generalknap: Array object containing general knapsack numbers
# Returns:
# - Array of encrypted numbers
def self.encrypt(plaintext, generalknap=DEF_GENERAL)
# TODO: implement this method
end
# Decrypts encrypted Array
# Params:
# - cipherarray: Array of encrypted numbers
# - superknap: SuperKnapsack object
# - m: prime number
# - n: prime number
# Returns:
# - String of plain text
def self.decrypt(cipherarray, superknap=DEF_SUPER, m=M, n=N)
raise(ArgumentError, "Argument should be a SuperKnapsack object"
) unless superknap.is_a? SuperKnapsack
# TODO: implement this method
end
end