Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:32+00:00 2026-06-12T09:14:32+00:00

I have a rails application that encrypts (with attr_encrypted) 2 fields in one of

  • 0

I have a rails application that encrypts (with attr_encrypted) 2 fields in one of the models.

Another part of my process, which is not the web-application needs to perform some tasks using this data (plaintext).

I’m trying to read the stored values from the DB and decrypt them but just can’t..

my model looks like this:

class SecretData < ActiveRecord::Base
  mysecret = "mylittlesecret"

  attr_encrypted :data1, :key=>mysecret, :algorithm => "aes-256-cbc"
  attr_encrypted :data2, :key=>mysecret, :algorithm => "aes-256-cbc"

  ...
end

The DB fields (encrypted_data1 and encrypted_data2) are filled with data but when I try to decode the base64 (attr_encrypted does that by default) and decrypt (I tried with openssl from commandline and using Java) I get “bad magic number” (openssl) or various errors about key length (in Java). I spent a lot of time trying to decrypt those strings but just couldn’t find the way.

Here is all the data I have:
encrypted + base64 strings (for data1 and data2) are:

cyE3jDkKc99GVB8TiUlBxQ==
sqcbOnBTl6yy3wwjkl0qhA==

I can decode base64 from both of them and get some byte array.
When I try:

echo cyE3jDkKc99GVB8TiUlBxQ== | openssl aes-256-cbc -a -d   (and type "mylittlesecret" as the password)

I get: “bad magic number”

When I try the following Java code:

Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);

I get “java.security.InvalidKeyException: Invalid AES key length: 14 bytes”
I’ve tried many variations for the Java code, so it might be that this particular one is a complete mistake..

When I try in ruby:

irb(main):069:0> Encryptor.decrypt(Base64.decode64("cyE3jDkKc99GVB8TiUlBxQ=="), ,key=>'mylittlesecret')
=> "data1-value"

I get the correct value decrypted (as you can see).

I’ve also noticed that when I try to encrypt the same string in Java and encode in Base64 I get a longer string (after base64). Don’t know why but it’s probably related..

I thought I should also have a salt/iv with the encrypted value, but I don’t see it stored anywhere.. I tried to encrypt the same value twice and got the same output string so it’s not a random one.

Does anyone know how does attr_encrypted (it’s using ruby’s Encryptor) encrypts data and how I should decrypt it externally?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:14:32+00:00Added an answer on June 12, 2026 at 9:14 am

    Well, thanks to owlstead I was able to solve this. I’m posting the code in ruby and Java in case someone needs it in the future:

    The problem, as owlstead mentioned, is indeed in the EVP_BytesToKey (key generation from a password and salt). Ruby from some reason doesn’t use the standard one and therefore Java (or openssl) can’t decode.

    Here is a ruby implementation that uses a standard method:

    def self.encrypt(options)
    
       plaintext = options[:value]
       return true if plaintext.blank?
    
       cipher = OpenSSL::Cipher::Cipher.new(@@cipher_type)
       cipher.encrypt
    
       iv = cipher.random_iv
       salt = (0 ... @@salt_length).map{65.+(rand(25)).chr}.join   # random salt
       key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(@@password, salt, @@pkbdf_num_iters, cipher.key_len)
    
       cipher.key = key
       cipher.iv = iv
    
       enc_data = cipher.update(plaintext)
       enc_data << cipher.final
    
       final_data = salt << iv << enc_data
       Base64.strict_encode64(final_data)
    end
    
    def self.decrypt(options)
    
       ciphertext = options[:value]
       return true if ciphertext.blank?
    
    
       cipher = OpenSSL::Cipher::Cipher.new(@@cipher_type)
       cipher.decrypt
    
       cipher_data = Base64.decode64(ciphertext)
    
       salt = cipher_data[0 .. @@salt_length-1]
       iv = cipher_data[@@salt_length .. @@salt_length+cipher.iv_len]
       enc_data = cipher_data[@@salt_length+cipher.iv_len .. -1]  # the rest
    
       key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(@@password, salt, @@pkbdf_num_iters, cipher.key_len)
    
       cipher.key = key
       cipher.iv = iv
    
       plaintext = cipher.update(enc_data)
       plaintext << cipher.final
    
       plaintext
      end
    

    I’ve set the following parameters:
    – cipher_type = aes-128-cbc (Java supports only 128 but out of the box. For more than that you need to install some additional packages)
    – salt_length = 8
    – pkbdf_num_iters = 1024

    This is the Java method for decoding:

    public String decrypt(String ciphertext) throws Exception {
        byte[] crypt = Base64.decodeBase64(ciphertext);
    
        // parse the encrypted data and get salt and IV
        byte[] salt = Arrays.copyOfRange(crypt, 0, saltLength);
        byte[] iv = Arrays.copyOfRange(crypt, saltLength, saltLength + ivLength);
        byte[] encryptedData = Arrays.copyOfRange(crypt, saltLength + ivLength, crypt.length);
    
        // generate key from salt and password  
        SecretKeyFactory f = SecretKeyFactory.getInstance(secretKeyName);
        KeySpec ks = new PBEKeySpec(password.toCharArray(), salt, pbkdfNumIters, keyLength);
        SecretKey s = f.generateSecret(ks);
        Key keySpec = new SecretKeySpec(s.getEncoded(),"AES");
    
        // initialize the cipher object with the key and IV
        Cipher cipher = Cipher.getInstance(cipherAlgo);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    
        // decrypt
        byte[] decBytes = cipher.doFinal(encryptedData);
    
        return new String(decBytes);
    }
    

    Worked for me.

    Hope it helps (or will, to someone..)

    Zach

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 3 application that needs to display images from another application.
I have a Ruby on Rails application that I'm developing on my computer, which
I have a root_path on my Rails application that is not user-protected i.e. it's
I have a rails application that models a house. house contains rooms and rooms
I have a Rails application that queries a 3rd party web service. I am
So I have a rails application that has two different navigation headers. One that
I have a rails application that helps users submit articles. Does any one know
I have a Rails simple application that has two main models. A person model
I have a Rails application that uses static html pages (not in app/views/) sending
I have a Rails application that I'm in the process of designing. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.