2009-07-21 15 views
5

私は最初のプロジェクトとして単純なrc4暗号化/復号化ユーティリティを書いています。私は、指定された文字列をコアアルゴリズムによって操作できるバイトの配列に変換しようとしています。どのように文字列を機能的なf#のバイト配列に変換しますか?あなたは組み込みの.NETメソッドに固執し、その最高の仕事をするために、独自の機能を書くことができますがF#:文字列をバイト配列に変換する

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(); 
    } 

答えて

8

//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 "-" "" 

はFYI C#で、それは次のようになります

バイト文字列:文字列へ

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

バイト:

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

それは私が必要な最初のものでした。 Text.Encoding.ASCII.GetBytes(復号化) – telesphore4

1

あなたが直接翻訳にGradbotの要求を1として

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

この翻訳を見ると便利ですが、私は物事を行うための機能的な方法を学ぼうとしています。私はF#でC#の "アクセント"を書いたくありません。 – telesphore4

+0

これはうれしい使い方です。あなたは最終的なコードを投稿しますか? – gradbot

2

を行うことができ、最終的なコードは次のようになります。

1)私は

2バイトをキャストを好きではありません)主要なアルゴリズム関数は非常に機能しないように見える

建設的な批評は大歓迎です。

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 
関連する問題