2011-07-20 104 views
2

我已經能夠使用該算法來加密和解密文件,但是當我嘗試從Android向WAS服務器發送文件時,它失敗。這裏是加密側RSA AES解密失敗 - InvalidKeyException

Security.addProvider(new BouncyCastleProvider()); 
    KeyGenerator keygen = KeyGenerator.getInstance("AES"); 
    SecureRandom random = new SecureRandom(); 
    keygen.init(random); 
    SecretKey key = keygen.generateKey(); 

    // wrap with RSA public key 
    ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream (getFileLocation(PUBLIC_KEY, localTest))); 
    Key publicKey = (Key) keyIn.readObject(); 
    keyIn.close(); 

    Cipher cipher = Cipher.getInstance("RSA"); 
    cipher.init(Cipher.WRAP_MODE, publicKey); 
    byte[] wrappedKey = cipher.wrap(key); 
    DataOutputStream out = new DataOutputStream(new FileOutputStream(getFileLocation(SIGN_FILE, localTest))); 
    out.writeInt(wrappedKey.length); 
    out.write(wrappedKey); 

    InputStream in = new ByteArrayInputStream(message.getBytes()); 
    cipher = Cipher.getInstance("AES"); 
    cipher.init(Cipher.ENCRYPT_MODE, key); 
    crypt(in, out, cipher); 
    in.close(); 
    out.close(); 

    FileInputStream fis = new FileInputStream(getFileLocation(SIGN_FILE, localTest)); 
    byte[] buffer = new byte[fis.available()]; 
    int i =0; 
    while (i< buffer.length){ 
     buffer[i]= (byte)fis.read(); 
     i++; 
    } 
    String ss = encodeMsg(buffer); 
    return ss; 

這裏是解密側

 Security.addProvider(new BouncyCastleProvider()); 

     byte[] arr = decodeMsg(encrypted); 

      DataInputStream in = new DataInputStream(new ByteArrayInputStream(arr)); 
      int length = in.readInt(); 
      byte[] wrappedKey = new byte[length]; 
      in.read(wrappedKey, 0, length); 
      // unwrap with RSA private key 
      ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream (getFileLocation(PRIVATE_KEY, localTest))); 
      Key privateKey = (Key) keyIn.readObject(); 
      keyIn.close(); 
      Cipher cipher = Cipher.getInstance("RSA"); 
      cipher.init(Cipher.UNWRAP_MODE, privateKey); 
      Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); 

      OutputStream out = new FileOutputStream(getFileLocation(DECRYPTED, localTest)); 
      cipher = Cipher.getInstance("AES"); 
      cipher.init(Cipher.DECRYPT_MODE, key); 
      crypt(in, out, cipher); 
      in.close(); 
      out.close(); 

      FileInputStream fis = new FileInputStream(getFileLocation(DECRYPTED, localTest)); 
      byte[] buffer = new byte[fis.available()]; 
      int i =0; 
      while (i< buffer.length){//!= 0) { 
       buffer[i]= (byte)fis.read(); 
       i++; 
      } 
      String ss = new String(buffer); 
      return ss; 

同樣,我的工作站上,這個工程。當對WAS Web服務器進行移動請求時,它失敗。起初,它與對象類爭論,所以我使用Java 1.6重新創建了密鑰。我也將這場戰爭重新編譯爲Java 1.6。它的錯誤如下。

--cipher解開

java.security.InvalidKeyException com.ibm.crypto.provider.RSA.engineUnwrap(Unknown Source) 
javax.crypto.Cipher.unwrap(Unknown Source) 
com.webapp.web.security.RSAEncrypt.decrypt(RSAEncrypt.java:161) 
com.webapp.web.MobileRequest.doPost(MobileRequest.java:81) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:738) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:831) 

...

是否WAS環境已經被更新來處理呢?想法? 更新密鑰大小設置爲2048

+1

我不知道這是不是問題,但要擺脫* all *默認值並用明確的值替換它們。不要使用getInstance(「RSA」)'getInstance(「RSA/ECB/PKCS1PADDING」)'。不要使用'getBytes()'使用'getBytes(「UTF-8」)'。 –

回答

0

Unlimited Jurisdiciton政策可能有效,但我試圖使用IBMJCE也沒有成功。然後,我轉而使用SunJCE提供程序(Java 1.6版),現在我可以在Android和Websphere中執行加密和解密。我讓管理員查看策略文件以查看BouncyCastle是否可以啓用,但我確定使用Sun提供程序文件。

1

這可能是由於關鍵策略設置,您是否在兩臺計算機上都安裝了Unlimited Strength Juristiction Policies?他們可以在這個頁面的底部找到:http://www.oracle.com/technetwork/java/javase/downloads/index.html

否則,你如何將數據發送到服務器?

+0

看起來服務器使用IBM的JVM,所以我不認爲Oracle策略文件適用。 –

+0

我不知道服務器是否設置了,但我會問 – iowatiger08