RSA asymmetric encryption to be encrypted data length problem

The RSA algorithm specifies:

The number of bytes to be encrypted cannot exceed the length of the key divided by 8 minus 11 (ie: KeySize / 8 - 11),

The number of bytes in the ciphertext obtained after encryption is exactly the length of the key divided by 8 (ie: KeySize / 8).

 

The default key length of the RSA algorithm in Java is 1024.

If the number of bytes of data to be encrypted is greater than (1024 / 8 - 11 = 128 - 11 = 117), the encryption fails and the following exception is thrown:

Data must not be longer than 117 bytes

 

Example

/**
     * Use public key encryption
     * @param key encryption key
     * @param yuanwen original
     * @return ciphertext
     * @throws Exception
     */
    public static String encryptWithPublicKey(String yuanwen){  
        Cipher cipher = null;  
        try {
        	byte[] plainTextData = yuanwen.getBytes("UTF-8");
        	System.out.println("Number of bytes to be encrypted: "+plainTextData.length);
			cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
			byte[] output = cipher.doFinal(plainTextData);  
			System.out.println("Number of ciphertext bytes after encryption: "+output.length);
			return Base64.encodeBytes(output);
		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
			e.printStackTrace ();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace ();
		} catch (BadPaddingException e) {
			e.printStackTrace ();
		} catch (InvalidKeyException e) {
			e.printStackTrace ();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace ();
		}  
        return null;
    }  

 

public static void main(String[] args) throws Throwable {

		String a = "The day is all over the mountains, the Yellow River flows into the sea, and I want to see a thousand miles and go up a higher floor. Take it, take it, take it, take it, take it, take it, take it, take it";
		String b = RSAUtil.encryptWithPublicKey(a);
		System.out.println(b);
	}

 

output result

Number of bytes to be encrypted: 117

Number of ciphertext bytes after encryption: 128

 

public static void main(String[] args) throws Throwable {

		String a = "When the sun is over the mountains, the Yellow River flows into the sea, and I want to see a thousand miles away and go up a higher floor. Take it, take it, take one
		String b = RSAUtil.encryptWithPublicKey(a);
		System.out.println(b);
	}

 

Output result:

Number of bytes to be encrypted: 118

javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes

 

 

There are two common solutions to the super-long problem.

First, the encrypted data is split into many small fragments, and when decrypted, it is also decrypted in segments.

Second, in combination with the symmetric encryption algorithm, use RSA to encrypt the key of the symmetric encryption algorithm.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326319915&siteId=291194637