Length of the decryption(RSA) string is changing












0















I have a String which is encrypted with RSA algorithm, When i was trying to decrypt it's working fine.The decryption output will be in byte.



My question is when i trying to convert the byte to a new String(decrypted String) it's getting different length .



If i try with a random string the length is remains same.But if i decrypt the
generated aesKey(AES) the length of the byte is changing while conversion of new String(decrypted String).



Why i am getting different length incase of aesKey decryption???
Is there any difference between "random string" and "generated aeskey"



String ==
"t8xypyI6gKlKTkt4Qec7FCor4EpukZXqYQcIDm6YvbtRB9+YBrX0CqyoHOHN91T8RBQS/JD2osbf4ao9Y"SgNbzhfDa2NpJKMEIBWH4TNlF4Ngb8yWdSm3hz3l8FdeFUIy3pyCxkLjU8n4VAxsmgoIQbgd7DJuPiSMZBA9/IVlcCfo/tZjMtSkezITtoT5aVvLxLaTsp08UREdalvXxb5USKi3cAEdqR9TmLJxB004IMv5Eiuvdmcc3fJzO6mnwiHPuGKArd9LjjiqbPQ75uc8NDOFrvleLc5KwSuThS5Xx7tR1qfoX6qefh6SD7FRk5UzyCEnv+eD+mCQ588Jam1A==";



****The above string is encrypted form of the aesKey(generated by KeyGenerator)



If i decrypt this string by using RSA----



private String decrypt(String text, Key privatekey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte dectyptedText = null;

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.DECRYPT_MODE, privatekey);
dectyptedText = cipher.doFinal(Base64.getDecoder().decode(text));
System.out.println(dectyptedText.length); //32
System.out.println(new String(dectyptedText).length()); //30
System.out.println(new String(dectyptedText).getBytes().length); //60
return new String(dectyptedText);

}


above, The length is changing in byte string conversions.



Suppose if i encrypt and decrypt with a normal string the lengths are not changing????why????










share|improve this question

























  • share the code. I'm not sure what is your problem.

    – piotr szybicki
    Nov 20 '18 at 10:29











  • yes,I shared the code Please look into it once..

    – Ram a Rao
    Nov 20 '18 at 10:59











  • I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

    – James K Polk
    Nov 20 '18 at 14:41


















0















I have a String which is encrypted with RSA algorithm, When i was trying to decrypt it's working fine.The decryption output will be in byte.



My question is when i trying to convert the byte to a new String(decrypted String) it's getting different length .



If i try with a random string the length is remains same.But if i decrypt the
generated aesKey(AES) the length of the byte is changing while conversion of new String(decrypted String).



Why i am getting different length incase of aesKey decryption???
Is there any difference between "random string" and "generated aeskey"



String ==
"t8xypyI6gKlKTkt4Qec7FCor4EpukZXqYQcIDm6YvbtRB9+YBrX0CqyoHOHN91T8RBQS/JD2osbf4ao9Y"SgNbzhfDa2NpJKMEIBWH4TNlF4Ngb8yWdSm3hz3l8FdeFUIy3pyCxkLjU8n4VAxsmgoIQbgd7DJuPiSMZBA9/IVlcCfo/tZjMtSkezITtoT5aVvLxLaTsp08UREdalvXxb5USKi3cAEdqR9TmLJxB004IMv5Eiuvdmcc3fJzO6mnwiHPuGKArd9LjjiqbPQ75uc8NDOFrvleLc5KwSuThS5Xx7tR1qfoX6qefh6SD7FRk5UzyCEnv+eD+mCQ588Jam1A==";



****The above string is encrypted form of the aesKey(generated by KeyGenerator)



If i decrypt this string by using RSA----



private String decrypt(String text, Key privatekey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte dectyptedText = null;

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.DECRYPT_MODE, privatekey);
dectyptedText = cipher.doFinal(Base64.getDecoder().decode(text));
System.out.println(dectyptedText.length); //32
System.out.println(new String(dectyptedText).length()); //30
System.out.println(new String(dectyptedText).getBytes().length); //60
return new String(dectyptedText);

}


above, The length is changing in byte string conversions.



Suppose if i encrypt and decrypt with a normal string the lengths are not changing????why????










share|improve this question

























  • share the code. I'm not sure what is your problem.

    – piotr szybicki
    Nov 20 '18 at 10:29











  • yes,I shared the code Please look into it once..

    – Ram a Rao
    Nov 20 '18 at 10:59











  • I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

    – James K Polk
    Nov 20 '18 at 14:41
















0












0








0








I have a String which is encrypted with RSA algorithm, When i was trying to decrypt it's working fine.The decryption output will be in byte.



My question is when i trying to convert the byte to a new String(decrypted String) it's getting different length .



If i try with a random string the length is remains same.But if i decrypt the
generated aesKey(AES) the length of the byte is changing while conversion of new String(decrypted String).



Why i am getting different length incase of aesKey decryption???
Is there any difference between "random string" and "generated aeskey"



String ==
"t8xypyI6gKlKTkt4Qec7FCor4EpukZXqYQcIDm6YvbtRB9+YBrX0CqyoHOHN91T8RBQS/JD2osbf4ao9Y"SgNbzhfDa2NpJKMEIBWH4TNlF4Ngb8yWdSm3hz3l8FdeFUIy3pyCxkLjU8n4VAxsmgoIQbgd7DJuPiSMZBA9/IVlcCfo/tZjMtSkezITtoT5aVvLxLaTsp08UREdalvXxb5USKi3cAEdqR9TmLJxB004IMv5Eiuvdmcc3fJzO6mnwiHPuGKArd9LjjiqbPQ75uc8NDOFrvleLc5KwSuThS5Xx7tR1qfoX6qefh6SD7FRk5UzyCEnv+eD+mCQ588Jam1A==";



****The above string is encrypted form of the aesKey(generated by KeyGenerator)



If i decrypt this string by using RSA----



private String decrypt(String text, Key privatekey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte dectyptedText = null;

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.DECRYPT_MODE, privatekey);
dectyptedText = cipher.doFinal(Base64.getDecoder().decode(text));
System.out.println(dectyptedText.length); //32
System.out.println(new String(dectyptedText).length()); //30
System.out.println(new String(dectyptedText).getBytes().length); //60
return new String(dectyptedText);

}


above, The length is changing in byte string conversions.



Suppose if i encrypt and decrypt with a normal string the lengths are not changing????why????










share|improve this question
















I have a String which is encrypted with RSA algorithm, When i was trying to decrypt it's working fine.The decryption output will be in byte.



My question is when i trying to convert the byte to a new String(decrypted String) it's getting different length .



If i try with a random string the length is remains same.But if i decrypt the
generated aesKey(AES) the length of the byte is changing while conversion of new String(decrypted String).



Why i am getting different length incase of aesKey decryption???
Is there any difference between "random string" and "generated aeskey"



String ==
"t8xypyI6gKlKTkt4Qec7FCor4EpukZXqYQcIDm6YvbtRB9+YBrX0CqyoHOHN91T8RBQS/JD2osbf4ao9Y"SgNbzhfDa2NpJKMEIBWH4TNlF4Ngb8yWdSm3hz3l8FdeFUIy3pyCxkLjU8n4VAxsmgoIQbgd7DJuPiSMZBA9/IVlcCfo/tZjMtSkezITtoT5aVvLxLaTsp08UREdalvXxb5USKi3cAEdqR9TmLJxB004IMv5Eiuvdmcc3fJzO6mnwiHPuGKArd9LjjiqbPQ75uc8NDOFrvleLc5KwSuThS5Xx7tR1qfoX6qefh6SD7FRk5UzyCEnv+eD+mCQ588Jam1A==";



****The above string is encrypted form of the aesKey(generated by KeyGenerator)



If i decrypt this string by using RSA----



private String decrypt(String text, Key privatekey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte dectyptedText = null;

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.DECRYPT_MODE, privatekey);
dectyptedText = cipher.doFinal(Base64.getDecoder().decode(text));
System.out.println(dectyptedText.length); //32
System.out.println(new String(dectyptedText).length()); //30
System.out.println(new String(dectyptedText).getBytes().length); //60
return new String(dectyptedText);

}


above, The length is changing in byte string conversions.



Suppose if i encrypt and decrypt with a normal string the lengths are not changing????why????







java algorithm security aes rsa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:48







Ram a Rao

















asked Nov 20 '18 at 10:25









Ram a RaoRam a Rao

34




34













  • share the code. I'm not sure what is your problem.

    – piotr szybicki
    Nov 20 '18 at 10:29











  • yes,I shared the code Please look into it once..

    – Ram a Rao
    Nov 20 '18 at 10:59











  • I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

    – James K Polk
    Nov 20 '18 at 14:41





















  • share the code. I'm not sure what is your problem.

    – piotr szybicki
    Nov 20 '18 at 10:29











  • yes,I shared the code Please look into it once..

    – Ram a Rao
    Nov 20 '18 at 10:59











  • I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

    – James K Polk
    Nov 20 '18 at 14:41



















share the code. I'm not sure what is your problem.

– piotr szybicki
Nov 20 '18 at 10:29





share the code. I'm not sure what is your problem.

– piotr szybicki
Nov 20 '18 at 10:29













yes,I shared the code Please look into it once..

– Ram a Rao
Nov 20 '18 at 10:59





yes,I shared the code Please look into it once..

– Ram a Rao
Nov 20 '18 at 10:59













I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

– James K Polk
Nov 20 '18 at 14:41







I cannot make sense of your question. First you're talking about a String, then you're talking about an AES key. It makes no sense to convert an AES key to a String using the new String() constructor.

– James K Polk
Nov 20 '18 at 14:41














1 Answer
1






active

oldest

votes


















1














Likely, the decryptedText contains funny bytes. The documentation says:




public String(byte bytes)



Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.



The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.







share|improve this answer
























  • I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

    – dave_thompson_085
    Nov 21 '18 at 1:18













  • Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

    – Ram a Rao
    Nov 23 '18 at 4:34











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390907%2flength-of-the-decryptionrsa-string-is-changing%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









1














Likely, the decryptedText contains funny bytes. The documentation says:




public String(byte bytes)



Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.



The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.







share|improve this answer
























  • I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

    – dave_thompson_085
    Nov 21 '18 at 1:18













  • Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

    – Ram a Rao
    Nov 23 '18 at 4:34
















1














Likely, the decryptedText contains funny bytes. The documentation says:




public String(byte bytes)



Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.



The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.







share|improve this answer
























  • I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

    – dave_thompson_085
    Nov 21 '18 at 1:18













  • Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

    – Ram a Rao
    Nov 23 '18 at 4:34














1












1








1







Likely, the decryptedText contains funny bytes. The documentation says:




public String(byte bytes)



Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.



The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.







share|improve this answer













Likely, the decryptedText contains funny bytes. The documentation says:




public String(byte bytes)



Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.



The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 20:02









user58697user58697

4,3431717




4,3431717













  • I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

    – dave_thompson_085
    Nov 21 '18 at 1:18













  • Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

    – Ram a Rao
    Nov 23 '18 at 4:34



















  • I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

    – dave_thompson_085
    Nov 21 '18 at 1:18













  • Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

    – Ram a Rao
    Nov 23 '18 at 4:34

















I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

– dave_thompson_085
Nov 21 '18 at 1:18







I'd bet OP's charset is UTF-8. AESkey is effectively random bytes, and statistically nearly half of them are invalid UTF-8; although not spec'ed in reality decoding substitutes U+FFFD which then re-encodes to 3 bytes, so you get a random length near 16+(3x16)=64 bytes. With the actual AES key value destroyed, of course. So don't do that.

– dave_thompson_085
Nov 21 '18 at 1:18















Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

– Ram a Rao
Nov 23 '18 at 4:34





Right, If we convert the String to byte---> byte to String behavior is not changing, if we convert the byte to new String , The length of the byte is changing, it's might effect on the byte.

– Ram a Rao
Nov 23 '18 at 4:34


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390907%2flength-of-the-decryptionrsa-string-is-changing%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$