【问题标题】:RSA padding decryption problem (chat application)RSA填充解密问题(聊天应用)
【发布时间】:2020-03-18 10:38:26
【问题描述】:

我正在用 Java 编写聊天应用程序的代码,我必须实现加密算法。我正在使用 RSA,每个用户都有自己的密钥。当一个用户向其他用户发送消息时,应用程序将加密消息并将密码写入 .txt 文件。然后,应用程序将读取该 .txt 文件,解密文件并显示解密的密码......但我有错误

> javax.crypto.BadPaddingException: Decryption error
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:383)
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:294)
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at org.fastchat.User.readMessage(User.java:139)
    at org.fastchat.ChatWith.<init>(ChatWith.java:80)
    at org.fastchat.WatchInbox.startWatch(WatchInbox.java:75)
    at org.fastchat.WatchInbox.run(WatchInbox.java:41)

我检查了密钥和消息,你能告诉我更多关于这个问题的信息吗? 在收件箱中加密和写入消息的功能

public void sendMessage(User receiver, String message) throws Exception {
    //Encrypter message, sent to receiver inbox
    // Getting the public key from the key pair
    PublicKey publicKey = receiver.getPub();

    // Creating a Cipher object
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

    // Initializing a Cipher object
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    // Add data to the cipher
    byte[] input = message.getBytes();
    cipher.update(input);

    // encrypting the data
    byte[] cipherText = cipher.doFinal();
//      System.out.println(new String(cipherText, "UTF8")); 

    BufferedWriter recFile=new BufferedWriter(new FileWriter(receiver.getInbox()+"/"+getUsername()+".txt"));
    recFile.write(new String(cipherText,"UTF8"));


    //DONT DELETE
    //Make check file in receiver check folder, inbox check
    BufferedWriter check=new BufferedWriter(new FileWriter(receiver.getCheck()+"/"+getUsername()+".txt"));
    Random random=new Random();
    Integer num=random.nextInt(20);
    check.write(num.toString());

    check.close();
    recFile.close();
}

这是一个解密和读取消息的函数

    public String readMessage(String msg) throws Exception{
    // Creating a Cipher object

    System.out.println("\n\n"+msg+"\n");     //!!!!!
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

    // Initializing the same cipher for decryption
    cipher.init(Cipher.DECRYPT_MODE, getPriv());

    // Decrypting the text
    byte[] decipheredText = cipher.doFinal(msg.getBytes());
    return new String(decipheredText,"UTF8");   
}

我可以仅使用 .txt 文件使用 RSA 算法进行加密/解密,还是必须使用一些额外的方法?

【问题讨论】:

    标签: java encryption cryptography rsa chat


    【解决方案1】:

    是的,如果要将密文视为文本,则需要对密文进行 base 64 编码。密文由随机值的字节组成,并不是每个字节都代表一个有效字符。

    recFile.write(new String(cipherText,"UTF8"));
    

    上述行中的new String 和解密过程中的getBytes 都可能导致字节丢失、更改甚至插入。


    使用 UTF-8 作为实际字节的编码策略是个好主意。目前您正在为纯文本消息使用平台编码:

    byte[] input = message.getBytes();
    

    这不是一个好主意,请使用message.getBytes(StandardCharsets.UTF_8)。通过以这种方式指定字符集,您还可以避免不必要的异常处理,因为上述字符集始终存在,因此在编译时会验证正确性。

    【讨论】:

    • 首先,我做了这个更正byte[] input = message.getBytes(StandardCharsets.UTF_8); ,没有任何反应,之后我做了这个byte[] decipheredText = cipher.doFinal(msg.getBytes(StandardCharsets.UTF_8));,又没有,我得到同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2017-08-21
    相关资源
    最近更新 更多