RSA AES 解密失败 - InvalidKeyException

发布于 2024-11-25 20:36:35 字数 3395 浏览 0 评论 0原文

我已经能够使用该算法来加密和解密文件,但是当我尝试将文件从 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

I have been able to use the algorithm to encrypt and decrypt files , but when I go to try and send a file from Android to a WAS server, it fails. Here is the encrypt side

    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;

Here is the decrypt side

        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;

Again, on my workstation, this works. When doing the mobile request to the WAS web server, it fails. At first, it argued with the object class and so I recreated the keys using Java 1.6. I have recompiled the war into Java 1.6 as well. It errors as below.

--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)

...

Does the WAS environment have to be updated to handle this? ideas?
UPDATE the keysize is set to 2048

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-12-02 20:36:35

这可能是由于关键策略设置造成的,您是否在两台计算机上都安装了无限强度管辖权策略?它们可以在此页面底部找到:http://www.oracle.com/javase/downloads/index.html。 oracle.com/technetwork/java/javase/downloads/index.html

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

This could be due to key policy settings, do you have the Unlimited Strength Juristiction Policies installed on both machines? They can be found at the bottom of this page: http://www.oracle.com/technetwork/java/javase/downloads/index.html

Else, how are you sending the data to the server?

温折酒 2024-12-02 20:36:35

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

The Unlimited Jurisdiciton policy might work, but I attempted to use the IBMJCE without success as well. Then, I switched to use the SunJCE provider (version Java 1.6) and now I am able to do the encryption and decryption in both Android and Websphere. I am having the administrators look into the policy files to see if BouncyCastle could be enabled, but I am ok with using the Sun provider files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文