【问题标题】:AES Encryption by Java code and decryption by using OpenSSL (terminal)Java代码的AES加密和使用OpenSSL的解密(终端)
【发布时间】:2019-08-16 19:39:59
【问题描述】:

我可以使用下面提到的 Java 代码加密文件中的数据。但是当我尝试从命令行使用 OpenSSL 解密加密文件时,我无法做到这一点。

我试过命令

openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt

它要求输入密码 - 输入aes-256-cbc解密密码: 我输入的密码是“helloworld”,

然后在终端中显示“bad magic number”错误消息。

String pwd  = "helloworld";
String SALT_VALUE  = "12345@salt";
private String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";


String finalTxt  = "Hi, Goof After noon All.";

char[] pwd = Crypt.password.toCharArray();

SecretKey originalKey = Crypt.generateSK(pwd);

byte[] cipherText = Crypt.encrypt(finalTxt.getBytes(),SALT_VALUE.getBytes(), originalKey);

public static SecretKey generateSK(char[] passPhrase) throws NoSuchAlgorithmException,
                                                             InvalidKeySpecException,
                                                             NoSuchPaddingException,
                                                             InvalidAlgorithmParameterException,
                                                             InvalidKeyException {

    PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
    SecretKeyFactory secretKeyFactory;
    secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
    return secretKeyFactory.generateSecret(pbeKeySpec);
}



public static byte[] encrypt(byte[] image, byte[] salt, SecretKey sKey) throws InvalidKeyException,
            IllegalBlockSizeException,
            BadPaddingException,
            InvalidKeySpecException,
            UnsupportedEncodingException,
            InvalidAlgorithmParameterException {
        Cipher cipher;
        try {
            cipher = getCipher(Cipher.ENCRYPT_MODE, salt, sKey);
            return cipher.doFinal(image);
        } catch (Exception e) {
            e.printStackTrace();

        }

        return null;
    }

private static Cipher getCipher(int mode, @NonNull byte[] salt, @NonNull SecretKey secretKey) throws Exception {
        PBEParameterSpec pbeParamSpecKey = new PBEParameterSpec(salt, 1000);
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(mode, secretKey, pbeParamSpecKey);
            return cipher;
    }

【问题讨论】:

标签: java android linux encryption command-line


【解决方案1】:

您似乎缺少 openssl 预期的标头 - 字符串 Salted__,后跟 8 个字节的盐,然后是密文。

【讨论】:

  • 他丢失的更多,key和iv的计算方式不同
【解决方案2】:

它要求输入密码 - 输入 aes-256-cbc 解密密码:我输入密码为“helloworld”,
然后在终端中显示“bad magic number”错误消息

Openssl 默认使用其内部EVP_BytesToKey 函数从提供的密码和盐生成密钥和IV。如果需要,只需在互联网上搜索以查找 Java 实现。

默认情况下,如果您不直接提供密钥和 IV,则 Openssl 期望格式为 Salted__<8bit_salt><ciphertext>

我尝试从命令行使用 OpenSSL 解密加密文件,但我无法做到这一点

我不确定你的 Crypt 类是什么实现的,你可以尝试打印十六进制编码的密钥和 iv。使用openssl和参数-iv <hex_IV> -K <hex_key>可以直接提供IV和Key值来解密密文

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2013-01-07
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2020-01-20
    相关资源
    最近更新 更多