Class: RCrypto::SimpleEncryptor
- Inherits:
-
Object
- Object
- RCrypto::SimpleEncryptor
- Defined in:
- lib/RCrypto.rb
Class Method Summary collapse
-
.caesar_decrypt(encrypted_text, shift) ⇒ Object
Decrypts text that was encrypted using the Caesar cipher.
-
.caesar_encrypt(text, shift) ⇒ Object
Encrypts the text using the Caesar cipher with the specified shift.
-
.frequency_analysis(text) ⇒ Object
Analyzes the frequency of characters in the text.
-
.generate_simple_key(length = 26) ⇒ Object
Generates a simple substitution cipher key by shuffling the alphabet.
-
.seems_encrypted?(text) ⇒ Boolean
Determines if the text appears to be encrypted based on several criteria.
-
.simple_hash(text) ⇒ Object
Generates a simple hash of the text.
-
.substitution_decrypt(encrypted_text, key) ⇒ Object
Decrypts text that was encrypted using a substitution cipher.
-
.substitution_encrypt(text, key) ⇒ Object
Encrypts the text using a substitution cipher with the provided key.
-
.xor_decrypt(encrypted_text, key) ⇒ Object
Decrypts text that was encrypted using XOR.
-
.xor_encrypt(text, key) ⇒ Object
Performs XOR encryption using the provided key.
Instance Method Summary collapse
-
#initialize(key) ⇒ SimpleEncryptor
constructor
A new instance of SimpleEncryptor.
Constructor Details
#initialize(key) ⇒ SimpleEncryptor
Returns a new instance of SimpleEncryptor.
40 41 42 43 |
# File 'lib/RCrypto.rb', line 40 def initialize(key) @key = key @key = generate_simple_key(20) end |
Class Method Details
.caesar_decrypt(encrypted_text, shift) ⇒ Object
Decrypts text that was encrypted using the Caesar cipher
61 62 63 |
# File 'lib/RCrypto.rb', line 61 def self.caesar_decrypt(encrypted_text, shift) Decode.caesar_cipher_decrypt(encrypted_text, shift) end |
.caesar_encrypt(text, shift) ⇒ Object
Encrypts the text using the Caesar cipher with the specified shift
56 57 58 |
# File 'lib/RCrypto.rb', line 56 def self.caesar_encrypt(text, shift) Encode.caesar_cipher_encrypt(text, shift) end |
.frequency_analysis(text) ⇒ Object
Analyzes the frequency of characters in the text
94 95 96 |
# File 'lib/RCrypto.rb', line 94 def self.frequency_analysis(text) text.downcase.chars.group_by(&:itself).transform_values(&:count).sort_by { |_, v| -v }.to_h end |
.generate_simple_key(length = 26) ⇒ Object
Generates a simple substitution cipher key by shuffling the alphabet
66 67 68 69 |
# File 'lib/RCrypto.rb', line 66 def self.generate_simple_key(length = 26) alphabet = ('a'..'z').to_a.shuffle.join alphabet end |
.seems_encrypted?(text) ⇒ Boolean
Determines if the text appears to be encrypted based on several criteria
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/RCrypto.rb', line 99 def self.seems_encrypted?(text) return false if text.length < 20 # Short texts are unlikely to be encrypted # Calculate entropy frequency = frequency_analysis(text) entropy = frequency.values.sum { |count| -count * Math.log2(count.to_f / text.length) } normalized_entropy = entropy / Math.log2(text.length) # Calculate the ratio of non-alphabetic characters non_alpha_chars_ratio = text.count("^a-zA-Z").to_f / text.length # Analyze patterns - number of repeated sequences repeated_patterns = text.scan(/(.+)\1/).size # Consider the text encrypted if entropy is very high, # if there is a high ratio of non-alphabetic characters, # or if the number of repeated patterns is very low normalized_entropy > 0.8 || non_alpha_chars_ratio > 0.3 || repeated_patterns < 2 end |
.simple_hash(text) ⇒ Object
Generates a simple hash of the text
84 85 86 87 88 89 90 91 |
# File 'lib/RCrypto.rb', line 84 def self.simple_hash(text) hash = 0 text.each_byte do |byte| hash = ((hash << 5) - hash) + byte hash = hash & 0xFFFFFFFF end hash.to_s(16) end |
.substitution_decrypt(encrypted_text, key) ⇒ Object
Decrypts text that was encrypted using a substitution cipher
78 79 80 81 |
# File 'lib/RCrypto.rb', line 78 def self.substitution_decrypt(encrypted_text, key) alphabet = ('a'..'z').to_a.join encrypted_text.tr(key, alphabet) end |
.substitution_encrypt(text, key) ⇒ Object
Encrypts the text using a substitution cipher with the provided key
72 73 74 75 |
# File 'lib/RCrypto.rb', line 72 def self.substitution_encrypt(text, key) alphabet = ('a'..'z').to_a.join text.downcase.tr(alphabet, key) end |
.xor_decrypt(encrypted_text, key) ⇒ Object
Decrypts text that was encrypted using XOR
51 52 53 |
# File 'lib/RCrypto.rb', line 51 def self.xor_decrypt(encrypted_text, key) Decode.xor_decrypt(encrypted_text, key) end |
.xor_encrypt(text, key) ⇒ Object
Performs XOR encryption using the provided key
46 47 48 |
# File 'lib/RCrypto.rb', line 46 def self.xor_encrypt(text, key) Encode.xor_encrypt(text, key) end |