Java 中的 RSA 加密

发布于 2024-11-07 20:24:36 字数 1887 浏览 0 评论 0原文

我正在尝试使用 Java 中的 RSA 编写加密算法,我得到了 “javax.crypto.BadPaddingException:数据必须从零开始”;我不知道这个例外有什么用。 这是我在此处使用的示例

,这是我的代码;请帮忙。

public byte[] getEncryptedValue(byte[] bytes, PublicKey key) {
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return blockCipher(bytes, Cipher.ENCRYPT_MODE);
    } catch (Exception ex) {
        Logger.getLogger(SecurityUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public byte[] getDecryptedValue(byte[] bytes, PrivateKey key) {
    try {
        cipher.init(Cipher.DECRYPT_MODE, key);
        return blockCipher(bytes, Cipher.DECRYPT_MODE);
    } catch (Exception ex) {
        Logger.getLogger(SecurityUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

private byte[] append(byte[] prefix, byte[] suffix) {
    byte[] toReturn = new byte[prefix.length + suffix.length];
    System.arraycopy(prefix, 0, toReturn, 0, prefix.length);
    System.arraycopy(suffix, 0, toReturn, prefix.length, suffix.length);
    return toReturn;
}

private byte[] blockCipher(byte[] bytes, int mode) throws IllegalBlockSizeException, BadPaddingException {
    byte[] scrambled = new byte[0];
    byte[] toReturn = new byte[0];blocks (because of RSA)
    int length = (mode == Cipher.ENCRYPT_MODE) ? 100 : 128;
    int n = 0;
    byte[] buffer = new byte[length];

    for (int i = 0; i < bytes.length; i++) {
        if ((i > 0) && (i % length == 0)) {
            n = 0;
            scrambled = cipher.doFinal(buffer);
            toReturn = append(toReturn, scrambled);
        }
        buffer[i % length] = bytes[i];
        n++;
    }
    ***scrambled = cipher.doFinal(buffer, 0, n);*** <-- the exception is caught here
    toReturn = append(toReturn, scrambled);
    return toReturn;
}

I am trying to write an encryption algorithm using RSA in Java, I am getting a
"javax.crypto.BadPaddingException: Data must start with zero"; I do not know what is this exception for.
This is the example I used here

and Here is my code; please help.

public byte[] getEncryptedValue(byte[] bytes, PublicKey key) {
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return blockCipher(bytes, Cipher.ENCRYPT_MODE);
    } catch (Exception ex) {
        Logger.getLogger(SecurityUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public byte[] getDecryptedValue(byte[] bytes, PrivateKey key) {
    try {
        cipher.init(Cipher.DECRYPT_MODE, key);
        return blockCipher(bytes, Cipher.DECRYPT_MODE);
    } catch (Exception ex) {
        Logger.getLogger(SecurityUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

private byte[] append(byte[] prefix, byte[] suffix) {
    byte[] toReturn = new byte[prefix.length + suffix.length];
    System.arraycopy(prefix, 0, toReturn, 0, prefix.length);
    System.arraycopy(suffix, 0, toReturn, prefix.length, suffix.length);
    return toReturn;
}

private byte[] blockCipher(byte[] bytes, int mode) throws IllegalBlockSizeException, BadPaddingException {
    byte[] scrambled = new byte[0];
    byte[] toReturn = new byte[0];blocks (because of RSA)
    int length = (mode == Cipher.ENCRYPT_MODE) ? 100 : 128;
    int n = 0;
    byte[] buffer = new byte[length];

    for (int i = 0; i < bytes.length; i++) {
        if ((i > 0) && (i % length == 0)) {
            n = 0;
            scrambled = cipher.doFinal(buffer);
            toReturn = append(toReturn, scrambled);
        }
        buffer[i % length] = bytes[i];
        n++;
    }
    ***scrambled = cipher.doFinal(buffer, 0, n);*** <-- the exception is caught here
    toReturn = append(toReturn, scrambled);
    return toReturn;
}

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

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

发布评论

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

评论(2

往事风中埋 2024-11-14 20:24:36

问题可能是由于某些编码问题,使用套接字通过网络发送的数据可能会损坏。我在开发一个简单的客户端/服务器聊天程序时遇到了同样的问题,该程序使用非对称密钥加密/解密服务器和客户端之间的消息,反之亦然,我没有将消息作为字符串发送,而是将其作为字节数组发送,即加密的消息。

The problem could be the data sent over the network using sockets may be corrupted due to some encoding problems. I had the same problem while developing a simple client/server chat program that encrypts/decrypts using asymmetric key the messages between the server and client and vise versa, instead of sending the message as a string, I sent it as a byte array which is the encrypted message.

任谁 2024-11-14 20:24:36
  • 检查密钥是否匹配
  • 检查 getEncryptedValue 返回的数据是否与传递给 getDecryptedValue 的数据相同
  • 检查 blockCipher 方法中循环的正确性
  • check if keys are matching
  • check if data returned by getEncryptedValue are the same that you pass to getDecryptedValue
  • check corectness of loop in blockCipher method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文