[英]Cipher.getInstance(“DES”) return different result on sun jre and ibm jre
我使用Cipher.getInstance(“ DES”)加密和解密消息,但在sun jre和ibm jre上發現了不同的結果。因此,我無法在AIX上加密消息,然后在Linux上解密。導入sunjce並在ibm jre環境中使用它並獲得與使用ibmjce相同的結果,但是與在sun jre上有所不同。是否有一種方法可以在Sun jre和ibm jre上使消息得到相同的結果? 下面在sun jre和ibm jre上運行的代碼將得到不同的結果。
public static void test2() throws Exception {
Security.addProvider(new SunJCE());
Security.addProvider(new IBMJCE());
String strKey = "12345678";
KeyGenerator generator = KeyGenerator.getInstance("DES", "SunJCE");
// KeyGenerator generator = KeyGenerator.getInstance("DES", "IBMJCE");
System.out.println("KeyGenerator provider:" + generator.getProvider());
//
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(strKey.getBytes());
generator.init(secureRandom);
Key key = generator.generateKey();
Cipher cipher = Cipher.getInstance("DES", "SunJCE");
// Cipher cipher = Cipher.getInstance("DES", "IBMJCE");
System.out.println("Cipher provider:" + cipher.getProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
String strTest = "TESTtest123";
byte[] byteTest = strTest.getBytes("UTF-8");
byte[] byteEncry = cipher.doFinal(byteTest);
System.out.println("strTest:" + strTest);
System.out.println("encode:" + new BASE64Encoder().encode(byteEncry));
}
任何想法,建議或解決方法,表示贊賞。 謝謝。
編輯:我的應用程序部署在Windows,red hat linux和aix上。它們可以互相傳遞加密的消息,並且接收方將解密該消息。在win和linux上工作正常。但是aix總是會收到“ Exception” javax.crypto.BadPaddingException:嘗試解密來自其他系統的消息時,給最終塊未正確填充。當窗口或linux嘗試解密來自aix的消息時,發生了同樣的事情。有什么辦法可以解決此問題?
服務提供商接口的全部目的是指定所需的功能(DES),然后系統為您提供適當的實現。 不要嘗試選擇特定的提供程序,只需像對SecureRandom
一樣使用getInstance("DES")
。
請注意,您確實確實需要指定所需的完整密碼設置。 只是DES
還不夠具體,這可能會導致您看到任何不兼容的情況。 DES/CBC/PKCS5Padding
可能正是您想要的,如果您使用的是新系統,則應改用AES。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.