本文整理汇总了Golang中crypto/cipher.NewCFBEncrypter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCFBEncrypter函数的具体用法?Golang NewCFBEncrypter怎么用?Golang NewCFBEncrypter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCFBEncrypter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: encrypt
// encrypt string to base64 crypto using AES
func encrypt(key []byte, text string) string {
// key := []byte(keyText)
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
log.Error(err)
return ""
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
log.Error(err)
return ""
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// convert to base64
return base64.URLEncoding.EncodeToString(ciphertext)
}
示例2: AESCtyptTest
func AESCtyptTest() {
fmt.Println("AES测试")
//需要去加密的字符串
plaintext := []byte("My name is daniel")
//aes的加密字符串
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
fmt.Println(len(key_text))
// 创建加密算法aes
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
return
}
//加密字符串
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
// 解密字符串
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
plaintextCopy := make([]byte, len(plaintext))
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
}
示例3: Close
// Method Close encrypts and writes the encrypted data to the key file
func (k *KeyStore) Close(secret []byte) {
// Encode a gob of the keystore
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
enc.Encode(k)
buffString := buff.String()
// Encrypt the data
block, err := aes.NewCipher(secret)
if err != nil {
panic(err)
}
ciphertext := make([]byte, aes.BlockSize+len(buffString))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(buffString))
// Write the encrypted data to the file
kFile, err := os.OpenFile(kf, os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
bytesWritten, err := kFile.Write(ciphertext)
if err != nil || bytesWritten == 0 {
panic(err)
}
}
示例4: AesEncryptData
func AesEncryptData(data, key []byte, ctp string) []byte {
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
data_enc := make([]byte, aes.BlockSize+len(data))
iv := data_enc[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil
}
var stream cipher.Stream
switch ctp {
case "cfb":
stream = cipher.NewCFBEncrypter(block, iv)
case "ctr":
stream = cipher.NewCTR(block, iv)
default:
stream = cipher.NewOFB(block, iv)
}
stream.XORKeyStream(data_enc[aes.BlockSize:], data)
// It's important to remember that ciphertexts must be authenticated
// (i.e. by using crypto/hmac) as well as being encrypted in order to
// be secure.
return data_enc
}
示例5: encodeTicket
func (self *defaultAuther) encodeTicket(userdata string, expiryMinutes int64) string {
// generate the token uuid|userdata|expiry
uuid := Uuid()
time := time.Now().Add(time.Duration(expiryMinutes) * time.Minute).Format(time.RFC3339)
token := fmt.Sprintf("%s|%s|%s", uuid, userdata, time)
// encrypt the token
block, blockError := aes.NewCipher(self.key)
if nil != blockError {
return ""
}
bytes := []byte(token)
encrypted := make([]byte, len(bytes))
encrypter := cipher.NewCFBEncrypter(block, self.key[:aes.BlockSize])
encrypter.XORKeyStream(encrypted, bytes)
return hex.EncodeToString(encrypted)
}
示例6: main
func main() {
plainText := []byte("Bob loves Alice.")
key := []byte("passw0rdpassw0rdpassw0rdpassw0rd")
// Create new DES cipher block
block, err := aes.NewCipher(key)
if err != nil {
fmt.Printf("err: %s\n", err)
}
// The IV (Initialization Vector) need to be unique, but not secure.
// Therefore, it's common to include it at the beginning of the cipher text.
cipherText := make([]byte, aes.BlockSize+len(plainText))
// Create IV
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
fmt.Printf("err: %s\n", err)
}
// Encrypt
encryptStream := cipher.NewCFBEncrypter(block, iv)
encryptStream.XORKeyStream(cipherText[aes.BlockSize:], plainText)
fmt.Printf("Cipher text: %v\n", cipherText)
// Decrpt
decryptedText := make([]byte, len(cipherText[aes.BlockSize:]))
decryptStream := cipher.NewCFBDecrypter(block, cipherText[:aes.BlockSize])
decryptStream.XORKeyStream(decryptedText, cipherText[aes.BlockSize:])
fmt.Printf("Decrypted text: %s\n", string(decryptedText))
}
示例7: NewAesEncryptionStream
// NewAesEncryptionStream creates a new AES description stream based on given key and IV.
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
aesBlock, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
return cipher.NewCFBEncrypter(aesBlock, iv)
}
示例8: NewChiper
func NewChiper(algo, secret string) (*Cipher, error) {
if algo == "rc4" {
c, err := rc4.NewCipher(truncateSecretToSize(secret, 32))
if err != nil {
return nil, err
}
return &Cipher{
enc: c,
dec: c,
}, nil
} else if algo == "aes" {
key := truncateSecretToSize(secret, 32)
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return &Cipher{
enc: cipher.NewCFBEncrypter(c, key[:c.BlockSize()]),
dec: cipher.NewCFBDecrypter(c, key[:c.BlockSize()]),
}, nil
}
cipher, err := rc4.NewCipher([]byte(secret))
if err != nil {
return nil, err
}
return &Cipher{
enc: cipher,
dec: cipher,
}, nil
}
示例9: encryptAES
// encryptAES enrypts plaintext input with passed key, IV and mode in AES block cipher;
// and returns ciphertext output
func encryptAES(input []byte, output []byte, key, iv []byte, mode Mode) error {
block, err := aes.NewCipher(key)
if err != nil {
return errors.New("Couldn't create block cipher.")
}
// Prepend IV to ciphertext.
// Generate IV randomly if it is not passed
if iv == nil {
if iv = generateIV(aes.BlockSize); iv == nil {
return errors.New("Couldn't create random initialization vector (IV).")
}
}
copy(output, iv)
switch mode {
case CBC:
if len(input)%aes.BlockSize != 0 {
input = addPadding(input, aes.BlockSize)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(output[aes.BlockSize:], input)
case CFB:
mode := cipher.NewCFBEncrypter(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
case CTR:
mode := cipher.NewCTR(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
case OFB:
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
}
return nil
}
示例10: main
func main() {
// 原文本
pliantext := []byte("遊ぼうよ、和と")
if len(os.Args) > 1 {
pliantext = []byte(os.Args[1])
}
Println("原文本:", string(pliantext))
// 加密盐 长度必须为 16B(128b), 24B(192b), 32B(256b)
key_text := "最高の奇跡に乗り込め!!"
if len(os.Args) > 2 {
key_text = os.Args[2]
}
Println("盐", len(key_text), ":", key_text)
// 加密算法
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
Printf("错误:NewCipher(%dB) = %s\n", len(key_text), err)
os.Exit(1)
}
// 实行加密
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(pliantext))
cfb.XORKeyStream(ciphertext, pliantext)
Printf("%s=>%x\n", pliantext, ciphertext)
// 实行解密
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
gettext := make([]byte, len(pliantext))
cfbdec.XORKeyStream(gettext, ciphertext)
Printf("%x=>%s\n", ciphertext, gettext)
}
示例11: encryptFile
func encryptFile(path string) {
fmt.Printf(" Lock: %s\n", path)
plaintext, err := ioutil.ReadFile(path)
if err != nil {
panic(err.Error())
}
byteKey := sha256.Sum256([]byte(*key))
block, err := aes.NewCipher(byteKey[:])
if err != nil {
panic(err)
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
f, err := os.Create(path + ".aes")
if err != nil {
panic(err.Error())
}
_, err = io.Copy(f, bytes.NewReader(ciphertext))
if err != nil {
panic(err.Error())
}
os.Remove(path)
}
示例12: Encrypt
func Encrypt(in io.Reader, key string) io.Reader {
// Convert io.Reader to string
buf := new(bytes.Buffer)
buf.ReadFrom(in)
s := buf.String()
// Load the plaintext message you want to encrypt.
plaintext := []byte(s)
// Setup a key that will encrypt the other text.
h := sha256.New()
io.WriteString(h, key)
key_text := h.Sum(nil)
// We chose our cipher type here in this case we are using AES.
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
os.Exit(-1)
}
// We use the CFBEncrypter in order to encrypt
// the whole stream of plaintext using the
// cipher setup with c and a iv.
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
return strings.NewReader(string(ciphertext))
}
示例13: encryptToBase64
func encryptToBase64(content string, k string) string {
var commonIV = []byte{0x1f, 0x2c, 0x49, 0xea, 0xb5, 0xf3, 0x47, 0x2e, 0xa8, 0x71, 0x0a, 0xc0, 0x4e, 0x5d, 0x83, 0x19}
plaintext := []byte(content)
key_salt := "3z0RlwtvIOdlC8aAwIaCbX0D"
var key_text string
if len(k) < 24 {
key_text = k + key_salt[len(k):]
}
if len(k) > 24 {
key_text = k[:24]
}
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
os.Exit(-1)
}
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
base64Text := base64.StdEncoding.EncodeToString(ciphertext)
return string(base64Text)
}
示例14: FakioDial
func FakioDial(server string, req []byte) (c *FakioConn, err error) {
conn, err := net.Dial("tcp", server)
if err != nil {
return nil, err
}
//handshake
key := stringToKey(fclient.PassWord)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
enc := cipher.NewCFBEncrypter(block, req[0:16])
// 22 = iv + username
index := 16 + int(req[16]) + 1
enc.XORKeyStream(req[index:], req[index:])
if _, err := conn.Write(req); err != nil {
return nil, errors.New("handshake to server error")
}
hand := make([]byte, 64)
if _, err := conn.Read(hand); err != nil {
return nil, errors.New("handshake to server error")
}
dec := cipher.NewCFBDecrypter(block, hand[0:16])
dec.XORKeyStream(hand[16:], hand[16:])
cipher, err := NewCipher(hand[16:])
return &FakioConn{conn, cipher}, nil
}
示例15: Bagla
// Bagla encrypts the text with the given key.
func Bagla(key string, text string) (string, error) {
k := len(key)
switch k {
default:
return "", errors.New("key length must be 16, 24 or 32")
case 16, 24, 32:
break
}
plaintext := []byte(text)
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}