Java private/public encrypt exception data must not be greater than 256 bytes
I have this code below for encrypting objects, which does a job.
But I have an object containing a list of another object. When I encrypt the object I have the error that data must be greater than 256 bytes. When I remove the list, the encryption is managed without error.
public class A {
private List<B> b = new ArrayList<>();
//getter setter
}
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(message.getBytes());
} catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException
| IllegalBlockSizeException | InvalidKeySpecException | InvalidKeyException e) {
e.printStackTrace();
}
java public-key-encryption
add a comment |
I have this code below for encrypting objects, which does a job.
But I have an object containing a list of another object. When I encrypt the object I have the error that data must be greater than 256 bytes. When I remove the list, the encryption is managed without error.
public class A {
private List<B> b = new ArrayList<>();
//getter setter
}
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(message.getBytes());
} catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException
| IllegalBlockSizeException | InvalidKeySpecException | InvalidKeyException e) {
e.printStackTrace();
}
java public-key-encryption
add a comment |
I have this code below for encrypting objects, which does a job.
But I have an object containing a list of another object. When I encrypt the object I have the error that data must be greater than 256 bytes. When I remove the list, the encryption is managed without error.
public class A {
private List<B> b = new ArrayList<>();
//getter setter
}
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(message.getBytes());
} catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException
| IllegalBlockSizeException | InvalidKeySpecException | InvalidKeyException e) {
e.printStackTrace();
}
java public-key-encryption
I have this code below for encrypting objects, which does a job.
But I have an object containing a list of another object. When I encrypt the object I have the error that data must be greater than 256 bytes. When I remove the list, the encryption is managed without error.
public class A {
private List<B> b = new ArrayList<>();
//getter setter
}
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(message.getBytes());
} catch (NoSuchPaddingException | NoSuchAlgorithmException | BadPaddingException
| IllegalBlockSizeException | InvalidKeySpecException | InvalidKeyException e) {
e.printStackTrace();
}
java public-key-encryption
java public-key-encryption
edited Nov 21 '18 at 11:40
Lajos Arpad
27.4k1861116
27.4k1861116
asked Nov 21 '18 at 9:27
boycod3
1,32221854
1,32221854
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
RSA is not suitable for encrypting more than limited amounts of data. The conventional solution is hybrid encryption where the data is encrypted with a symmetric cipher with a nonce key and RSA is used to encrypt (only) the key. See for examples PKCS7/CMS/SMIME, PGP, XMLenc, JWE, SSL/TLS, and SSH.
Dupes here and cross-Stack:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408888%2fjava-private-public-encrypt-exception-data-must-not-be-greater-than-256-bytes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
RSA is not suitable for encrypting more than limited amounts of data. The conventional solution is hybrid encryption where the data is encrypted with a symmetric cipher with a nonce key and RSA is used to encrypt (only) the key. See for examples PKCS7/CMS/SMIME, PGP, XMLenc, JWE, SSL/TLS, and SSH.
Dupes here and cross-Stack:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa
add a comment |
RSA is not suitable for encrypting more than limited amounts of data. The conventional solution is hybrid encryption where the data is encrypted with a symmetric cipher with a nonce key and RSA is used to encrypt (only) the key. See for examples PKCS7/CMS/SMIME, PGP, XMLenc, JWE, SSL/TLS, and SSH.
Dupes here and cross-Stack:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa
add a comment |
RSA is not suitable for encrypting more than limited amounts of data. The conventional solution is hybrid encryption where the data is encrypted with a symmetric cipher with a nonce key and RSA is used to encrypt (only) the key. See for examples PKCS7/CMS/SMIME, PGP, XMLenc, JWE, SSL/TLS, and SSH.
Dupes here and cross-Stack:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa
RSA is not suitable for encrypting more than limited amounts of data. The conventional solution is hybrid encryption where the data is encrypted with a symmetric cipher with a nonce key and RSA is used to encrypt (only) the key. See for examples PKCS7/CMS/SMIME, PGP, XMLenc, JWE, SSL/TLS, and SSH.
Dupes here and cross-Stack:
Encrypting large files using a public key
How to encrypt a large file in openssl using public key
https://crypto.stackexchange.com/questions/14/how-can-i-use-asymmetric-encryption-such-as-rsa-to-encrypt-an-arbitrary-length
https://crypto.stackexchange.com/questions/3608/why-is-padding-used-for-rsa-encryption-given-that-it-is-not-a-block-cipher
https://crypto.stackexchange.com/questions/5782/why-is-asymmetric-cryptography-bad-for-huge-data
https://crypto.stackexchange.com/questions/10685/hybrid-encryption-with-rsa-and-aes-versus-spliting-into-multiple-rsa-messages
https://crypto.stackexchange.com/questions/25899/using-ecb-as-rsa-encryption-mode-when-encrypted-messages-are-unique
https://security.stackexchange.com/questions/37581/why-does-pgp-use-symmetric-encryption-and-rsa
answered Nov 21 '18 at 11:54
dave_thompson_085
13k11631
13k11631
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408888%2fjava-private-public-encrypt-exception-data-must-not-be-greater-than-256-bytes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown