RSA使用Java的加密

RSA使用Java的加密

本文介绍了RSA使用Java的加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的程序使用RSA算法在Java中加密/解密。我创建一个密码对象如下:

I am doing a simple program to encrypt/decrypt using RSA algorithm in Java. I create a cipher object as follows:

//Create a Cipher object
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");

我通过调用加密函数进行加密:

I do the encryption by calling the encrypt function:

String cipher=encrypt(textByte, pair, rsaCipher);
System.out.println("The Encryption using RSA Algorithm : "+cipher);

解密为:

//Decryption
String plain=decrypt(Base64.decodeBase64(cipher),pair, rsaCipher);
System.out.println("The Decryption using RSA Algorithm : "+plain);

当我显示输出时,解密输出在原始文本之前返回一个长空格:

When I display the output, the decryption output returns a long space before the original text:

但是,当我编辑用于创建Cipher对象的代码为:
//创建Cipher对象
Cipher rsaCipher = Cipher.getInstance(RSA);

However, when I edit the code for creating the Cipher object to be: //Create a Cipher object Cipher rsaCipher = Cipher.getInstance("RSA");

即,删除操作模式和填充参数,问题得到解决,输出变为:

i.e, removed the operation mode and padding arguments, the problem get resolved and the output becomes:

问题在哪里。在第一种情况(当空间出现时),我指定NoPadding?为什么空间出现在解密的消息中?即使我使用填充,我预计这不应该发生。

Where is the problem. In the first case (when the space appears), I specified NoPadding? Why the spaces appears in the decrypted message ? Even if I used padding, I expect this should not happen.

EDIT:
这是加密和解密方法:

This is the encrypt and decrypt methods:

public static String encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
    //get the public key
    PublicKey pk=pair.getPublic();


    //Initialize the cipher for encryption. Use the public key.
    rsaCipher.init(Cipher.ENCRYPT_MODE, pk);

    //Perform the encryption using doFinal
    byte[] encByte = rsaCipher.doFinal(textBytes);

    // converts to base64 for easier display.
    byte[] base64Cipher = Base64.encodeBase64(encByte);

    return new String(base64Cipher);
}//end encrypt

public static String decrypt(byte[] cipherBytes, KeyPair pair, Cipher rsaCipher) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException
{
    //get the public key
    PrivateKey pvk=pair.getPrivate();

    //Create a Cipher object
    //Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");

    //Initialize the cipher for encryption. Use the public key.
    rsaCipher.init(Cipher.DECRYPT_MODE, pvk);

    //Perform the encryption using doFinal
    byte[] decByte = rsaCipher.doFinal(cipherBytes);

    return new String(decByte);

}//end decrypt


推荐答案

p>你的问题确实是与填充。为了安全的RSA功能,需要一些种类的填充,实际上PKCS#1 1.5或OAEP填充。此外,需要找到加密明文的开始和结束。

Your problem is indeed with the padding. Some kind of padding, either PKCS#1 1.5 or OAEP padding in practice, is required for secure RSA functionality. Furthermore, it is required to find the start and end of the encrypted plain text.

RSA的模幂运算使用大整数执行。这些操作的结果然后被表示为八位字节串。这些字节串基本上是大端,无符号,固定长度表示的整数。这些整数用 00 值字节填充(这在RSA标准中称为I2OS原语)。所以你看到的是模幂运算的结果, 00 padding仍然存在。

The modular exponentiation of RSA is performed using large integers. The results of these operations are then represented as octet strings. These octet strings are basically big endian, unsigned, fixed length representation of an integer. These integers are left padded with 00 valued bytes (this is called the I2OS primitive in the RSA standard). So what you are seeing is the result of the modular exponentiation, with the 00 padding still in place.

Long故事短,总是使用填充方案。如今,OAEP将是更可取的。将它与混合加密方案一起使用,或者使用更高级别的容器格式(例如CMS或PGP)。

Long story short, always use a padding scheme. Nowadays, OAEP would be preferable. Use it together with hybrid encryption scheme, or use a higher level container format such as CMS or PGP.

这篇关于RSA使用Java的加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:28