亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 OpenSSL 中完成的 AES 加密解密成功,但在 Java 中加密時失敗

在 OpenSSL 中完成的 AES 加密解密成功,但在 Java 中加密時失敗

GCT1015 2023-08-23 11:41:26
我使用下面的 OpenSSL 代碼進行 AES 加密,該加密在稅務網站中成功解密openssl rand 48 > 48byterandomvalue.binhexdump /bare 48byterandomvalue.bin > 48byterandomvalue.txtset /a counter=0for /f "tokens=* delims= " %%i in (48byterandomvalue.txt) do (set /a counter=!counter!+1set var=%%iif "!counter!"=="1" (set aes1=%%i)if "!counter!"=="2" (set aes2=%%i)if "!counter!"=="3" (set iv=%%i))set result1=%aes1:~0,50%set result1=%result1: =%set result2=%aes2:~0,50%set result2=%result2: =%set aeskey=%result1%%result2%set initvector=%iv:~0,50%set initvector=%initvector: =%openssl aes-256-cbc -e -in PAYLOAD.zip -out PAYLOAD -K %aeskey% -iv %initvector%openssl rsautl -encrypt -certin -inkey test_public.cer -in 48byterandomvalue.bin -out 000000.00000.TA.840_Key但我想在 Java 中做同樣的事情作為遷移的一部分,所以我使用了 javax.crypto和java.security庫,但是當我在稅務網站上上傳文件時解密失敗//creating the random AES-256 secret keySecureRandom srandom = new SecureRandom(); KeyGenerator keyGen = KeyGenerator.getInstance("AES");keyGen.init(256);SecretKey secretKey = keyGen.generateKey();byte[] aesKeyb = secretKey.getEncoded();//creating the initialization vectorbyte[] iv = new byte[128/8];srandom.nextBytes(iv);IvParameterSpec ivspec = new IvParameterSpec(iv);byte[] encoded = Files.readAllBytes(Paths.get(filePath));str = new String(encoded, StandardCharsets.US_ASCII);//fetching the Public Key from certificateFileInputStream fin = new FileInputStream("test_public.cer");CertificateFactory f = CertificateFactory.getInstance("X.509");X509Certificate certificate = (X509Certificate)f.generateCertificate(fin);PublicKey pk = certificate.getPublicKey();//encrypting the AES Key with Public KeyCipher RSACipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");RSACipher.init(Cipher.ENCRYPT_MODE, pk);byte[] RSAEncrypted = RSACipher.doFinal(aesKeyb);FileOutputStream out = new FileOutputStream("000000.00000.TA.840_Key");out.write(RSAEncrypted);out.write(iv);out.close();另外,java 中生成的 AES 密鑰與通過 openssl 生成的密鑰不同。你們能幫忙嗎?
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

腳本和 Java 代碼使用 RSA 加密的數據不同:


該腳本生成一個隨機的 48 字節序列并將其存儲在文件中48byterandomvalue.bin。前 32 個字節用作 AES 密鑰,后 16 個字節用作 IV。Key 和 IV 用于PAYLOAD.zip以 CBC 模式使用 AES-256 加密文件并將其存儲為 file PAYLOAD。該文件48byterandomvalue.bin使用 RSA 加密并存儲為 file 000000.00000.TA.840_Key。


在 Java 代碼中,生成隨機 32 字節 AES 密鑰和隨機 16 字節 IV。兩者都用于在 CBC 模式下使用 AES-256 執行加密。AES 密鑰使用 RSA 加密,與未加密的 IV 連接,結果存儲在文件 中000000.00000.TA.840_Key。


腳本和 Java 代碼的文件內容000000.00000.TA.840_Key不同。file 000000.00000.TA.840_Key對于使用腳本邏輯生成的 Java 代碼,未加密的AES 密鑰必須與未加密的 IV 連接,并且必須使用 RSA對該結果進行加密:


...

//byte[] aesKeyb byte-array with random 32-bytes key

//byte[] iv      byte-array with random 16-bytes iv

byte[] key_iv = new byte[aesKeyb.length + iv.length];

System.arraycopy(aesKeyb, 0, key_iv, 0, aesKeyb.length);

System.arraycopy(iv, 0, key_iv, aesKeyb.length, iv.length);

...

byte[] RSAEncrypted = RSACipher.doFinal(key_iv);

FileOutputStream out = new FileOutputStream("000000.00000.TA.840_Key");

out.write(RSAEncrypted);

out.close();

...

注意:IV 不必保密,因此不需要加密。僅在生成 Java 代碼中的腳本結果時才需要加密。


另一個問題涉及將任意二進制數據轉換為字符串。如果編碼不合適(例如 ASCII 或 UTF8),這通常會導致數據損壞。所以


...

byte[] encoded = Files.readAllBytes(Paths.get(filePath));

str = new String(encoded, StandardCharsets.US_ASCII);           // Doesn't work: ASCII (7-bit) unsuitable for arbitrary bytes, *        

...

byte[] AESEncrypted = AESCipher.doFinal(str.getBytes("UTF-8")); // Doesn't work: UTF-8 unsuitable for arbitrary bytes and additionally different from * 

String encryptedStr = new String(AESEncrypted);                 // Doesn't work: UTF-8 unsuitable for arbitrary bytes

...

應替換為


...

byte[] encoded = Files.readAllBytes(Paths.get(filePath));

...

byte[] AESEncrypted = AESCipher.doFinal(encoded);

FileOutputStream out = new FileOutputStream("PAYLOAD");

out.write(AESEncrypted);

out.close();

...

在字符串中存儲任意數據的合適編碼是 Base64,但在這種情況下這不是必需的,因為腳本中也不使用 Base64 編碼。


嘗試這些改變。如果出現其他問題,最好分別key_iv測試 AES 加密、RSA 加密和生成。這使得隔離錯誤變得更加容易。


查看完整回答
反對 回復 2023-08-23
  • 1 回答
  • 0 關注
  • 390 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號