0

I am working on implementing the RSA encryption system for a class or mine and to be able to implement it we obviously would need to convert strings to integers and then the reverse of that. Using basic level haskell can someone explain to me how this might be done. I have a function that will tell me the max length that a character code can be for your consideration as well.

Thank you everyone.

5
  • Can you please provide some examples on what conversions do you need? Commented Mar 8, 2014 at 22:44
  • What do you mean with convert? Like "123" -> 123 och something else? Commented Mar 8, 2014 at 22:53
  • I mean something like "Word" -> some number then some number back to "Word" Commented Mar 8, 2014 at 23:02
  • If you intend to encode the message directly with RSA, you're using RSA very incorrectly. Commented Mar 8, 2014 at 23:07
  • @WombatCombat: What if the resulting number is larger than the modulus? Then you can't encrypt it. Usually with RSA you use a hybrid approach. You use RSA to share a key and use that key to encrypt the message symmetrically Commented Mar 8, 2014 at 23:25

2 Answers 2

4

I am not sure exactly what you need, but if you are just trying to get a numerical representation of a String, you can use Data.Char.ord which will convert a Char to an Int. Then you can use Data.Char.chr to convert each Int back to a Char.

Using:

import Data.Char (ord, chr)

stringsToInts :: String -> [Int]
stringToInts = map ord

intsToString :: [Int] -> String
intsToString = map chr

you can:

Prelude Data.Char> let ints = stringsToInts "Hello world!"
Prelude Data.Char> ints
[72,101,108,108,111,32,119,111,114,108,100,33]
Prelude Data.Char> intsToString ints
"Hello world!"

You could then further process the list ints if that is what you need (and further modify intsToString.)

Sign up to request clarification or add additional context in comments.

3 Comments

Pretty much exactly what I was looking for thank you! More input is welcome though guys!
This is what I tried with no luck though :( str2Integer :: [Char] -> Integer str2Integer = map ord
ord returns an Int which is not the same type as Integer. You should use str2Integer = map (toInteger . ord). There is also a corresponding function fromInteger
0

You may also use the ByteString module: convert a simple String into a ByteString with pack and then directly work with it as an array of bytes (Word8's, if to be exact).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.