简体   繁体   English

相当于java解密代码的openssl(shell脚本)

[英]Openssl (shell script) equivalent of java decryption code

Need help with openssl, What would be the openssl equivalent of the below java method?需要有关 openssl 的帮助,以下 java 方法的 openssl 等价物是什么?

private static String decryptString(String value, String myKey) {

    MessageDigest sha = null;
    SecretKeySpec secretKey = null;
    try {
        byte[] key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); // use only first 128 bit
        secretKey = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return (new String(cipher.doFinal(Base64.decodeBase64(value))));
    } catch (Exception e) {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

The function is digesting a string (myKey) using SHA-1 and then using the first 16 bytes of the resulting digest as the key for an AES128-ECB decryption operation.该函数使用 SHA-1 对字符串 (myKey) 进行摘要,然后使用结果摘要的前 16 个字节作为 AES128-ECB 解密操作的密钥。 Before decryption, the ciphertext is first base64 decoded.在解密之前,密文首先被base64解码。

To digest a string using SHA-1 use the code here: https://wiki.openssl.org/index.php/EVP_Message_Digests要使用 SHA-1 消化字符串,请使用此处的代码: https : //wiki.openssl.org/index.php/EVP_Message_Digests

That code actually uses SHA-256.该代码实际上使用 SHA-256。 To make it do SHA-1 instead replace the instance of EVP_sha256() with EVP_sha1() .要使其执行 SHA-1, EVP_sha256()的实例替换为EVP_sha1() The first 16 bytes of the result is your "key".结果的前 16 个字节是您的“密钥”。

Next the input "value" is base64 decoded.接下来,输入“值”被 base64 解码。 There is some sample code to do that with OpenSSL here:这里有一些示例代码可以使用 OpenSSL 做到这一点:

http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/ http://www.ioncannon.net/programming/122/howto-base64-decode-with-cc-and-openssl/

Finally, to do the AES-ECB decryption operation use the code here:最后,要进行 AES-ECB 解密操作,请使用此处的代码:

https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#Decrypting_the_Message https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#Decrypting_the_Message

That code is using AES256-CBC.该代码使用的是 AES256-CBC。 To make it do AES128-ECB instead replace the instance of EVP_aes_256_cbc() with EVP_aes_128_ecb() .让它做AES128-ECB而不是取代的实例EVP_aes_256_cbc()EVP_aes_128_ecb() The key argument is as you calculated above. key参数如您上面计算的那样。 The iv argument is just NULL for ECB. ECB 的iv参数只是 NULL。 The ciphertext is the base64 decoded data that you calculated above.密文是您在上面计算的 base64 解码数据。 Note that the "PKCS5PADDING" part of the Java code is the default in OpenSSL anyway, so nothing special needs to be done for that.请注意,Java 代码的“PKCS5PADDING”部分无论如何都是 OpenSSL 中的默认值,因此无需为此做任何特殊处理。

The plaintext that has been output from the decryption operation is the return value from your Java function.解密操作输出的明文是 Java 函数的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM