crypto :: SingleByteXorCipher :: find_key
Tries to find key using frequency analysis on all possible plaintexts.crypto :: SingleByteXorCipher :: key
Cryptographic key used in encryption and decryption.crypto :: SingleByteXorCipher :: key=
Cryptographic key used in encryption and decryption.crypto $ SingleByteXorCipher :: SELF
Type of this instance, automatically specialized in every classcrypto $ SingleByteXorCipher :: decrypt
Decrypt ciphertext and populateself.plaintext
crypto $ SingleByteXorCipher :: encrypt
Encrypt plaintext and populateself.ciphertext
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
crypto :: Cipher :: defaultinit
core :: Object :: defaultinit
crypto :: SingleByteXorCipher :: find_key
Tries to find key using frequency analysis on all possible plaintexts.core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
crypto :: SingleByteXorCipher :: key
Cryptographic key used in encryption and decryption.crypto :: SingleByteXorCipher :: key=
Cryptographic key used in encryption and decryption.core :: Object :: output_class_name
Display class name on stdout (debug only).
# Simple XOR cipher where the whole plaintext is XORed with a single byte.
class SingleByteXorCipher
super Cipher
# Cryptographic key used in encryption and decryption.
var key: Int = 0
redef fun encrypt do
var key_bytes = new Bytes.with_capacity(1)
key_bytes.add(key)
ciphertext = plaintext.xorcipher(key_bytes)
end
redef fun decrypt do
var key_bytes = new Bytes.with_capacity(1)
key_bytes.add(key)
plaintext = ciphertext.xorcipher(key_bytes)
end
end
lib/crypto/xor_ciphers.nit:67,1--85,3
redef class SingleByteXorCipher
# Tries to find key using frequency analysis on all possible plaintexts.
# Populates `self.key`
fun find_key do
# Accumulate best result
var max = 0.0
var best = 0
# Iterate on possible values for a byte
var xor_b = new Bytes.with_capacity(1)
for b in [0 .. 255] do
# Need `Bytes` to pass to xor
xor_b[0] = b
# Xor and evaluate result
var xored = ciphertext.xorcipher(xor_b)
var result = xored.to_s.english_scoring
if result > max then
max = result
best = b
end
end
self.key = best
end
end
lib/crapto/xor.nit:22,1--49,3