久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 openssl 命令行工具進行 AES 加密,并在 Java

AES encrypt with openssl command line tool, and decrypt in Java(使用 openssl 命令行工具進行 AES 加密,并在 Java 中解密)
本文介紹了使用 openssl 命令行工具進行 AES 加密,并在 Java 中解密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個使用 openssl 工具進行加密的 bash 腳本.

I have a bash script that uses the openssl tool to encrypt.

#!/bin/bash

key128="1234567890123456"
iv="1234567890123456"
openssl enc -aes-128-cbc -in test -out test.enc -K $key128 -iv $iv

以及嘗試解密腳本生成的文件的 Java 代碼.

And Java code that tries to decrypt the file produced by the script.

public class crypto {

    public static void main( String[] args )
    {
        try {
            File f = new File("test.enc");
            Cipher c;
            Key k;
            String secretString = "01020304050607080900010203040506";
            String ivString = "01020304050607080900010203040506";
            byte[] secret = hexStringToByteArray(secretString);
            byte[] iv = hexStringToByteArray(ivString);

            c = Cipher.getInstance("AES/CBC/PKCS5Padding");
            k = new SecretKeySpec(secret, "AES");
            c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv));

            CipherInputStream cis = new CipherInputStream(new FileInputStream(f), c);
            BufferedReader br = new BufferedReader(new InputStreamReader(cis));

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.getMessage());
        } catch (NoSuchPaddingException e) {
            System.out.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.out.println(e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            System.out.println(e.getMessage());
        }

    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}
                                                            33,1          71%

當我運行 Java 代碼時,它不會打印任何內容.腳本和 Java 代碼是否不匹配?

When I run the Java code, it doesn't print anything. Is there a mismatch between the script and Java code?

第二個問題是我是否可以將其重寫為使用密碼而不是密鑰/iv.為了做到這一點,有沒有辦法知道 openssl 用于給定密碼的 iv?

A secondary question is whether I can rewrite this to use password instead of key/iv. In order to do that, is there a way to know the iv that openssl uses for a given password?

推薦答案

正如上面提到的@Polynomial,bash 腳本和 Java 代碼之間的鍵和 iv 不匹配.將 bash 腳本更改為以下內容即可解決問題.

As @Polynomial mentioned above, the keys and iv's don't match between the bash script and Java code. Changing the bash script to the following solves the problem.

#!/bin/bash

key128="01020304050607080900010203040506"
iv="01020304050607080900010203040506"
openssl enc -aes-128-cbc -in test -out test.enc -K $key128 -iv $iv

如果按以下方式執行openssl,它將使用密碼,并打印使用的密鑰和iv.在上面的 Java 程序中,key 和 iv 可以被替換.

If openssl is executed in the following way, it will use a password, and print the key and iv used. That key and iv can be substituted in the Java program above.

openssl enc -nosalt -aes-128-cbc -in test -out test.enc -p

這篇關于使用 openssl 命令行工具進行 AES 加密,并在 Java 中解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 特黄毛片视频 | 国产真实精品久久二三区 | 日韩精品一区二区三区视频播放 | 精品久久久久久久人人人人传媒 | 欧美日韩亚洲国产 | 91成人免费看片 | 秋霞av国产精品一区 | 亚洲 欧美 另类 日韩 | av官网在线 | 伊人精品久久久久77777 | 国产日韩欧美一区二区 | 国产成人精品免费视频 | 国产区精品在线观看 | 久久亚洲国产精品日日av夜夜 | 国产一区二区三区欧美 | 男女羞羞在线观看 | 亚洲一区二区三区免费观看 | 在线免费观看黄色网址 | 日韩精品在线看 | 狠狠色网| 亚洲高清一区二区三区 | 欧美日韩国产在线观看 | 成人精品久久日伦片大全免费 | 亚洲国产中文字幕 | 国产乱码久久久久久 | 亚洲一区二区三区欧美 | 中文字幕日韩一区 | 99日韩| 日韩一二区在线 | 免费一级做a爰片久久毛片潮喷 | 一区二区福利视频 | 久久成| 91在线看 | 国产在线一区二区 | 中文字幕日韩欧美一区二区三区 | 国产精品久久久久久久粉嫩 | 日本黄色片免费在线观看 | 久久av网| 久久伊人亚洲 | 日韩a在线观看 | 欧美一级α片 |