Last active
March 26, 2017 16:58
-
-
Save bglusman/7e1df9efad80845977b60d44d335f3df to your computer and use it in GitHub Desktop.
simple working ruby-encryption to elixir-decryption (DO NOT USE a zero IV for real)
This file contains hidden or 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
defmodule Decrypt do | |
@iv String.duplicate("0", 16) | |
def unpad(data) do | |
to_remove = :binary.last(data) | |
:binary.part(data, 0, byte_size(data) - to_remove) | |
end | |
def decrypt(data, key) do | |
IO.puts "WOrking to decrypt #{data} using #{key}" | |
padded = :crypto.block_decrypt(:aes_cbc256, key, @iv, :base64.decode(data)) | |
unpad(padded) | |
end | |
end |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require "openssl" | |
require 'digest/sha2' | |
require 'base64' | |
alg = "AES-256-CBC" | |
key = ARGV[0] | |
iv = "0" * 16 | |
key64 = [key].pack('m') | |
raise 'Key Error' if(key.nil? or iv.size != 16) | |
aes = OpenSSL::Cipher::Cipher.new(alg) | |
aes.encrypt | |
aes.key = key | |
aes.iv = iv | |
cipher = "" | |
cipher << aes.update(ARGV[1]) | |
cipher << aes.final | |
cipher64 = [cipher].pack('m') | |
puts cipher64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, and yes, for sure typically random IV and base64 encoding it makes total sense, that's approx what ruby does, but for decrypting obviously random doesn't work, and in either case it's good to be able to control it... I'm considering making it a SHA1 of something already unique about the data so I can avoid shipping around the IV also and just have a convention, but we'll see....
Oh and yes, I was aware it wasn't really a zero iv, it was just meant to be simple and the same in both languages, not necessarily have any special properties.