2013-10-22 2 views
0

http://hackage.haskell.org/package/cryptocipher-0.6.1/docs/Crypto-Cipher.html을 사용하여 ByteString을 암호화하고 싶습니다. 여기 내 미안 시도이다 : 나는 어떤 영리한 방식으로 makeKeycipherInit를 사용해야 할 것 같습니다AES ECB 모드에서 cryptocipher를 사용하여 암호화하는 방법은 무엇입니까?

import Crypto.Cipher 
import Crypto.Cipher.AES (AES128) 
import Crypto.Cipher.Types 
import qualified Data.ByteString.Char8 as B 

key = B.pack "It a 128-bit key" 

main = do 
    let ctext = ecbEncrypt key (B.pack "16 bytte ssecret") 
    print (B.unpack ctext) 

하지만 하스켈 초보자로서, 나는 makeKey을 사용하는 방법을 알아 내기 위해 사투를 벌인거야.

짧은 샘플 블록이나 올바른 방향을 가리키는 점에 깊이 감사드립니다.

+1

FYI ECB는 매우 안전하지 않습니다. – Wes

답변

3

ecbEncrypt 유형을 확인해야합니다. cipher (귀하의 경우 AES128) 및 ByteString이 필요합니다. ciphercipherInit으로 제조 할 수 있으며, Key cipher이 필요합니다. KeymakeKey으로 만들어지며, ToSecureMem (ByteString)의 인스턴스를 취해 Either KeyError (Key cipher)을 반환합니다. 오류를 무시하면 Right 생성자의 패턴 일치만으로 키를 얻을 수 있습니다. 다음과 같이 코드 샘플은 같습니다

import Crypto.Cipher 
import Crypto.Cipher.Types 
import qualified Data.ByteString.Char8 as B 

keyString = B.pack "It a 128-bit key" 

Right key = makeKey keyString 

aes128 :: AES128 
aes128 = cipherInit key 

ptext = B.pack "16 bytte ssecret" 

ctext = ecbEncrypt aes128 ptext 

main = putStrLn $ B.unpack ctext 

것은 내가 컴파일러가 사용하는 암호화하기 자체적으로 알 수 없습니다 직접 때문에 여기 aes128의 유형을 지정했습니다.

관련 문제