At least I think I need a string (;)) since: 1. The input to a RSA encryption is typically a string to be encrypted. This string is converted to a large integer which is then encrypted. This large integer is then dumped to a string so that it can be transmitted, saved to file etc.
2. The PKCS standard/spec describes the i2sop function and what it needs to do (http://tools.ietf.org/html/rfc3447#section-4.1). I'd like to find a nice way to implement it in Julia... :) I guess I could send around Uint8[] internally and then dump it out in a "transport string" (Base64?) when it needs to be saved and communicated over sockets... Cheers, Robert Den tisdagen den 11:e mars 2014 kl. 15:17:04 UTC+1 skrev Ivar Nesje: > > In Julia you should not use strings to encode binary data, as you have to > do in C. Strings in Julia is just a wrapper around an `Array{Uint8,1}` with > some extra validation. As you do not want the validation, you should > probably just use the array directly. > > Why do you think you need a String? > > Ivar > > kl. 11:37:35 UTC+1 tirsdag 11. mars 2014 skrev Robert Feldt følgende: >> >> Implementing simple RSA crypto in pure Julia (not for actual >> sec-sensitive use) but for low-sec applications. But I have troubles with >> encoding strings as integers and back. The PKCS#1 crypto standard says that >> strings should be seen as 8-bit (octet) strings. I tried creating a Uint8[] >> with the byte values and converting to ASCIIString but that fails when the >> values are more than 7 bits. However, I cannot just convert to UTF8String >> instead since those might not be valid either. >> >> Ideas for how to do this cleanly? Current code below... Thanks! >> >> # Convert a non-negative integer i into an octet string. >> function i2osp(x::Integer, len = nothing) >> if typeof(len) <: Integer && (x >= 256^len) >> throw("integer is too large") >> end >> >> if x < 0 >> throw("integer is negative") >> end >> >> bytes = Uint8[] >> while x > 0 >> b = uint8(x & 0xff) >> push!(bytes, b) >> x = x >>> 8 >> end >> str = convert(ASCIIString, reverse(bytes)) # Fails if any byte value > >> 127 >> >> if typeof(len) <: Integer && (length(str) < len) >> str = repeat("\0", len - str) * str >> end >> >> return str >> end >> >>
