affine 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +11 -0
- data/Rakefile +57 -0
- data/VERSION.yml +4 -0
- data/affine.gemspec +50 -0
- data/lib/affine.rb +5 -0
- data/lib/affine/cipher.rb +61 -0
- data/lib/affine/errors.rb +30 -0
- data/test/affine_test.rb +48 -0
- data/test/test_helper.rb +10 -0
- metadata +66 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bryce Kerley
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
= affine
|
2
|
+
|
3
|
+
Simple affine cipher
|
4
|
+
|
5
|
+
a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
6
|
+
r = rand(123235150) # (should be smaller then the modulus)
|
7
|
+
r == a.decipher(a.encipher r)
|
8
|
+
|
9
|
+
== Copyright
|
10
|
+
|
11
|
+
Copyright (c) 2009 Bryce Kerley. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "affine"
|
8
|
+
gem.summary = %Q{Simple affine cipher for Ruby}
|
9
|
+
gem.email = "bkerley@brycekerley.net"
|
10
|
+
gem.homepage = "http://github.com/bkerley/affine"
|
11
|
+
gem.authors = ["Bryce Kerley"]
|
12
|
+
|
13
|
+
gem.rubyforge_project = 'affine'
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION.yml')
|
46
|
+
config = YAML.load(File.read('VERSION.yml'))
|
47
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "affine #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
57
|
+
|
data/VERSION.yml
ADDED
data/affine.gemspec
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{affine}
|
5
|
+
s.version = "0.2.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Bryce Kerley"]
|
9
|
+
s.date = %q{2009-05-18}
|
10
|
+
s.email = %q{bkerley@brycekerley.net}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"affine.gemspec",
|
23
|
+
"lib/affine.rb",
|
24
|
+
"lib/affine/cipher.rb",
|
25
|
+
"lib/affine/errors.rb",
|
26
|
+
"test/affine_test.rb",
|
27
|
+
"test/test_helper.rb"
|
28
|
+
]
|
29
|
+
s.has_rdoc = true
|
30
|
+
s.homepage = %q{http://github.com/bkerley/affine}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubyforge_project = %q{affine}
|
34
|
+
s.rubygems_version = %q{1.3.1}
|
35
|
+
s.summary = %q{Simple affine cipher for Ruby}
|
36
|
+
s.test_files = [
|
37
|
+
"test/affine_test.rb",
|
38
|
+
"test/test_helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 2
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
data/lib/affine.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Affine
|
2
|
+
# The affine cipher for positive integers. This algorithm is only defined
|
3
|
+
# for positive integers, and many arguments have further restrictions.
|
4
|
+
#
|
5
|
+
# == Usage
|
6
|
+
#
|
7
|
+
# @a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
8
|
+
# ct = @a.encipher(14)
|
9
|
+
# 14 == @a.decipher(ct)
|
10
|
+
class Cipher
|
11
|
+
|
12
|
+
# Cipher objects are used both for decryption and encryption.
|
13
|
+
#
|
14
|
+
# == Arguments
|
15
|
+
# [+modulus+] specifies how many different plaintexts and ciphertexts
|
16
|
+
# are available.
|
17
|
+
# [+a_key+] multiplied against the plaintext. <b>Must be coprime with
|
18
|
+
# +modulus+.</b>
|
19
|
+
# [+b_key+] added to the multiplied plaintext. No restrictions, but
|
20
|
+
# it's modulus math, so making it larger than +modulus+ is
|
21
|
+
# useless.
|
22
|
+
def initialize(modulus, a_key, b_key)
|
23
|
+
raise CoprimeError.new(modulus, a_key) if modulus.gcd(a_key) != 1
|
24
|
+
@modulus = modulus
|
25
|
+
@a_key = a_key
|
26
|
+
@b_key = b_key
|
27
|
+
@a_inv = extended_gcd(a_key, modulus)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Encrypt one +plaintext+ into a +ciphertext+.
|
31
|
+
def encipher(plaintext)
|
32
|
+
raise RangeError.new(plaintext, @modulus) if plaintext > @modulus
|
33
|
+
((@a_key * plaintext) + @b_key) % @modulus
|
34
|
+
end
|
35
|
+
|
36
|
+
# Decrypt one +ciphertext+ into a +plaintext+.
|
37
|
+
def decipher(ciphertext)
|
38
|
+
raise RangeError.new(ciphertext, @modulus) if ciphertext > @modulus
|
39
|
+
(@a_inv * (ciphertext - @b_key)) % @modulus
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :encrypt, :encipher
|
43
|
+
alias_method :decrypt, :decipher
|
44
|
+
private
|
45
|
+
# from http://snippets.dzone.com/posts/show/6074
|
46
|
+
def extended_gcd(b,m,_recursion_depth=0)
|
47
|
+
if b % m == 0
|
48
|
+
temp = [0,1]
|
49
|
+
return temp
|
50
|
+
else
|
51
|
+
temp = extended_gcd(m, b % m, _recursion_depth+1)
|
52
|
+
temp2 = [temp[1], temp[0]-temp[1] * ((b/m).to_i)]
|
53
|
+
if _recursion_depth == 0
|
54
|
+
return temp2[0] % m
|
55
|
+
else
|
56
|
+
return temp2
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Affine
|
2
|
+
|
3
|
+
# Parent class for Affine cipher errors.
|
4
|
+
#
|
5
|
+
# This is a good kind of error to rescue when you're not
|
6
|
+
# sure about the key, modulus, plaintext, or ciphertext
|
7
|
+
# and want to fail cleanly.
|
8
|
+
class AffineError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
# Raised when two numbers that are supposed to be coprime
|
12
|
+
# (greatest common denominator of 1) are not.
|
13
|
+
#
|
14
|
+
# Keys and moduli from external sources might raise these.
|
15
|
+
class CoprimeError < AffineError
|
16
|
+
def initialize(a, b) #:nodoc:
|
17
|
+
super("Expected #{a} to be coprime with #{b}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Raised when an input number is larger than the modulus.
|
22
|
+
#
|
23
|
+
# Plaintexts or ciphertexts from external sources might
|
24
|
+
# raise these.
|
25
|
+
class RangeError < AffineError
|
26
|
+
def initialize(n, mod) #:nodoc:
|
27
|
+
super("Expected input #{n} to be smaller than modulus #{mod}")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/test/affine_test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AffineTest < Test::Unit::TestCase
|
4
|
+
context 'affine cipher 5,8 mod 26' do
|
5
|
+
setup do
|
6
|
+
@a = Affine::Cipher.new(26, 5, 8)
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'be created correctly' do
|
10
|
+
assert_nothing_raised do
|
11
|
+
Affine::Cipher.new(26, 5, 8)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'encipher ITSCOOL correctly' do
|
16
|
+
# example from http://en.wikipedia.org/wiki/Affine_cipher
|
17
|
+
plain = 'ITSCOOL'.split('').map{ |c| c[0] - 65}
|
18
|
+
coded = plain.map{ |c| @a.encipher c }
|
19
|
+
check ='WZUSAAL'.split('').map{ |c| c[0] - 65}
|
20
|
+
assert_equal check, coded
|
21
|
+
end
|
22
|
+
should 'decipiher WZUSAAL correctly' do
|
23
|
+
# example from http://en.wikipedia.org/wiki/Affine_cipher
|
24
|
+
check ='WZUSAAL'.split('').map{ |c| c[0] - 65}
|
25
|
+
coded = check.map{ |c| @a.decipher c }
|
26
|
+
plain = 'ITSCOOL'.split('').map{ |c| c[0] - 65}
|
27
|
+
assert_equal plain, coded
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'affine cipher 65182241782, 123235151 mod 2176782371' do
|
32
|
+
setup do
|
33
|
+
@a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
34
|
+
end
|
35
|
+
should 'get created correctly' do
|
36
|
+
assert_nothing_raised do
|
37
|
+
Affine::Cipher.new(2176782371, 65182241782, 123235151)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
should 'pass a few random identity checks' do
|
41
|
+
# I'm not doing this by hand
|
42
|
+
1000.times do
|
43
|
+
r = rand(123235150)
|
44
|
+
assert_equal r, @a.decipher(@a.encipher r)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: affine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryce Kerley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-18 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: bkerley@brycekerley.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION.yml
|
32
|
+
- affine.gemspec
|
33
|
+
- lib/affine.rb
|
34
|
+
- lib/affine/cipher.rb
|
35
|
+
- lib/affine/errors.rb
|
36
|
+
- test/affine_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/bkerley/affine
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: affine
|
60
|
+
rubygems_version: 1.3.1
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Simple affine cipher for Ruby
|
64
|
+
test_files:
|
65
|
+
- test/affine_test.rb
|
66
|
+
- test/test_helper.rb
|