当前位置: 首页>>代码示例>>Golang>>正文


Golang cipher.NewCFBDecrypter函数代码示例

本文整理汇总了Golang中crypto/cipher.NewCFBDecrypter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCFBDecrypter函数的具体用法?Golang NewCFBDecrypter怎么用?Golang NewCFBDecrypter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewCFBDecrypter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: DecodePostPush

func DecodePostPush(nbpp *PostPush, seed int64, passbytes []byte) (PostContent, error) {
	var nbpc PostContent
	bb := bytes.NewBuffer(nbpp.Content)

	//get hash generated
	res := hashIt(seed, passbytes)

	//get our encrypter rolling
	block, err := aes.NewCipher(res)
	if err != nil {
		return nbpc, err
	}
	stream := cipher.NewCFBDecrypter(block, nbpp.IV)
	cryptrdr := cipher.StreamReader{S: stream, R: bb}

	dec := gob.NewDecoder(cryptrdr)
	if err := dec.Decode(&nbpc); err != nil {
		return nbpc, err
	}

	//verify hash
	if !CompareHash(nbpc.BP.hash(), nbpc.Hash) {
		return nbpc, errors.New("Invalid post hash")
	}

	return nbpc, nil
}
开发者ID:traetox,项目名称:blogEngine,代码行数:27,代码来源:blogpost.go

示例2: AesDecryptFd

func AesDecryptFd(inFile, outFile *os.File, key, iv []byte, ctp int) error {
	block, err := aes.NewCipher(key)
	if err != nil {
		return err
	}

	var stream cipher.Stream
	switch ctp {
	case 1:
		stream = cipher.NewCFBDecrypter(block, iv[:])
	case 2:
		stream = cipher.NewCTR(block, iv[:])
	default:
		stream = cipher.NewOFB(block, iv[:])
	}

	reader := &cipher.StreamReader{S: stream, R: inFile}
	// Copy the input file to the output file, decrypting as we go.
	if _, err := io.Copy(outFile, reader); err != nil {
		return err
	}

	// Note that this example is simplistic in that it omits any
	// authentication of the encrypted data. If you were actually to use
	// StreamReader in this manner, an attacker could flip arbitrary bits in
	// the output.
	return nil
}
开发者ID:st2py,项目名称:bitcrypt,代码行数:28,代码来源:ut_aes.go

示例3: Decrypt

// Decrypt attempts to decrypt an encrypted session key. If it returns nil,
// ske.Key will contain the session key.
func (ske *SymmetricKeyEncrypted) Decrypt(passphrase []byte) error {
	if !ske.Encrypted {
		return nil
	}

	key := make([]byte, ske.CipherFunc.KeySize())
	ske.s2k(key, passphrase)

	if len(ske.encryptedKey) == 0 {
		ske.Key = key
	} else {
		// the IV is all zeros
		iv := make([]byte, ske.CipherFunc.blockSize())
		c := cipher.NewCFBDecrypter(ske.CipherFunc.new(key), iv)
		c.XORKeyStream(ske.encryptedKey, ske.encryptedKey)
		ske.CipherFunc = CipherFunction(ske.encryptedKey[0])
		if ske.CipherFunc.blockSize() == 0 {
			return errors.UnsupportedError("unknown cipher: " + strconv.Itoa(int(ske.CipherFunc)))
		}
		ske.CipherFunc = CipherFunction(ske.encryptedKey[0])
		ske.Key = ske.encryptedKey[1:]
		if len(ske.Key)%ske.CipherFunc.blockSize() != 0 {
			ske.Key = nil
			return errors.StructuralError("length of decrypted key not a multiple of block size")
		}
	}

	ske.Encrypted = false
	return nil
}
开发者ID:JayBlaze420,项目名称:camlistore,代码行数:32,代码来源:symmetric_key_encrypted.go

示例4: view

func view(c *gin.Context) {
	id := c.Param("uniuri")
	key := c.Param("key")
	re := models.ResourceEntry{}
	remote := c.ClientIP()

	db.Where(&models.ResourceEntry{Key: id}).First(&re)
	if re.Key == "" {
		log.Printf("[INFO][%s]\tNot found : %s", remote, id)
		c.AbortWithStatus(http.StatusNotFound)
		return
	}
	log.Printf("[INFO][%s]\tFetched %s file and entry\n", remote, id)
	f, err := os.Open(path.Join(conf.C.UploadDir, re.Key))
	if err != nil {
		log.Printf("[ERROR][%s]\tWhile opening %s file\n", remote, id)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	block, err := aes.NewCipher([]byte(key))
	if err != nil {
		log.Printf("[ERROR][%s]\tDuring Cipher creation : %s\n", remote, err)
		c.String(http.StatusInternalServerError, "Something went wrong on the server side. Try again later.")
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	var iv [aes.BlockSize]byte
	stream := cipher.NewCFBDecrypter(block, iv[:])
	reader := &cipher.StreamReader{S: stream, R: f}
	c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
	io.Copy(c.Writer, reader)
}
开发者ID:krionux,项目名称:goploader,代码行数:32,代码来源:main.go

示例5: decrypt

func decrypt(str string) (plaintext string, err error) {
	ciphertext, err := hex.DecodeString(str)
	if err != nil {
		return "", err
	}

	var block cipher.Block
	if block, err = aes.NewCipher(AES_KEY); err != nil {
		return
	}

	if len(ciphertext) < aes.BlockSize {
		err = errors.New("ciphertext too short")
		return
	}

	iv := ciphertext[:aes.BlockSize]
	fmt.Println(iv)
	ciphertext = ciphertext[aes.BlockSize:]

	cfb := cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(ciphertext, ciphertext)

	plaintext = string(ciphertext)
	return
}
开发者ID:cubeee,项目名称:go-sig,代码行数:26,代码来源:util.go

示例6: Decrypt

// Eecrypt from base64 to decrypted string
func Decrypt(keyStr, cryptoText string) (r string, err error) {
	key := []byte(keyStr)
	ciphertext, err := base64.URLEncoding.DecodeString(cryptoText)
	if err != nil {
		return
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		return
	}

	// The IV needs to be unique, but not secure. Therefore it's common to
	// include it at the beginning of the ciphertext.
	if len(ciphertext) < aes.BlockSize {
		err = errors.New("ciphertext too short")
		return
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	stream := cipher.NewCFBDecrypter(block, iv)

	// XORKeyStream can work in-place if the two arguments are the same.
	stream.XORKeyStream(ciphertext, ciphertext)

	r = fmt.Sprintf("%s", ciphertext)
	return
}
开发者ID:kobeld,项目名称:goutils,代码行数:31,代码来源:encrypt.go

示例7: Decrypt

func Decrypt(key string, publickey PublicKey, rin io.Reader) (r io.Reader, length int64, sequence int64, e os.Error) {
	c, e := simpleCipher(key)
	if e != nil {
		e = os.NewError("Trouble reading the symmetric key: " + e.String())
		return
	}
	pub, e := readPublicKey(publickey)
	if e != nil {
		e = os.NewError("Trouble reading the public key: " + e.String())
		return
	}
	iv := make([]byte, c.BlockSize())
	_, e = io.ReadFull(rin, iv) // read the iv first (it's not encrypted)
	if e != nil {
		e = os.NewError("Trouble reading the iv: " + e.String())
		return
	}
	decrypter := cipher.NewCFBDecrypter(c, iv)
	rdec := flate.NewReader(cipher.StreamReader{decrypter, rin})
	e = binary.Read(rdec, binary.LittleEndian, &length)
	if e != nil {
		e = os.NewError("Trouble reading the file length: " + e.String())
		return
	}
	e = binary.Read(rdec, binary.LittleEndian, &sequence)
	if e != nil {
		e = os.NewError("Trouble reading the serial number: " + e.String())
		return
	}
	return &hashReader{rdec, sha256.New(), pub, length}, length, sequence, nil
}
开发者ID:andradeandrey,项目名称:goadmin,代码行数:31,代码来源:crypt.go

示例8: decryptAES

// decryptAES derypts ciphertext input with passed key and mode (IV is contained in input)
// in AES block cipher; and returns plaintext output
func decryptAES(input []byte, output []byte, key []byte, mode Mode) error {
	block, err := aes.NewCipher(key)
	if err != nil {
		return errors.New("Couldn't create block cipher.")
	}
	if len(input) < aes.BlockSize {
		return errors.New("Ciphertext too short.")
	}

	iv := input[:aes.BlockSize]
	ciphertext := input[aes.BlockSize:]

	switch mode {
	case CBC:
		if len(input)%aes.BlockSize != 0 {
			return errors.New("Ciphertext doesn't satisfy CBC-mode requirements.")
		}
		mode := cipher.NewCBCDecrypter(block, iv)
		mode.CryptBlocks(output, ciphertext)
	case CFB:
		mode := cipher.NewCFBDecrypter(block, iv)
		mode.XORKeyStream(output, ciphertext)
	case CTR:
		mode := cipher.NewCTR(block, iv)
		mode.XORKeyStream(output, ciphertext)
	case OFB:
		mode := cipher.NewOFB(block, iv)
		mode.XORKeyStream(output, ciphertext)
	}
	return nil
}
开发者ID:mohoff,项目名称:CryptoWrapper,代码行数:33,代码来源:aes.go

示例9: AESDecrypt

func AESDecrypt(c string, key string) (s string, err error) {
	var k []byte
	var block cipher.Block
	var cfb cipher.Stream
	var cb []byte
	var iv []byte

	k = AESMake256Key(key)

	cb, err = hex.DecodeString(c)
	if err != nil {
		return
	}

	block, err = aes.NewCipher(k)
	if err != nil {
		return
	}
	if len(cb) < aes.BlockSize {
		err = errors.New("crypt string is too short")
		return
	}

	iv = cb[:aes.BlockSize]
	cb = cb[aes.BlockSize:]

	cfb = cipher.NewCFBDecrypter(block, iv)
	cfb.XORKeyStream(cb, cb)
	s = bytes.NewBuffer(cb).String()
	return
}
开发者ID:dulumao,项目名称:ant,代码行数:31,代码来源:aes.go

示例10: decryptFile

func decryptFile(path string) {
	fmt.Printf("UnLock: %s\n", path)

	content, 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)
	}

	iv := content[:aes.BlockSize]
	cyphertext := content[aes.BlockSize:]

	plaintext := make([]byte, len(content)-aes.BlockSize)

	stream := cipher.NewCFBDecrypter(block, iv)
	stream.XORKeyStream(plaintext, cyphertext)

	f, err := os.Create(path[:len(path)-4])
	if err != nil {
		panic(err.Error())
	}
	_, err = io.Copy(f, bytes.NewReader(plaintext))
	if err != nil {
		panic(err.Error())
	}
	os.Remove(path)
}
开发者ID:fernandopn,项目名称:flashSafe,代码行数:32,代码来源:main.go

示例11: 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
}
开发者ID:detailyang,项目名称:hikarian,代码行数:31,代码来源:cipher.go

示例12: 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)
}
开发者ID:kingfree,项目名称:haut,代码行数:30,代码来源:aes.go

示例13: Decrypt

func Decrypt(in io.Reader, key string) io.Reader {

	buf := new(bytes.Buffer)
	buf.ReadFrom(in)
	s := buf.String()

	// Load the ciphertext message you want to decrypt
	ciphertext := []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 CFBDecrypter in order to decrypt
	// the whole stream of ciphertext using the
	// cipher setup with c and a iv.
	cfb := cipher.NewCFBDecrypter(c, commonIV)
	plaintext := make([]byte, len(ciphertext))
	cfb.XORKeyStream(plaintext, ciphertext)
	return strings.NewReader(string(plaintext))
}
开发者ID:Hermes,项目名称:hermes,代码行数:29,代码来源:encryption.go

示例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
}
开发者ID:NichoZhang,项目名称:fakio,代码行数:34,代码来源:fclient.go

示例15: Unveil

// Unveil base64 decodes a slice of bytes and uses aes encryption to decrypt. It returns a decrypted slice of bytes,
// and an error. A CipherLengthError is returned if the data is less than 16 bytes.
func (cloak Cloak) Unveil(data []byte) ([]byte, error) {
	decodedData := make([]byte, base64.URLEncoding.DecodedLen(len(data)))
	n, err := base64.URLEncoding.Decode(decodedData, data)
	if err != nil {
		return []byte{}, err
	}
	decodedData = decodedData[:n]

	if len(decodedData) < aes.BlockSize {
		return []byte{}, CipherLengthError{}
	}

	initializationVector := decodedData[:aes.BlockSize]
	decodedData = decodedData[aes.BlockSize:]

	cipherDecrypter := cipher.NewCFBDecrypter(cloak.cipherBlock, initializationVector)
	cipherDecrypter.XORKeyStream(decodedData, decodedData)

	decoded := make([]byte, base64.StdEncoding.DecodedLen(len(decodedData)))
	n, err = base64.StdEncoding.Decode(decoded, decodedData)
	if err != nil {
		return []byte{}, err
	}

	return decoded[:n], nil
}
开发者ID:dieucao,项目名称:notifications,代码行数:28,代码来源:conceal.go


注:本文中的crypto/cipher.NewCFBDecrypter函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。