英文:
How to remove little-endian byte 0 in Java
问题
以下是翻译好的部分:
#C 在加密之前对myValue进行编码。
byte[] myBytes = myValue.getBytes(StandardCharsets.UTF_16);
byte[] code = cryptoClient.encrypt(EncryptionAlgorithm.RSA_OAEP, myBytes);
String encryptedmyString = Base64.getEncoder().encodeToString(code);
我需要在Java中解密它。
byte[] code = Base64.getDecoder().decode(encryptedmyString);
DecryptResult decryptionResult = cryptoClient.decrypt(EncryptionAlgorithm.RSA_OAEP, code);
String result = new String(decryptionResult.getPlainText(), StandardCharsets.UTF_8);
在每个字节后面出现了额外的0。我找到了关于小端字节序的这篇文章:https://stackoverflow.com/questions/32585671/why-encoding-unicode-getbytes-returns-an-additional-0
如何在Java中去掉这些额外的0?
**更新**:
我不是C#开发者。我不能更改他们的代码。
如果我在解密后尝试以下操作,它会给我错误的结果:"搀攀瘀栀最愀瀀瀀猀琀漀爀愀最攀戀氀漀戀"
new String(decryptionResult.getPlainText(), StandardCharsets.UTF_16);
英文:
#C encodes original myValue before encryption.
var myBytes = Encoding.Unicode.GetBytes(myValue);
var encryptedResult = cryptoClient.Encrypt(EncryptionAlgorithm.RsaOaep, myBytes);
var encryptedmyString = Convert.ToBase64String(encryptedResult.Ciphertext);
I need to decrypt it in Java.
byte[] code = Base64.getDecoder().decode(encryptedmyString);
DecryptResult decryptionResult = cryptoClient.decrypt(EncryptionAlgorithm.RSA_OAEP, code);
String result = new String(decryptionResult.getPlainText(), StandardCharsets.UTF_8);
result has 0 after each byte. I found out this article about little-endian byte https://stackoverflow.com/questions/32585671/why-encoding-unicode-getbytes-returns-an-additional-0
How to remove those extra 0 in Java?
Updated:
I am not #C developer. I can't change their code.
If I tried this after decryption, it gives me wrong "搀攀瘀栀最愀瀀瀀猀琀漀爀愀最攀戀氀漀戀"
new String(decryptionResult.getPlainText(), StandardCharsets.UTF_16);
答案1
得分: 1
感谢JosefZ在评论中的回答。它有效:
new String(decryptionResult_.getPlainText(), StandardCharsets.UTF_16LE);
英文:
Thanks for JosefZ's answer in the comment. It works
new String(decryptionResult_.getPlainText(), StandardCharsets.UTF_16LE);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论