RSA AES解密失败 - InvalidKeyException

时间:2011-07-20 16:27:35

标签: java android encryption rsa

我已经能够使用该算法来加密和解密文件,但是当我尝试将文件从Android发送到WAS服务器时,它会失败。这是加密方

    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 unwrap

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

2 个答案:

答案 0 :(得分:1)

这可能是由于关键策略设置,您是否在两台计算机上安装了无限强度法律政策?它们可以在本页底部找到:http://www.oracle.com/technetwork/java/javase/downloads/index.html

否则,您如何将数据发送到服务器?

答案 1 :(得分:0)

Unlimited Jurisdiciton策略可能有效,但我尝试使用IBMJCE也没有成功。然后,我切换到使用SunJCE提供程序(Java 1.6版本),现在我可以在Android和Websphere中进行加密和解密。我让管理员查看策略文件以查看是否可以启用BouncyCastle,但我可以使用Sun提供程序文件。

相关问题