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 8046357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:45:47+00:00 2026-06-05T05:45:47+00:00

I am trying to encrypt data by the cipher DES-EDE3-CBC in Ruby, then decrypt

  • 0

I am trying to encrypt data by the cipher “DES-EDE3-CBC” in Ruby, then decrypt the encrypted data in Java.

Here is the code in Ruby I do the encrypting:

require 'digest'
require 'openssl'
require 'base64'

ALG = "DES-EDE3-CBC"
key = "80f28a1ef4aa9df6ee2ee3210316b98f383eb344"
cipher = OpenSSL::Cipher::Cipher.new(ALG)
cipher.pkcs5_keyivgen(key, nil)
cipher.encrypt
data = "hello"
result = cipher.update(data)
result << cipher.final
# Write the data to file.
File.open("enc.txt", "wb"){|f| f.write result}

Then decrypt in Java:

import java.security.*;
import java.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Test{
  public static void main(String[] args) throws Exception {
    String key = "80f28a1ef4aa9df6ee2ee3210316b98f383eb344";

    // Init the key
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    Key secretKey = keyFactory.generateSecret(desKeySpec);

    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    byte[] buf = new byte[1024];
    InputStream input = new FileInputStream(new File("enc.txt"));
    FileOutputStream output = new FileOutputStream(new File("dec.txt"));

    int count = input.read(buf);

    // Read and decrypt file content
    while (count >= 0) {
        output.write(cipher.update(buf, 0, count)); 
        count = input.read(buf);        
    }
    output.write(cipher.doFinal());
    output.flush();

  }
}

But I always get the exception when running the Java code:

Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
  at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
  at javax.crypto.Cipher.doFinal(Cipher.java:1969)
  at Test.main(Test.java:29)

I think the problem is the “DES-EDE3-CBC” cipher in Ruby is not compatible with Java “DES/ECB/PKCS5Padding”.
How I can do it?

  • 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-05T05:45:48+00:00Added an answer on June 5, 2026 at 5:45 am

    On the Ruby side, don’t use Cipher#key_ivgen unless it’s a strict requirement. It has been deprecated and OpenSSL::PKCS5 should be used instead for password-based encryption. Your key seems to be hex, 40 characters, which results in at most 20 bytes of entropy. Any reason why it has this particular form? Triple DES key are 24 bytes long, the easiest way to create a secure key is to do:

    cipher = OpenSSL::Cipher.new("DES-EDE3-CBC")
    cipher.encrypt
    key = cipher.random_key
    

    When doing so, you should also always generate a random IV before encryption:

    iv = cipher.random_iv
    

    In order to communicate the key and IV to Java, if you should require a hex representation of them:

    hex_key = key.unpack("H*")[0]
    

    In Java, you made the mistake to use DES instead of DESede, and you use it in ECB mode, when in Ruby you were using CBC. Don’t use ECB. You will also have to initialize the Cipher with the key and IV that were used in Ruby. You’ll need a proper hex en-/decoder for this (search on the web), I’ve taken the first code sample I could find, but be warned that this is not the most efficient way to do it, a table-based lookup will be much faster. But we need something to get you started, so here it is:

    public static byte[] hexDecode(String hex) {
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        for (int i = 0; i < hex.length(); i+=2) {
            int b = Integer.parseInt(hex.substring(i, i + 2), 16);
            bas.write(b);
        }
        return bas.toByteArray();
    }
    
    byte[] key = hexDecode("<hex representation of the Ruby key>");
    byte[] iv = hexDecode("<hex representation of the Ruby IV>");
    
    DESedeKeySpec desKeySpec = new DESedeKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
    Key secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
    ... /* do the decryption */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to encrypt some data from python (Google App Engine) and then decrypt
Talking about javax.crypto.Cipher I was trying to encrypt data using Cipher.getInstance(RSA/None/NoPadding, BC) but I
I am trying to encrypt some data with the following code: public static byte[]
I'm trying to measure how long it takes read then encrypt some data (independently).
I am trying encrypt and decrypt one column in table. My code is like
I am trying to encrypt data that a serializable (xml) class contains. Here is
I'm trying to encrypt and decrypt data using RSA in C#. I have the
I'm trying to encrypt a text file using Perl and then decrypt it using
I am trying to write encrypt/decrypt methods for AES 256 CBC encryption using PKCS5Padding
I am trying to encrypt/decrypt an XML file. I found this sample for encrypting

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.