2009-07-21 3 views
5

J'écris un simple utilitaire de cryptage/décryptage rc4 en tant que premier projet. Je suis bloqué en essayant de convertir la chaîne donnée en un tableau d'octets qui peuvent ensuite être manipulés par l'algorithme de base. Comment convertir une chaîne en un tableau d'octets dans f # fonctionnel?F #: Conversion d'une chaîne en un tableau d'octets

//From another thread 
let private replace find (repl : string) (str : string) = str.Replace(find, repl) 

//let private algorithm bytes = blah blah blah 

let Encrypt (decrypted : string) = 
    decrypted.Chars 
    |> Array.map(fun c -> byte.Parse(c)) // This line is clearly not working 
    // |> algorithm 
    |> BitConverter.ToString 
    |> replace "-" "" 

Pour votre information en C#, il ressemble à:

public static string Encrypt(string decrypted) 
    { 
     byte[] bytes = new byte[decrypted.Length]; 

     for (int i = 0; i < decrypted.Length; ++i) 
      bytes[i] = (byte)decrypted[i]; 

     Algorithm(ref bytes); 

     return BitConverter.ToString(bytes).Replace("-", "").ToLower(); 
    } 

Répondre

8

Bien que vous puissiez écrire votre propre fonction pour faire le travail, de son mieux pour coller avec les méthodes .NET intégrées:

chaîne à octets:

System.Text.Encoding.ASCII.GetBytes("hello world!") 

octets à chaîne:

System.Text.Encoding.ASCII.GetString([|104uy; 101uy; 108uy; 108uy; 
      111uy; 32uy; 119uy; 111uy; 114uy; 108uy; 100uy; 33uy|]) 
+0

Il était le premier que je avais besoin. Text.Encoding.ASCII.GetBytes (décrypté) – telesphore4

1

vous pourriez faire une traduction directe

let Encrypt(decrypted : string) = 
    let bytes = Array.init decrypted.Length (fun i -> byte decrypted.[i]) 
    Algorithm(ref bytes) 
    BitConverter.ToString(bytes).Replace("-", "").ToLower() 
+0

Il est utile de voir cette traduction mais j'essaie d'apprendre la manière fonctionnelle de faire les choses. Je ne veux pas écrire F # avec un "accent" C#. – telesphore4

+0

Heureux que vous avez trouvé cela utile. Souhaitez-vous poster votre code final? – gradbot

2

Selon la demande de Gradbot, le code final ressemble à:

1) Je n'aime pas les moulages à l'octet

2) La fonction principale de l'algorithme semble très peu fonctionnelle

La critique constructive est la bienvenue.

Rc4.fs

#light 

open System 
open MiscUtils 
open StringUtils 

let private key = "Mykey"B 
let private byteMask = 0xff 
let private byteMax = byteMask 

let private algorithm (bytes : byte[]) = 
    let mutable j = 0 
    let mutable i = 0 
    let mutable s = [| for c in 0 .. byteMax -> (byte) c |] 

    for i in 0 .. byteMax do 
     j <- (j + (int) (s.[i] + key.[i % key.GetLength(0)])) &&& byteMask 
     Swap (&s.[i]) (&s.[j]) 

    i <- 0 
    j <- 0 
    for x in 0 .. bytes.Length - 1 do 
     i <- (i + 1) &&& byteMask 
     j <- (j + (int) s.[i]) &&& byteMask 
     Swap (&s.[i]) (&s.[j]) 
     let mutable t = (int)(s.[i] + s.[j]) &&& byteMask 
     bytes.[x] <- bytes.[x] ^^^ s.[t] 

    bytes 

let Encrypt (decrypted : string) = 
    Text.Encoding.ASCII.GetBytes decrypted 
    |> algorithm 
    |> BitConverter.ToString 
    |> ToLower 
    |> Replace "-" "" 

let Decrypt (encrypted : string) = 
    [| for i in 0 .. 2 .. encrypted.Length - 1 -> Convert.ToByte(encrypted.Substring(i, 2), 16) |] 
    |> algorithm 
    |> System.Text.Encoding.ASCII.GetString 

StringUtils.Fs

#light 

let Replace find (repl : string) (str : string) = str.Replace(find, repl) 
let ToLower (str : string) = str.ToLower() 

MiscUtils.fs

#light 

let Swap (left : 'a byref) (right : 'a byref) = 
    let temp = left 
    left <- right 
    right <- temp 
Questions connexes