Code Monkey home page Code Monkey logo

Comments (16)

mhenrixon avatar mhenrixon commented on August 16, 2024

I guess adding something like Base64.encode64(encrypted).encode('utf-8') somewhere would do the trick. Possibly before the encryption and after the decryption.

from crypt_keeper.

mhenrixon avatar mhenrixon commented on August 16, 2024

Don't seem to work and I don't know it there is something wrong on my setup but the following code doesn't help.

require 'crypt_keeper/provider/aes'

module CryptKeeper
  module Provider
    class BaseAesEncryptor < CryptKeeper::Provider::Aes

      # Public: Encrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES.
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the encrypted string
      def encrypt(value)
        Base64.encode64(super(value)).encode('utf-8')
      end

      # Public: Decrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES (and thus cannot be decrypted).
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the decrypted string
      def decrypt(value)
        super(Base64.decode64(value.encode('ascii-8bit')))
      end
    end
  end
end

from crypt_keeper.

mhenrixon avatar mhenrixon commented on August 16, 2024
# encoding: utf-8
require 'crypt_keeper/provider/aes'

module CryptKeeper
  module Provider
    class BaseAesEncryptor < CryptKeeper::Provider::Aes

      # Public: Encrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES.
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the encrypted string
      def encrypt(value)
        super(value)
      end

      # Public: Decrypt a string
      #
      # Note: nil and empty strings are not encryptable with AES (and thus cannot be decrypted).
      # When they are encountered, the orignal value is returned.
      # Otherwise, returns the decrypted string
      def decrypt(value)
        super(value).force_encoding('utf-8')
      end
    end
  end
end

That solves the problem

from crypt_keeper.

jfeltesse avatar jfeltesse commented on August 16, 2024

Could it be related to #50 ? Using AES as well.
We worked around it with https://github.com/robotvert/crypt_keeper/commit/af78261984d87f4c26220e5b6b41c655d588b876
In some other app maintained by other guys in our company they solved it with a rails initializer that basically does the same using monkey patching.

from crypt_keeper.

mhenrixon avatar mhenrixon commented on August 16, 2024

Yeah it is that exact problem decrypting and then forcing encoding to utf-8 seems to do the trick.

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

Iā€™m mulling over some options on how to fix this. I really want to avoid messing with encodings, but it sounds like I need to at least provide an option to force encodings on operations.

@robotvert @mhenrixon any thoughts on providing an option in crypt_keeper like force_encoding that would enable this behavior for a given provider?

class Dog < ActiveRecord::Base
  crypt_keeper encryptor: :aes, encoding: 'UTF-8' ...
end

from crypt_keeper.

jfeltesse avatar jfeltesse commented on August 16, 2024

e10e3d6 made big changes around that area, I need to test when I have time but could it be the AES gem handles that automagically? (wishful thinking)

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

@robotvert are you using a pre-release? That commit you're pointing to was not available in a stable release.

from crypt_keeper.

jfeltesse avatar jfeltesse commented on August 16, 2024

@jmazzi yes and no, I mean, aren't all versions -pre?
My fork that we are using is based off 0.14.pre, which dates back to last summer.
That commit I pointed it out to has indeed been added at a later time, my point was, could it be the problem we are experiencing with encodings be naturally resolved since that commit changed a lot of things around this area?
Sorry, I haven't had the time to try out the latest code, since the existing data is not compatible it's not as simple as incrementing a version number in the Gemfile... šŸ˜…

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

@robotvert it's possible it fixed it, that's what I was getting at.

I've updated the release notes. This v0.16.0-pre deprecates the legacy encryptor but it does not remove them. You'll just get a warning about it. Could you give it a shot when you get time?

from crypt_keeper.

jfeltesse avatar jfeltesse commented on August 16, 2024

@jmazzi unfortunately, after upgrading to 0.16.0.pre and re-encoding the data has explained in the AES Legacy Migration Instructions the result is the same here.
Encrypting data in Japanese works but decrypting it returns the hex representation.
Applying force_encoding('utf-8') does the trick though.

from crypt_keeper.

mhenrixon avatar mhenrixon commented on August 16, 2024

Tried upgrading to 16.1 but it completely messed up my whole application and like @robotvert says the problem persists.

What now? How can I safely update?

from crypt_keeper.

jfeltesse avatar jfeltesse commented on August 16, 2024

@mhenrixon the upgrade was smooth here, the executable provided to re-encrypt the data using the new method worked like a charm. FWIW I dealt with the encoding issue using this:

module AesNewOverrides
  def decrypt(value)
    v = super(value)
    v.force_encoding('utf-8') if v.respond_to?(:force_encoding)
  end
end

module CryptKeeper
  module Provider
    class AesNew
      prepend AesNewOverrides
    end
  end
end

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

@mhenrixon could you elaborate on the problems you had when you upgraded?

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

@mhenrixon @robotvert would you mind giving the feature/encoding_fixes branch a shot? This should fix the encoding issue you were encoutering.

from crypt_keeper.

jmazzi avatar jmazzi commented on August 16, 2024

This was released as 0.17.0

from crypt_keeper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.