Add rspec and fix a bug in XOR in the process

This commit is contained in:
Wei Chen 2018-04-12 00:58:19 -05:00
parent 1247158968
commit 11001d010c
4 changed files with 116 additions and 2 deletions

View File

@ -18,8 +18,7 @@ module Rex
value.each_byte do |byte|
xor_byte = byte ^ xor_key
xor_byte = [xor_byte].pack('c').first
buf << xor_byte
buf << [xor_byte].pack('c')
end
buf

View File

@ -0,0 +1,55 @@
require 'spec_helper'
require 'securerandom'
require 'openssl'
describe Rex::Text do
let(:iv) {
SecureRandom.random_bytes(16)
}
let(:key) {
SecureRandom.random_bytes(32)
}
let(:value) {
'Hello World'
}
describe '#encrypt_aes256' do
it 'raises a CipherError exception due to a short IV' do
iv = SecureRandom.random_bytes(1)
expect { Rex::Text.encrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
end
it 'raises a CipherError exception due to a short key' do
key = SecureRandom.random_bytes(1)
expect { Rex::Text.encrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
end
it 'encrypts the string Hello World' do
encrypted_str = Rex::Text.encrypt_aes256(iv, key, value)
expect(encrypted_str).not_to eq(value)
end
end
describe '#decrypt_aes256' do
it 'raises a CipherError exception due to a short IV' do
iv = SecureRandom.random_bytes(1)
expect { Rex::Text.decrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
end
it 'raises a CipherError exception due to a short key' do
key = SecureRandom.random_bytes(1)
expect { Rex::Text.decrypt_aes256(iv, key, value) }.to raise_error(OpenSSL::Cipher::CipherError)
end
it 'decrypts the value to Hello World' do
encrypted_str = Rex::Text.encrypt_aes256(iv, key, value)
decrypted_str = Rex::Text.decrypt_aes256(iv, key, encrypted_str)
expect(decrypted_str).to eq(value)
end
end
end

28
spec/rex/text/rc4_spec.rb Normal file
View File

@ -0,0 +1,28 @@
require 'spec_helper'
require 'securerandom'
describe Rex::Text do
describe '#rc4' do
let(:key) {
SecureRandom.random_bytes(32)
}
let(:value) {
'Hello World'
}
it 'encrypts a string' do
expect(Rex::Text.rc4(key, value)).not_to eq(value)
end
it 'decrypts a string' do
encrypted_str = Rex::Text.rc4(key, value)
decrypted_str = Rex::Text.rc4(key, encrypted_str)
expect(decrypted_str).to eq(value)
end
end
end

32
spec/rex/text/xor_spec.rb Normal file
View File

@ -0,0 +1,32 @@
require 'spec_helper'
describe Rex::Text do
describe '#xor' do
let(:hello_world_str) {
'hello world'
}
let(:xor_hello_world_str) {
"\x67\x6a\x63\x63\x60\x2f\x78\x60\x7d\x63\x6b"
}
it 'XORs with an integer type key' do
xor_key = 0x0f
expect(Rex::Text.xor(xor_key, hello_world_str)).to eq(xor_hello_world_str)
end
it 'XORs with a string type key' do
xor_key = "0x0f"
expect(Rex::Text.xor(xor_key, hello_world_str)).to eq(xor_hello_world_str)
end
it 'raises an ArgumentError due to an out of range key' do
bad_key = 0x1024
expect { Rex::Text.xor(bad_key, hello_world_str) }.to raise_error(ArgumentError)
end
end
end