java - RSA AES 解密失败 - InvalidKeyException

标签 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 重新创建了 key 。我也将 war 重新编译为 Java 1.6。错误如下。

--密码解包

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 环境才能处理此问题?想法? 更新 key 大小设置为 2048

最佳答案

这可能是关键策略设置造成的,您是否在两台机器上都安装了 Unlimited Strength Juristiction Policies?它们可以在本页底部找到:http://www.oracle.com/technetwork/java/javase/downloads/index.html

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

关于java - RSA AES 解密失败 - InvalidKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6765240/

相关文章:

java - 如何在Java中的OrientDB 2.2.x空间模块中查询位置 "within"?

java - 如何使用服务器刷新身份验证 token

java - java加解密密码用什么API和算法

encryption - 如何在 nginx 上进行即时解密?

java - 验证C#中Java生成的RSA签名

java - JGraphX - 展开/折叠单元格/顶点

c# - 有没有办法将数据流式传输到 GIT 而不是传递文件名?

java - 无法为 JMX 禁用 SSL

android - 无法在 Flutter 中构建具有多种风格的发布 apk?

android - 如何在 Android 上创建随键盘一起显示的 EditText