+ 14

Why this code output was null character in encrypted & decrypted message place?

In this code was RSA algorithm was used with string inputs. When I run it the "output of encrypted and decrypted message are prints null character". Not working as expected. Where is the problem and how to solve this issue? https://code.sololearn.com/cbc56c87cx0P/?ref=app

24th Jul 2020, 7:58 AM
Azhagesanヾ(✿)
Azhagesanヾ(✿) - avatar
5 Respuestas
+ 6
Jayakrishna🇮🇳 encmsg[i]=pow.... ....fmod(...) These lines take i'th character and encode to encmsg[i] This is RSA algorithm. My last code is convert strings to ASCII value and encode it. These work's on symmetric key method. https://code.sololearn.com/coAcBdd13rvc/?ref=app
24th Jul 2020, 2:26 PM
Azhagesanヾ(✿)
Azhagesanヾ(✿) - avatar
+ 6
Thank you Shadow sir. Lot's of information you shared. I'll try to change empty string and pow method. If I'm can't solve further mention here.
24th Jul 2020, 2:32 PM
Azhagesanヾ(✿)
Azhagesanヾ(✿) - avatar
+ 2
May I know what is the purpose of these statements? encmsg[i]=pow(msg[i],e); encmsg[i]=fmod(encmsg[i],n); Your taking msg as string so may charecters range exceeding...! Edit :AZHAGESAN S The algorithm working on numbers only with some modifications. For strings, which algarithm your following..? Same algarithm yelding empty characters... Not visible..
24th Jul 2020, 12:52 PM
Jayakrishna 🇮🇳
+ 2
I haven't been able to make the algorithm work yet, so I might update my answer later with a working implementation, but for now, there are some things to get you started: 1. I think you are calculating 'd' in the wrong way, at least it doesn't seem to fit the formular mentioned in the comment. 2. Regarding the for loops, C++ strings don't end with the null character '\0' like C strings, so you might iterate more characters than there are in the string. To do it properly, either use the string size: for ( ...; i < msg.length(); ... ) {} or simpler, a short for loop: for ( char c : msg ) {} 3. The strings "encmsg" and "decmsg" are initially empty, so the [] operator operates outside of its boundaries. This is a problem because the string doesn't "know" you had it grow, and thus is still empty after the loop has finished. A simple solution would be to assign "msg" to "encmsg" during initialization and operate on it directly (likewise assign "encmsg" to "decmsg"). ...
24th Jul 2020, 1:26 PM
Shadow
Shadow - avatar
+ 2
4. Splitting exponentiation and modulo operation is a problem because the value is implicitely converted to char and back to double between the operations, as a consequence you lose precision since the conversion not only truncates the floating point part, but also maps the value to the range of a character (-127 ~ 127). You should merge the two lines: ... = fmod( pow( ... ), n ); 5. Storing the encrypted message in a string potentially yields the same problem, because as soon as 'n' surpasses the upper char value range (127), values might get mapped to the char range, so you wouldn't be able to recover them anymore during decryption. 6. As you choose bigger prime numbers, the computed numbers will grow bigger rapidly, so you should always look out for a possible overflow. I don't see an issue right now, apart from the char conversion mentioned earlier, but it is something to look out for.
24th Jul 2020, 1:34 PM
Shadow
Shadow - avatar