-
-
Save anonymous/a8ade63cf22746e79dbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require "crypt/blowfish" | |
if RUBY_VERSION.to_f >= 1.9 | |
module ::Crypt | |
class Blowfish | |
def setup_blowfish | |
@sBoxes = Array.new(4) { |i| INITIALSBOXES[i].clone } | |
@pArray = INITIALPARRAY.clone | |
keypos = 0 | |
0.upto(17) { |i| | |
data = 0 | |
4.times { | |
data = ((data << 8) | @key[keypos].ord) % ULONG | |
keypos = (keypos.next) % @key.length | |
} | |
@pArray[i] = (@pArray[i] ^ data) % ULONG | |
} | |
l = 0 | |
r = 0 | |
0.step(17, 2) { |i| | |
l, r = encrypt_pair(l, r) | |
@pArray[i] = l | |
@pArray[i+1] = r | |
} | |
0.upto(3) { |i| | |
0.step(255, 2) { |j| | |
l, r = encrypt_pair(l, r) | |
@sBoxes[i][j] = l | |
@sBoxes[i][j+1] = r | |
} | |
} | |
end | |
end | |
end | |
end | |
module IRC | |
class BadInputError < StandardError; end | |
class MBase64 | |
B64 = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
def self.decode encoded | |
return nil if not encoded.length % 12 == 0 | |
decoded = String.new | |
k = -1 | |
while (k < encoded.length - 1) do | |
right = 0 | |
left = 0 | |
(0..5).each do |i| | |
k = k + 1 | |
right |= B64.index(encoded[k]) << (i * 6) | |
end | |
(0..5).each do |i| | |
k = k + 1 | |
left |= B64.index(encoded[k]) << (i * 6) | |
end | |
(0..3).each do |i| | |
decoded += ((left & (0xFF << ((3 - i) * 8))) >> ((3 - i) * 8)).chr | |
end | |
(0..3).each do |i| | |
decoded += ((right & (0xFF << ((3 - i) * 8))) >> ((3 - i) * 8)).chr | |
end | |
end | |
return decoded | |
end | |
def self.encode decoded | |
if not decoded.length % 8 == 0 | |
raise IRC::BadInputError, | |
"can only encode strings which are a multiple of 8 characters." | |
end | |
encoded = String.new | |
k = -1 | |
while (k < decoded.length - 1) do | |
k = k.next | |
left = (decoded[k].ord << 24) | |
k = k.next | |
left += (decoded[k].ord << 16) | |
k = k.next | |
left += (decoded[k].ord << 8) | |
k = k.next | |
left += decoded[k].ord | |
k = k.next | |
right = (decoded[k].ord << 24) | |
k = k.next | |
right += (decoded[k].ord << 16) | |
k = k.next | |
right += (decoded[k].ord << 8) | |
k = k.next | |
right += decoded[k].ord | |
(0..5).each do | |
encoded += B64[right & 0x3F].chr | |
right = right >> 6 | |
end | |
(0..5).each do | |
encoded += B64[left & 0x3F].chr | |
left = left >> 6 | |
end | |
end | |
return encoded | |
end | |
end | |
class FiSH | |
@blowfish = Crypt::Blowfish.new("some nice key here") | |
def encrypt text | |
text = pad(text, 8) | |
result = "" | |
num_block = text.length / 8 | |
num_block.times do |n| | |
block = text[n*8..(n+1)*8-1] | |
enc = @blowfish.encrypt_block(block) | |
result += MBase64.encode(enc) | |
end | |
return result | |
end | |
def decrypt text | |
return nil if not text.length % 12 == 0 | |
result = "" | |
num_block = (text.length / 12).to_i | |
num_block.times do |n| | |
block = MBase64.decode( text[n*12..(n+1)*12-1] ) | |
result += @blowfish.decrypt_block(block) | |
end | |
return result.gsub /\0*$/, "" | |
end | |
private | |
def pad text, n=8 | |
pad_num = n - (text.length % n) | |
if pad_num > 0 and pad_num != n | |
pad_num.times { text += 0.chr } | |
end | |
return text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment