Created
March 17, 2011 14:56
-
-
Save scotttam/874452 to your computer and use it in GitHub Desktop.
decrypt PBKDF2/SHA1 and AES
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 "base64" | |
require 'openssl' | |
require 'iconv' | |
CIPHER = "aes-256-cbc" | |
#http://www.ruby-doc.org/ruby-1.9/classes/OpenSSL/PKCS5.html | |
PASSPHRASE = "12345" | |
IV = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].pack("c*") | |
SALT = [1, 2, 3, 4, 5, 6, 7, 8].pack("c*") | |
ITERATIONS = 2048 | |
KEY_LENGTH = 256 | |
module Maitred | |
class AesCbcCrypter | |
CIPHER = "aes-256-cbc" | |
KEY_LENGTH = 256 | |
def initialize(passphrase, salt, iv, iterations = 1024) | |
@key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(passphrase, salt, iterations, KEY_LENGTH) | |
@iv = iv | |
@cipher_obj = OpenSSL::Cipher::Cipher.new(CIPHER) | |
end | |
def encrypt(data) | |
return nil unless data | |
@cipher_obj.encrypt | |
Maitred::BcBase64.encode64(encrypt_decrypt_update_final(data)).gsub("\n", '') | |
end | |
def decrypt(encoded_data) | |
return nil unless encoded_data | |
@cipher_obj.decrypt | |
encrypt_decrypt_update_final(Maitred::BcBase64.decode64(encoded_data)) | |
end | |
private | |
def encrypt_decrypt_update_final(data) | |
@cipher_obj.key = @key | |
@cipher_obj.iv = @iv | |
data = @cipher_obj.update(data) | |
data << @cipher_obj.final | |
end | |
end | |
class BcBase64 | |
def self.decode64(bc_encoded_string) | |
Base64.decode64(bc_encoded_string.gsub("-", "+").gsub("_", "/").gsub(".", "=")) | |
end | |
def self.encode64(unencoded_string) | |
Base64.encode64(unencoded_string).gsub("+", "-").gsub("/", "_").gsub("=", ".") | |
end | |
end | |
end | |
crypter = Maitred::AesCbcCrypter.new(PASSPHRASE, SALT, IV, ITERATIONS) | |
encrypted_data = crypter.encrypt("1304013516000:144034499:CORPLOGIN:203") | |
puts encrypted_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment