one_time_pad 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.envrc +2 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +156 -0
- data/Rakefile +8 -0
- data/lib/one_time_pad.rb +130 -0
- data/sig/one_time_pad.rbs +4 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 33f288597326f8bac1f7f4d51e5dd803fe94594937cecbdb0259ae3c18ea5c00
|
4
|
+
data.tar.gz: 3b6108b127b784808f8f489a32cdf1302a8e77da31591cb0b8782df8cef12100
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2427e8f2a849086917a66a9d877d2110c387d41ccca36449760e96fbabe2d739d4e4873911c229e05176e968ccc9be9f4dd461bb1a6791b5b5a3b48f4e72cc76
|
7
|
+
data.tar.gz: 747a960a9233d04927a1fbba00d7094454d45153b3b707eea5a678f07253195bb959421ad0186c229a79d3c133ad4a0cfe12a342b98117eebfd931074d973e2b
|
data/.envrc
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Dewayne VanHoozer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
# OneTimePad
|
2
|
+
|
3
|
+
OneTimePad is a Ruby gem that provides a casually secure implementation of the One-Time Pad encryption technique. It uses multiple substitution ciphers and is suitable for educational and practical use.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'one_time_pad'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ gem install one_time_pad
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Here's a basic example of how to use the OneTimePad class:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'one_time_pad'
|
31
|
+
|
32
|
+
# Create a new OneTimePad instance
|
33
|
+
otp = OneTimePad.new
|
34
|
+
|
35
|
+
# Set a secret (optional, defaults to current time if not set)
|
36
|
+
otp.secret = Time.now
|
37
|
+
|
38
|
+
# Generate a one-time pad
|
39
|
+
pad = otp.generate_otp
|
40
|
+
|
41
|
+
# Encode a message
|
42
|
+
encoded_message = otp.code(message: "Hello, World!")
|
43
|
+
|
44
|
+
# Decode the message
|
45
|
+
decoded_message = otp.decode(message: encoded_message)
|
46
|
+
|
47
|
+
puts decoded_message # Output: Hello, World!
|
48
|
+
```
|
49
|
+
|
50
|
+
You can also initialize the OneTimePad with a secret or an existing pad:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# Initialize with a secret
|
54
|
+
otp = OneTimePad.new(secret: Time.now)
|
55
|
+
|
56
|
+
# Initialize with an existing pad
|
57
|
+
existing_pad = Array.new(2048) { (32..126).to_a.shuffle }
|
58
|
+
otp = OneTimePad.new(otp: existing_pad)
|
59
|
+
```
|
60
|
+
|
61
|
+
Example using a custom number of rows:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# Create a new OneTimePad instance
|
65
|
+
otp = OneTimePad.new
|
66
|
+
|
67
|
+
# Set the number of rows to 5
|
68
|
+
otp.rows = 5
|
69
|
+
|
70
|
+
# Generate a one-time pad
|
71
|
+
pad = otp.generate_otp
|
72
|
+
|
73
|
+
# Encode a message
|
74
|
+
encoded_message = otp.code(message: "This is a test message.")
|
75
|
+
|
76
|
+
# Decode the message
|
77
|
+
decoded_message = otp.decode(message: encoded_message)
|
78
|
+
|
79
|
+
puts decoded_message # Output: This is a test message.
|
80
|
+
```
|
81
|
+
|
82
|
+
## Features
|
83
|
+
|
84
|
+
- Generate a one-time pad based on a secret or current time
|
85
|
+
- Initialize with an existing pad
|
86
|
+
- Encode messages using ASCII characters (32-126)
|
87
|
+
- Decode messages using the same pad
|
88
|
+
- Handles non-ASCII characters by replacing them with underscores
|
89
|
+
|
90
|
+
## What is a Pad?
|
91
|
+
|
92
|
+
Its a matrix. The columns are character values. A row is a full collection of the ASCII characters (32..129) You index a row using the value of the plaintext character minus 32. `row[0]['x'.ord - 32` to get the encoded value for 'x' Each row is used for encoding one character. The next character to be incoded will be at the current row index + 1 with a module on the size of the Pad.
|
93
|
+
|
94
|
+
If we had a Pad size of 3 rows and wanted to encode the message 'xox' The two 'x' characters would have the same encrypted value from row 0. If the number of rows in the Pad is greater than the number of characters in your message then it is highly unlikely that the plaintext 'x' character would have the same encoded character since they never use the same row in the Pad.
|
95
|
+
|
96
|
+
## What is a Pad?
|
97
|
+
|
98
|
+
A Pad in this gem is a two-dimensional matrix used for encoding and decoding messages. It consists of multiple rows, each containing a shuffled set of ASCII characters (32-126).
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
# Structure:
|
102
|
+
pad = [
|
103
|
+
(32..126).to_a.shuffle, # Row 0
|
104
|
+
(32..126).to_a.shuffle, # Row 1
|
105
|
+
# ... more rows ...
|
106
|
+
]
|
107
|
+
```
|
108
|
+
|
109
|
+
Encoding process:
|
110
|
+
1. For each character in the plaintext:
|
111
|
+
a. Calculate the index: char.ord - 32
|
112
|
+
b. Use this index to look up the encoded value in the current row
|
113
|
+
c. Move to the next row (wrapping around if necessary)
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
# Example:
|
117
|
+
# To encode 'x' using row 0:
|
118
|
+
encoded_x = pad[0]['x'.ord - 32]
|
119
|
+
```
|
120
|
+
|
121
|
+
The Pad's size affects security:
|
122
|
+
- Larger Pads (more rows) reduce the chance of reusing rows for long messages
|
123
|
+
- A Pad with more rows than the message length ensures each character uses a unique substitution
|
124
|
+
|
125
|
+
Here is what happens when you have too few rows.
|
126
|
+
|
127
|
+
Example:
|
128
|
+
Encoding 'xox' with a 2-row Pad:
|
129
|
+
- 'x' uses row 0
|
130
|
+
- 'o' uses row 1
|
131
|
+
- 'x' uses row 0 (same as the first 'x')
|
132
|
+
- 'o' uses row 1 (same as the first 'o')
|
133
|
+
|
134
|
+
Now your secret hugs and kisses may be exposed.
|
135
|
+
|
136
|
+
The more rows approach significantly increases the difficulty of breaking the cipher through frequency analysis or other common cryptanalysis techniques.
|
137
|
+
|
138
|
+
## Background
|
139
|
+
|
140
|
+
The history of ciphers dates back thousands of years, with ancient examples such as the Caesar cipher, which shifted letters in the alphabet to obscure messages. Substitution ciphers, like the Caesar cipher, replace each letter of the plaintext with another letter. Although simple and historically significant, these early ciphers were vulnerable to frequency analysis due to their fixed substitution rules.
|
141
|
+
|
142
|
+
One of the most secure methods introduced in the 20th century is the One-Time Pad (OTP). The One-Time Pad was first conceptualized by Frank Miller in 1882 and was later popularized by Gilbert Vernam in 1917. It is the only cryptographic method recognized as information-theoretically secure when implemented correctly. The security of the OTP arises from its use of a completely random key that is as long as the message and used only once. This ensures that even with infinite computational power, an eavesdropper cannot decipher the message without the key.
|
143
|
+
|
144
|
+
However, while the One-Time Pad is theoretically unbreakable, it's crucial to emphasize that complexity does not inherently equate to strength. Despite the robust principles behind the One-Time Pad, practical implementation issues such as key generation and distribution can expose vulnerabilities.
|
145
|
+
|
146
|
+
In this OneTimePad Ruby gem, we harness the Ruby MT1995 pseudo-random number generator, which can produce approximately 4.3 billion possible seeds. This level of randomness is significant, but it is important to note that the true security of the implementation relies on the utilization of an unknown number of unique pads. We use a default pad size of 2048 rows, each containing shuffled ASCII codes from 32 to 126. This provides a significant amount of randomness for practical use. Theoritically a plain text message could have 2048 "X" characters which result in 2048 unique encoded characters.
|
147
|
+
|
148
|
+
Each plaintext is run through different pads, adding layers of complexity. However, a determined adversary with sufficient resources and expertise might still find ways to break the cipher quickly.
|
149
|
+
|
150
|
+
This class aims to take advantage of these principles for educational and practical use, illustrating the foundational concepts of modern cryptography and the importance of careful implementation.
|
151
|
+
|
152
|
+
## License
|
153
|
+
|
154
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
155
|
+
|
156
|
+
|
data/Rakefile
ADDED
data/lib/one_time_pad.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# lib/one_time_pad.rb
|
3
|
+
|
4
|
+
# Implements a One-Time Pad (OTP) encryption system
|
5
|
+
# This class provides methods to generate and use a one-time pad for
|
6
|
+
# message encryption and decryption using ASCII characters (32-126)
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# otp = OneTimePad.new
|
10
|
+
# otp.secret = Time.now
|
11
|
+
# pad = otp.generate_otp
|
12
|
+
# encoded = otp.code(message: "Hello")
|
13
|
+
# decoded = otp.decode(message: encoded)
|
14
|
+
class OneTimePad
|
15
|
+
VERSION = '1.0.0'
|
16
|
+
MAX_ROWS = 2048
|
17
|
+
|
18
|
+
attr_accessor :pad, :secret, :rows
|
19
|
+
|
20
|
+
# Initialize a new OneTimePad instance
|
21
|
+
#
|
22
|
+
# @param secret [Time, nil] Optional time value used for pad generation
|
23
|
+
# @param otp [Array<Array<Integer>>, nil] Optional existing pad to use
|
24
|
+
# @return [OneTimePad] New instance
|
25
|
+
def initialize(
|
26
|
+
secret: nil,
|
27
|
+
otp: nil
|
28
|
+
)
|
29
|
+
|
30
|
+
@secret = secret
|
31
|
+
@rows = MAX_ROWS
|
32
|
+
|
33
|
+
if otp
|
34
|
+
@pad = otp
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Generates a random seed based on provided UTC time or current time
|
39
|
+
#
|
40
|
+
# @return [Integer] A random seed value based on time components
|
41
|
+
def kaos
|
42
|
+
utc = secret.nil? ? Time.now.utc : secret
|
43
|
+
utc.year + utc.month + utc.day +
|
44
|
+
utc.hour + utc.min + utc.sec +
|
45
|
+
utc.usec
|
46
|
+
end
|
47
|
+
|
48
|
+
# Generates a pad of shuffled ASCII codes (32-126)
|
49
|
+
#
|
50
|
+
# @return [Array<Array<Integer>>] A 2D array of @rows,
|
51
|
+
# each containing shuffled ASCII codes from 32-126
|
52
|
+
def generate_otp
|
53
|
+
srand(kaos) # seed the random generator
|
54
|
+
ascii_codes = (32..126).to_a
|
55
|
+
row_size = ascii_codes.size
|
56
|
+
@pad = Array.new(@rows) { ascii_codes.shuffle }
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
# Encodes a message using the current pad
|
61
|
+
#
|
62
|
+
# @param message [String] The message to encode
|
63
|
+
# @return [String] The encoded message as a string of ASCII characters
|
64
|
+
def code(message:)
|
65
|
+
ascii_string = message.encode('ASCII',
|
66
|
+
invalid: :replace,
|
67
|
+
undef: :replace,
|
68
|
+
replace: '_')
|
69
|
+
|
70
|
+
ascii_string.bytes.map.with_index do |byte, i|
|
71
|
+
row = @pad[i % @pad.size]
|
72
|
+
char_code = byte.clamp(32, 126)
|
73
|
+
row[char_code - 32]
|
74
|
+
end.map(&:chr).join
|
75
|
+
end
|
76
|
+
|
77
|
+
# Decodes a message using the current pad
|
78
|
+
#
|
79
|
+
# @param message [String, Array<Integer>] The encoded message
|
80
|
+
# @return [String] The decoded message, with invalid characters
|
81
|
+
# replaced by underscores
|
82
|
+
def decode(message:)
|
83
|
+
encoded = message.is_a?(String) ? message.bytes : message
|
84
|
+
encoded.map.with_index do |code, i|
|
85
|
+
row = @pad[i % @pad.size]
|
86
|
+
char_index = row.index(code)
|
87
|
+
if char_index.nil?
|
88
|
+
'_'
|
89
|
+
else
|
90
|
+
(char_index + 32).chr
|
91
|
+
end
|
92
|
+
end.join
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Main Line
|
97
|
+
if __FILE__ == $PROGRAM_NAME
|
98
|
+
|
99
|
+
original_message = <<~MESSAGE
|
100
|
+
The British are comming!
|
101
|
+
The British are comming!
|
102
|
+
The British are comming!
|
103
|
+
The British are comming!
|
104
|
+
They are so pretty in their red coats; however,
|
105
|
+
its our job as minute men to shoot them
|
106
|
+
down. Don't fire until you see the whites of their eyes!
|
107
|
+
Grab your muskets and hide your women!
|
108
|
+
Grab your muskets and hide your women!
|
109
|
+
Grab your muskets and hide your women!
|
110
|
+
Grab your muskets and hide your women!
|
111
|
+
The British are comming!
|
112
|
+
The British are comming!
|
113
|
+
MESSAGE
|
114
|
+
|
115
|
+
otp = OneTimePad.new
|
116
|
+
otp.secret = Time.now
|
117
|
+
my_code = otp.generate_otp
|
118
|
+
secret = otp.code(message: original_message)
|
119
|
+
|
120
|
+
puts
|
121
|
+
puts secret
|
122
|
+
puts "="*65
|
123
|
+
puts otp.decode(message: secret)
|
124
|
+
puts "="*65
|
125
|
+
puts "== Different Instance with otp passed in ..."
|
126
|
+
puts
|
127
|
+
another = OneTimePad.new(otp: my_code)
|
128
|
+
puts another.decode(message: secret)
|
129
|
+
puts
|
130
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: one_time_pad
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
original_platform: ''
|
7
|
+
authors:
|
8
|
+
- Dewayne VanHoozer
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: "This gem provides a casually secure (you will never beat \nthe pros)
|
14
|
+
implementation of the One-Time Pad \nencryption technique, using multiple substitution
|
15
|
+
\ncyphers suitable for educational and practical use.\nKnowing the code, you still
|
16
|
+
have approximately\n4.3 billion randon seeds to choose from to decode an\nunknown
|
17
|
+
message.\n"
|
18
|
+
email:
|
19
|
+
- dvanhoozer@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- ".envrc"
|
25
|
+
- CHANGELOG.md
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/one_time_pad.rb
|
30
|
+
- sig/one_time_pad.rbs
|
31
|
+
homepage: https://github.com/MadBomber/one_time_pad
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
allowed_push_host: https://rubygems.org
|
36
|
+
homepage_uri: https://github.com/MadBomber/one_time_pad
|
37
|
+
source_code_uri: https://github.com/MadBomber/one_time_pad
|
38
|
+
changelog_uri: https://github.com/MadBomber/one_time_pad/blob/main/CHANGELOG.md
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.3'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.6.1
|
54
|
+
specification_version: 4
|
55
|
+
summary: A One-Time Pad encryption method for messages.
|
56
|
+
test_files: []
|