简体   繁体   中英

java get the plaintext in RSA from decryption

I'm integrating RSA encryption in an app using java. But I have 2 problems here.

I followed all steps according to RSA algorithm.

For example:

I generated prime numbers p and q, then I calculated them to get encryption and decryption key.

p = 71, q = 73, n = 5183

Encryption key: (e, n) = (53, 5183)

Decryption key: (d, n) = (1997, 5183)

I used plaintext: Save The Queen

Then, I convert all letters into number according to ASCII.

So, the converting numbers: 839711810132841041013281117101101110

After that, I divide all numbers into n-digits blocks. Let's say 3-digits blocks.

So, dividing numbers must be: 839 711 810 132 841 041 013 281 117 101 101 110

For encryption, I got ciphertext numbers: 4451 853 2353 1269 1946 4583 1726 4643 208 1665 1665 3360

After that, I decrypted the ciphertext. I got plaintext numbers: 839 711 810 132 841 41 13 281 117 101 101 110

At a glance, it looks working well. But actually, it doesn't.

Take a look for 6th block. It should be 041 , but it appears 41 . This is the first problem. How to fix this?

After getting plaintext numbers from decryption, I must combine all plaintext numbers then convert them into letters.

It's the second problem: I confuse how to convert the plaintext numbers into plaintext letters.

I confuse because I don't have delimiters to separate numbers.

So, the plaintext must be successfully converted to: Save The Queen

What can I do to solve the them?

RSA works on numbers. This is different than eg AES that works on bits and bytes. So, during the calculation, 041 is identical to 41 . However, you explicitly split your plaintext up into 3 digit values. So the only thing you have to do is to add 0 characters to the left until you have 3 digits. The only thing left is the final number. You could pad with zeros and add a final (encrypted?) value that specifies how many zero's were added at the end.

Now there is another problem; you don't have delimiters. This can be solved by using the octal encoding of the characters instead. Check this ASCII table and look under Octal. Additional advantage in this case is that you get one character per decrypted value.

Obviously you could also left pad each decimal representation with zero's until you get 3 decimal characters per character in "Save The Queen".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM