當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Block.Decrypt方法代碼示例

本文整理匯總了Golang中crypto/cipher.Block.Decrypt方法的典型用法代碼示例。如果您正苦於以下問題:Golang Block.Decrypt方法的具體用法?Golang Block.Decrypt怎麽用?Golang Block.Decrypt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在crypto/cipher.Block的用法示例。


在下文中一共展示了Block.Decrypt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestCipher

func TestCipher(t *testing.T) {
	var aes cipher.Block
	var err error
	for cipher_id, expected := range cipher_results {
		aes, err = NewCipher(cipher_id, key, nil)
		if err != nil {
			t.Fatal(err)
		}
		blocksize := aes.BlockSize()
		ciphertext := make([]byte, blocksize)
		aes.Encrypt(ciphertext, cleartext)
		deciphertext := make([]byte, blocksize)
		if !strings.EqualFold(expected, fmt.Sprintf("%x", ciphertext)) {
			t.Fatal("couldn't encrypt")
		}
		aes, err = NewCipher(cipher_id, key, nil)
		aes.Decrypt(deciphertext, ciphertext)
		if !bytes.Equal(cleartext, deciphertext) {
			t.Fatal("couldn't decrypt %s %s", cleartext, deciphertext)
		}
		if err != nil {
			t.Fatal(err)
		}
	}
}
開發者ID:postfix,項目名稱:gocryptodev,代碼行數:25,代碼來源:cipher_test.go

示例2: ECBDecrypt

func ECBDecrypt(block cipher.Block, dst, src []byte) {
	size := block.BlockSize()
	if len(dst)%size != 0 {
		panic("size of dst and src should be multiples of blocksize")
	}
	for i := 0; i < len(dst); i += size {
		block.Decrypt(dst[i:i+size], src[i:i+size])
	}
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:9,代碼來源:util.go

示例3: aesTransform

// aesTransform - encrypt or decrypt (according to "direction") using block
// cipher "bc" (typically AES)
func aesTransform(dst []byte, src []byte, direction directionConst, bc cipher.Block) {
	if direction == DirectionEncrypt {
		bc.Encrypt(dst, src)
		return
	} else if direction == DirectionDecrypt {
		bc.Decrypt(dst, src)
		return
	}
}
開發者ID:ncw,項目名稱:rclone,代碼行數:11,代碼來源:eme.go

示例4: aesDecrypt

func aesDecrypt(block cipher.Block, src []byte) []byte {
	var dst = make([]byte, 16)
	var src_len = len(src)
	var dec []byte
	for i := 0; i < src_len; i += 16 {
		block.Decrypt(dst, src[i:i+16])
		dec = append(dec, dst...)
	}
	return dec
}
開發者ID:qingtao,項目名稱:mana,代碼行數:10,代碼來源:aes.go

示例5: decryptBlocks

func decryptBlocks(b cipher.Block, dst, src []byte) error {
	if len(src)%b.BlockSize() != 0 {
		return ErrBlockSize
	}
	if len(dst) < len(src) {
		return ErrOutputSize
	}
	for len(src) > 0 {
		b.Decrypt(dst, src[:b.BlockSize()])
		src = src[b.BlockSize():]
		dst = dst[b.BlockSize():]
	}
	return nil
}
開發者ID:Crysty-Yui,項目名稱:goim,代碼行數:14,代碼來源:aes.go

示例6: blockDecrypt

// blockDecrypt decrypts using the block cipher blk in ECB mode.
func blockDecrypt(blk cipher.Block, dst, src []byte) error {

	if len(src) > len(dst) || len(src)%blk.BlockSize() != 0 {
		return errors.New("Block decryption failed")
	}

	l := len(src) - blk.BlockSize()

	for i := 0; i <= l; i += blk.BlockSize() {
		blk.Decrypt(dst[i:], src[i:])
	}

	return nil
}
開發者ID:Festum,項目名稱:go-mega,代碼行數:15,代碼來源:utils.go

示例7: DecryptCBC

func DecryptCBC(ciph cipher.Block, iv []byte, ciphertext []byte) ([]byte, error) {
	var resultblocks [][]byte
	blocks := mobytes.SplitEvery(ciphertext, 16)
	for i, block := range blocks {
		decrypted := make([]byte, 16)
		ciph.Decrypt(decrypted, block)
		if i == 0 {
			decrypted = mobytes.XOR(decrypted, iv)
		} else {
			decrypted = mobytes.XOR(decrypted, blocks[i-1])
		}
		resultblocks = append(resultblocks, decrypted)
	}
	return bytes.Join(resultblocks, nil), nil
}
開發者ID:phayes,項目名稱:cryptocourse,代碼行數:15,代碼來源:main.go

示例8: decrypt

func decrypt(src io.Reader, des io.Writer, cipher cipher.Block, length int) error {
	blockSize := cipher.BlockSize()
	if length <= 0 {
		return errors.New("length must be greater than 0")
	}
	buf := make([]byte, blockSize)
	acc := 0

	n, err := src.Read(buf)
	for err == nil && acc < length {
		acc += n
		cipher.Decrypt(buf, buf)

		if acc > length {
			paddings := acc - length
			des.Write(buf[0 : blockSize-paddings])
			return nil
		}
		des.Write(buf)
		buf = make([]byte, blockSize)

		n, err = src.Read(buf)
	}
	return nil
}
開發者ID:crayontxx,項目名稱:go-rc5,代碼行數:25,代碼來源:cipher_ecb.go

示例9: decryptDES

// decryptDES derypts ciphertext input with passed key and 3DES-flag
// in DES block cipher; and returns plaintext output
func decryptDES(input, output, key []byte, tripleDES bool) error {
	var block cipher.Block
	var err error
	if tripleDES {
		block, err = des.NewTripleDESCipher(key)
	} else {
		block, err = des.NewCipher(key)
	}

	if err != nil {
		return errors.New("Couldn't create block cipher.")
	}

	for i := 0; i < len(input)/des.BlockSize; i++ {
		start := des.BlockSize * i
		end := start + des.BlockSize
		block.Decrypt(output[start:end], input[start:end])
	}
	return nil
}
開發者ID:mohoff,項目名稱:CryptoWrapper,代碼行數:22,代碼來源:des.go

示例10: AESDecryptBytes

func AESDecryptBytes(block cipher.Block, cipherBytes []byte) (plain []byte, err error) {
	defer func() {
		if e := recover(); e != nil {
			plain = nil
			err = fmt.Errorf("%v", e)
		}
	}()

	blockSize := block.BlockSize()
	cipherBytes = utils.PadBytes(cipherBytes, blockSize)
	length := len(cipherBytes)

	// Decrypt
	plain = make([]byte, length)
	for i := 0; i < length; i += blockSize {
		block.Decrypt(plain[i:i+blockSize], cipherBytes[i:i+blockSize])
	}

	return
}
開發者ID:thecloakproject,項目名稱:utils,代碼行數:20,代碼來源:aes.go

示例11: keyunwrap

func keyunwrap(block cipher.Block, ciphertxt []byte) ([]byte, error) {
	if len(ciphertxt)%keywrapChunkLen != 0 {
		return nil, ErrInvalidBlockSize
	}

	n := (len(ciphertxt) / keywrapChunkLen) - 1
	r := make([][]byte, n)

	for i := range r {
		r[i] = make([]byte, keywrapChunkLen)
		copy(r[i], ciphertxt[(i+1)*keywrapChunkLen:])
	}

	buffer := make([]byte, keywrapChunkLen*2)
	tBytes := make([]byte, keywrapChunkLen)
	copy(buffer[:keywrapChunkLen], ciphertxt[:keywrapChunkLen])

	for t := 6*n - 1; t >= 0; t-- {
		binary.BigEndian.PutUint64(tBytes, uint64(t+1))

		for i := 0; i < keywrapChunkLen; i++ {
			buffer[i] = buffer[i] ^ tBytes[i]
		}
		copy(buffer[keywrapChunkLen:], r[t%n])

		block.Decrypt(buffer, buffer)

		copy(r[t%n], buffer[keywrapChunkLen:])
	}

	if subtle.ConstantTimeCompare(buffer[:keywrapChunkLen], keywrapDefaultIV) == 0 {
		return nil, errors.New("keywrap: failed to unwrap key")
	}

	out := make([]byte, n*keywrapChunkLen)
	for i := range r {
		copy(out[i*keywrapChunkLen:], r[i])
	}

	return out, nil
}
開發者ID:lestrrat,項目名稱:go-jwx,代碼行數:41,代碼來源:key_encrypt.go

示例12: KeyUnwrap

// KeyUnwrap implements NIST key unwrapping; it unwraps a content encryption key (cek) with the given block cipher.
func KeyUnwrap(block cipher.Block, ciphertext []byte) ([]byte, error) {
	if len(ciphertext)%8 != 0 {
		return nil, errors.New("square/go-jose: key wrap input must be 8 byte blocks")
	}

	n := (len(ciphertext) / 8) - 1
	r := make([][]byte, n)

	for i := range r {
		r[i] = make([]byte, 8)
		copy(r[i], ciphertext[(i+1)*8:])
	}

	buffer := make([]byte, 16)
	tBytes := make([]byte, 8)
	copy(buffer[:8], ciphertext[:8])

	for t := 6*n - 1; t >= 0; t-- {
		binary.BigEndian.PutUint64(tBytes, uint64(t+1))

		for i := 0; i < 8; i++ {
			buffer[i] = buffer[i] ^ tBytes[i]
		}
		copy(buffer[8:], r[t%n])

		block.Decrypt(buffer, buffer)

		copy(r[t%n], buffer[8:])
	}

	if subtle.ConstantTimeCompare(buffer[:8], defaultIV) == 0 {
		return nil, errors.New("square/go-jose: failed to unwrap key")
	}

	out := make([]byte, n*8)
	for i := range r {
		copy(out[i*8:], r[i])
	}

	return out, nil
}
開發者ID:CometKim,項目名稱:platform,代碼行數:42,代碼來源:key_wrap.go

示例13: TestStreamCipher

func TestStreamCipher(t *testing.T) {
	var aes cipher.Block
	var err error
	iv := []byte{0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
	aes, err = NewCipher(CRYPTO_AES_CBC, key, iv)
	if err != nil {
		t.Fatal(err)
	}
	blocksize := aes.BlockSize()
	ciphertext := make([]byte, blocksize)
	aes.Encrypt(ciphertext, cleartext)
	deciphertext := make([]byte, blocksize)
	aes, err = NewCipher(CRYPTO_AES_CBC, key, iv)
	aes.Decrypt(deciphertext, ciphertext)
	if !bytes.Equal(cleartext, deciphertext) {
		t.Fatal("couldn't decrypt %s %s", cleartext, deciphertext)
	}
	if err != nil {
		t.Fatal(err)
	}
}
開發者ID:postfix,項目名稱:gocryptodev,代碼行數:21,代碼來源:cipher_test.go

示例14: key_unwarp

func key_unwarp(block cp.Block, ciphertext []byte) ([]byte, error) {
	if len(ciphertext)%8 != 0 {
		return nil, errors.New("key warp must 8 byte")
	}

	n := (len(ciphertext) / 8) - 1
	r := make([][]byte, n)

	for i := range r {
		r[i] = make([]byte, 8)
		copy(r[i], ciphertext[(i+1)*8:])
	}

	buf := make([]byte, 16)
	tb := make([]byte, 8)
	copy(buf[:8], ciphertext[:8])

	for t := 6*n - 1; t >= 0; t-- {
		binary.BigEndian.PutUint64(tb, uint64(t+1))

		for i := 0; i < 8; i++ {
			buf[i] = buf[i] ^ tb[i]
		}
		//fmt.Println(buf, r, t%n)
		copy(buf[8:], r[t%n])
		block.Decrypt(buf, buf)
		copy(r[t%n], buf[8:])
	}

	if subtle.ConstantTimeCompare(buf[:8], default_iv) == 0 {
		return nil, errors.New("failed to unwarp key")
	}
	out := make([]byte, n*8)
	for i := range r {
		copy(out[i*8:], r[i])
	}
	return out, nil
}
開發者ID:blinkat,項目名稱:go-invoker,代碼行數:38,代碼來源:key.go

示例15: CBCDecrypt

func CBCDecrypt(block cipher.Block, iv, dst, src []byte) {
	size := block.BlockSize()
	if len(iv) != size {
		panic("size of IV not equal to block size")
	}
	if len(dst)%size != 0 {
		panic("size of dst and src should be multiples of blocksize")
	}
	if len(dst) == 0 || len(src) == 0 {
		return
	}
	x := make([]byte, size)
	oldSrc := make([]byte, size)
	copy(x, iv)
	var i int
	for i = 0; i < len(dst) && i < len(src); i += size {
		copy(oldSrc, src[i:i+size])
		block.Decrypt(dst[i:i+size], src[i:i+size])
		for j := 0; j < size; j++ {
			dst[i+j] ^= x[j]
		}
		copy(x, oldSrc)
	}
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:24,代碼來源:util.go


注:本文中的crypto/cipher.Block.Decrypt方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。