Created
June 14, 2013 12:51
-
-
Save nostrict/5781559 to your computer and use it in GitHub Desktop.
Blowfish example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/go.crypto/blowfish" | |
"crypto/cipher" | |
"encoding/hex" | |
"fmt" | |
) | |
func EncryptBlowfish(dst, src, key, iv []byte) error { | |
bBlockEncrypter, err := blowfish.NewCipher([]byte(key)) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
bEncrypter := cipher.NewCFBEncrypter(bBlockEncrypter, iv) | |
bEncrypter.XORKeyStream(dst, src) | |
return nil | |
} | |
func DecryptBlowfish(dst, src, key, iv []byte) error { | |
bBlockDecrypter, err := blowfish.NewCipher([]byte(key)) | |
if err != nil { | |
fmt.Println(err) | |
return err | |
} | |
bDecrypter := cipher.NewCFBDecrypter(bBlockDecrypter, iv) | |
bDecrypter.XORKeyStream(dst, src) | |
return nil | |
} | |
func main() { | |
key := "secret key" | |
msg := "ultra secret message" | |
var iv = []byte(key)[:blowfish.BlockSize] | |
var err error | |
encrypted := make([]byte, len(msg)) | |
decrypted := make([]byte, len(msg)) | |
err = EncryptBlowfish(encrypted, []byte(msg), []byte(key), iv) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(encrypted) | |
fmt.Println(hex.EncodeToString(encrypted)) | |
err = DecryptBlowfish(decrypted, encrypted, []byte(key), iv) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(decrypted)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment