OCaml: Encryption
Symmetric key encryption using libcryptgps library:
open ExtLib
open Crypt_blowfish
let iv = (0, 0, 0, 0)
let key = ref (Cryptsystem.prepare "secret")
let init passphrase =
key := Cryptsystem.prepare passphrase
let encrypt s =
let iv', i', s' = Cryptmodes.encrypt_cfb64 !key iv 0 s in
Base64.str_encode s'
let decrypt s =
let bin = Base64.str_decode s in
let iv', i', s' = Crypt_blowfish.Cryptmodes.decrypt_cfb64 !key iv 0 bin in
s'
let test () =
let o = "abc" in
let e = encrypt o in
let d = decrypt e in
print_endline ("encryption test: " ^ o ^ " -> " ^ e ^ " -> " ^ d);
assert (o = d)
Build it with "-I +cryptgps cryptgps.cmxa -I +extlib extLib.cmxa" options; note that extlib is used for base64 encoding.