Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ruby doesn't like dealing with the binary that mysql aes functions
return. It causes string encoding errors like:
Encoding::UndefinedConversionError:  from ASCII-8BIT to UTF-8

This change sidesteps the issue by base64 encoding the string
before storing it in the db.
  • Loading branch information
jmazzi committed Aug 30, 2012
1 parent b4db698 commit 6892ec2
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/crypt_keeper_providers/mysql_aes.rb
Expand Up @@ -18,16 +18,16 @@ def initialize(options = {})
#
# Returns an encrypted string
def encrypt(value)
value = Base64.encode64(value)
escape_and_execute_sql(["SELECT AES_ENCRYPT(?, ?)", value, key]).first
Base64.encode64 escape_and_execute_sql(
["SELECT AES_ENCRYPT(?, ?)", value, key]).first
end

# Public: Decrypts a string
#
# Returns a plaintext string
def decrypt(value)
value = Base64.decode64(value)
escape_and_execute_sql(["SELECT AES_DECRYPT(?, ?)", value, key]).first
escape_and_execute_sql(
["SELECT AES_DECRYPT(?, ?)", Base64.decode64(value), key]).first
end

private
Expand Down

0 comments on commit 6892ec2

Please sign in to comment.