tags:

views:

137

answers:

1

I'm using the NTL library to implement ElGamal encryption/decryption algorithm. I've got it to the point that it's working but the algorithm wants the message to be converted to integers so it can be encrypted.
So if i input a number like 1234 everything works ok but how would i go to be able to convert a C++ string (std::string) to a ZZ number and then back from that ZZ number to a string?

LE:

ZZ it's a class that represent a large number.
Ex: 18287348238476283658234881728316274273671623781254124517353

So basically i'm looking to take "Hello World" for example and run it char by char and get the ascii code of the chars so i'll get a number: "72 101 108 108 111 32 87 111 114 108 100"
And then i need to convert this number back to string "Hello World"

Or maybe there's a better way.

A: 

By far the easiest solution is to realize that you can convert chars to unsigned ints, and unsigned ints to ZZ numbers. Then treat the string as a base-256 number. For instance, "abc" would be 256*"ab" + "c", or 65536 * "a" + 256 * "b" + "c", or ((ZZ(unsigned('a')*256) + ZZ(unsigned('b'))*256) + ZZ(unsigned('c')),

MSalters
and on the other end, when i have the number how to convert it back to string?
daniels
iterate `nextchar=val % 256;val = val / 256;` until val is 0. This, admittedly, builds the string backwards, but it's just a case of reversing the string.
Vatine
True - you get to pick once whether you want little-endian or big-endian, the other conversion will just have to match that. Both endiannnesses are equally valid, and it makes sense to match your platform convention.
MSalters
Exactly what i was looking for. Works like a charm. Thank you both MSalters and vatine
daniels