let encode s pwd = let text = (String.copy s) and l = String.length pwd in for i = 0 to (String.length text) - 1 do text.[i] <- (char_of_int ((int_of_char (text.[i])) lxor (int_of_char (pwd.[i mod l])))) done; text;; (* the following piece of code is not copyrighted by me and cannot be used under terms of my licence *) let check_rep str len = let rec check_p pos1 = if pos1 >= String.length str then true else if str.[pos1] <> str.[pos1 mod len] then false else check_p (pos1 + 1) in check_p 0;; let find_rep str = let rec find_len' len = if check_rep str len then len else find_len' (len+1) in String.sub str 0 (find_len' 1);; (* end of non-copyrighted code *) let decode1 text entext = if ((String.length text) <> (String.length entext)) || ((String.length text) = 0) then failwith "decode: string error"; (find_rep (encode text entext));; let is_symbol c = (* да, я понимаю, что можно вообще обойтись без if, но так понятнее *) let code = int_of_char c in if ((code > 31) && (code < 176) && (code <> 127)) || ((code > 223) && (code < 242)) || (code = 252) then true else false;; let check_text text = let last = (String.length text) - 1 in let rec ct pos = if pos = last then true else if is_symbol text.[pos] then ct (pos + 1) else false in ct 0;; let next_pwd pwd = let rec np pos = if pos = -1 then " " ^ pwd else match int_of_char (pwd.[pos]) with c when (c > 31) && (c < 126) -> (pwd.[pos] <- char_of_int (c + 1)); pwd |c when (c = 126) -> (pwd.[pos] <- char_of_int 128); pwd |c when (c > 127) && (c < 175) -> (pwd.[pos] <- char_of_int (c + 1)); pwd |c when (c = 175) -> (pwd.[pos] <- char_of_int 240); pwd |c when (c > 223) && (c < 241) -> (pwd.[pos] <- char_of_int (c + 1)); pwd |c when (c = 241) -> (pwd.[pos] <- char_of_int 252); pwd |c when (c = 252) -> (pwd.[pos] <- ' '); np (pos - 1) |_ -> failwith "bad password symbol" in np ((String.length pwd) - 1);; let keep_working entext pwd = Printf.printf "Possible password is: %s\nPossible decoded text is: %s\n Dow you wish to keep searching? (y/n): " pwd (encode entext pwd); match read_line () with "y" -> true |"n" -> false |_ -> failwith "Incorrect choice";; let decode2 entext = let rec dec pwd = if check_text (encode entext pwd) then (if keep_working entext pwd then dec (next_pwd pwd) else pwd) else dec (next_pwd pwd) in dec " ";; let text = "This is a text to be encoded.";; print_string ("\n" ^ text);; for i = 0 to (String.length text) - 1 do Printf.printf "%d " (int_of_char text.[i]) done;; print_string "\nEnter password: ";; let encoded = encode text (read_line ());; print_string encoded;; for i = 0 to (String.length encoded) - 1 do Printf.printf "%d " (int_of_char encoded.[i]) done;; print_string "\n";; let password = decode2 encoded;; let decoded = encode encoded password;; Printf.printf "Your password: \"%s\"\nDecoded text: " password decoded;;