cipher result different from M2Crypto and command line? #30

rnons opened this Issue May 1, 2014 · 3 comments


None yet
3 participants

rnons commented May 1, 2014

I'm writing a program to interface with another program written in python. I find that the ciphered result are different.

In ghci, the ciphered result is \GS\n\197:

import OpenSSL
import OpenSSL.EVP.Cipher 
import Data.Maybe
method <- fmap fromJust $ withOpenSSL $ getCipherByName "bf-cfb"
cipher method "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72" "\xcc\x88\xa5\x26\x85\xaf\x7f\x8d" Encrypt "abcd"

In python, the ciphered result is K\x10<Q

import M2Crypto
M2Crypto.EVP.Cipher("bf_cfb", "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72", "\xcc\x88\xa5\x26\x85\xaf\x7f\x8d", 1).update("abcd")

In shell, with abcd.txt containing the string "abcd", abcd.bin shown in vim as K^P<Q}

openssl bf-cfb -in abcd.txt -out abcd.bin -pass pass:abc -K 900150983cd24fb0d6963f7d28e17f72 -iv cc88a52685af7f8d -nosalt

I assume the M2Crypto result is equivalent to command line result, why the HsOpenSSL result is different?

Use ByteString instead of String for non-ASCII key and IV.

clowwindy@clowwindy-debian:~/test_openssl$ cat hs.hs
import OpenSSL
import OpenSSL.EVP.Cipher 
import Data.Maybe
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import Data.ByteString.Lazy
import Data.ByteString.Base16

main = do
  method <- fmap fromJust $ withOpenSSL $ getCipherByName "bf-cfb"
  let key = B8.pack "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72"
  let iv = B8.pack "\xcc\x88\xa5\x26\x85\xaf\x7f\x8d"
  let plain = L8.pack "abcdefgh"
  cipherText <- cipherStrictLBS method key iv Encrypt plain
  print $ encode $ B8.pack $ L8.unpack cipherText

clowwindy@clowwindy-debian:~/test_openssl$ cat 
import M2Crypto
print M2Crypto.EVP.Cipher("bf_cfb", "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72", "\xcc\x88\xa5\x26\x85\xaf\x7f\x8d", 1).update("abcdefgh").encode('hex')

clowwindy@clowwindy-debian:~/test_openssl$ ghc hs.hs && ./hs && python 
[1 of 1] Compiling Main             ( hs.hs, hs.o )
Linking hs ...

rnons commented Jun 13, 2014

@clowwindy Great, thank you very much!

@rnons rnons closed this Jun 13, 2014


depressed-pho commented Jul 13, 2014

Sorry for the late reply. @clowwindy is right and I deprecated those functions taking String in HsOpenSSL-0.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment