【问题标题】:Encrypt in Ruby and Decrypt in Java - Why is it not working?在 Ruby 中加密和在 Java 中解密 - 为什么它不起作用?
【发布时间】:2012-06-29 17:46:35
【问题描述】:

我做错了什么?我希望 Java 程序打印“私有”。我的目标是尝试用 Java 编写 MessageEncryptor.decrypt ruby​​ 方法。

Ruby 加密(大部分代码取自 MessageEncryptor,但未修改为 Marshal),但我已将其提取出来以便更容易查看发生了什么:

require 'openssl'
require 'active_support/base64'

@cipher = 'aes-256-cbc'
d = OpenSSL::Cipher.new(@cipher)
@secret = OpenSSL::PKCS5.pbkdf2_hmac_sha1("password", "some salt", 1024, d.key_len)
cipher = OpenSSL::Cipher::Cipher.new(@cipher)

iv = cipher.random_iv

cipher.encrypt
cipher.key = @secret
cipher.iv = iv

encrypted_data = cipher.update("private")
encrypted_data << cipher.final

puts [encrypted_data, iv].map {|v| ::Base64.strict_encode64(v)}.join("--")

打印出来的:

tzFUIVllG2FcYD7xqGPmHQ==--UAPvdm3oN3Hog9ND9HrhEA==

Java 代码:

package decryptruby;

import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class DecryptRuby {    
    public static String decrypt(String encrypted, String pwd, byte[] salt)
            throws Exception {

        String[] parts = encrypted.split("--");
        if (parts.length != 2) return null;

        byte[] encryptedData = Base64.decodeBase64(parts[0]);
        byte[] iv = Base64.decodeBase64(parts[1]);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(pwd.toCharArray(), salt, 1024, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey aesKey = new SecretKeySpec(tmp.getEncoded(), "AES");


        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));

        byte[] result = cipher.doFinal(encryptedData);
        return result.toString();
    }


    public static void main(String[] args) throws Exception {
        String encrypted = "tzFUIVllG2FcYD7xqGPmHQ==--UAPvdm3oN3Hog9ND9HrhEA==";

        System.out.println("Decrypted: " + decrypt(encrypted, "password", "some salt".getBytes()));
    }
}

打印出来的

解密:[B@432a0f6c

【问题讨论】:

    标签: java ruby encryption


    【解决方案1】:

    这就是问题——或者至少是一个问题:

    byte[] result = cipher.doFinal(encryptedData);
    return result.toString();
    

    你在一个字节数组上调用toString()。数组不会覆盖toString()。如您所见,那根本不会给您想要的东西。相反,您需要编写 something 之类的:

    return new String(result, "UTF-8");
    

    ...但是您需要知道在加密之前使用什么编码将原始字符串转换为字节。从 Ruby 代码中我不清楚使用什么编码,但如果你能明确说明它(最好使用 UTF-8),它会让你的生活更轻松。

    简而言之,我怀疑这个问题根本与加密无关 - 它与在 Ruby 中将文本转换为字节然后将相同的字节序列转换回字符串有关在 Java 中。

    当然,加密也可能会失败,但那是另一回事。

    【讨论】:

    • 谢谢,乔恩。就是这样。再过 7 分钟无法标记为答案。
    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多