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


Golang Block.Encrypt方法代碼示例

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


在下文中一共展示了Block.Encrypt方法的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: generateSubkeys

// Given the supplied cipher, whose block size must be 16 bytes, return two
// subkeys that can be used in MAC generation. See section 5.3 of NIST SP
// 800-38B. Note that the other NIST-approved block size of 8 bytes is not
// supported by this function.
func generateSubkeys(ciph cipher.Block) (k1 []byte, k2 []byte) {
	if ciph.BlockSize() != blockSize {
		panic("generateSubkeys requires a cipher with a block size of 16 bytes.")
	}

	// Step 1
	l := make([]byte, blockSize)
	ciph.Encrypt(l, subkeyZero)

	// Step 2: Derive the first subkey.
	if common.Msb(l) == 0 {
		// TODO(jacobsa): Accept a destination buffer in ShiftLeft and then hoist
		// the allocation in the else branch below.
		k1 = common.ShiftLeft(l)
	} else {
		k1 = make([]byte, blockSize)
		common.Xor(k1, common.ShiftLeft(l), subkeyRb)
	}

	// Step 3: Derive the second subkey.
	if common.Msb(k1) == 0 {
		k2 = common.ShiftLeft(k1)
	} else {
		k2 = make([]byte, blockSize)
		common.Xor(k2, common.ShiftLeft(k1), subkeyRb)
	}

	return
}
開發者ID:itiserik,項目名稱:crypto,代碼行數:33,代碼來源:subkey.go

示例3: Encrypt

// Encrypts the contents of a file and returns the encrypted byte slice.
// AES encryption with PCKS#7 padding.
func Encrypt(filename, key string) (encrypted []byte, err error) {
	var (
		aesCipher cipher.Block
		data      []byte
		tempenc   = make([]byte, blockSize)
	)
	aesCipher, err = aes.NewCipher([]byte(key))
	if err != nil {
		return
	}

	data, err = ioutil.ReadFile(filename)
	if err != nil {
		return encrypted, err
	}

	padded, err := padding.Pad(data, blockSize)
	if err != nil {
		return encrypted, err
	}
	for i := 0; i < len(padded)/blockSize; i++ {
		aesCipher.Encrypt(tempenc, padded[i*blockSize:i*blockSize+blockSize])
		encrypted = append(encrypted, tempenc...)
	}
	return encrypted, nil
}
開發者ID:Wombats,項目名稱:wombat-desktop,代碼行數:28,代碼來源:encryption.go

示例4: CTREncrypt

func CTREncrypt(block cipher.Block, nonce, dst, src []byte) {
	size := block.BlockSize()
	if len(nonce) != size {
		panic("size of IV not equal to block size")
	}
	if len(dst) == 0 || len(src) == 0 {
		return
	}
	// temp key
	key := make([]byte, size)
	// copy of nonce
	n := make([]byte, size)
	copy(n, nonce)
	counter := binary.LittleEndian.Uint64(n[8:])
	for i := 0; i < len(dst) && i < len(src); i += size {
		block.Encrypt(key, n)

		for j := 0; j < size && i+j < len(src); j++ {
			dst[i+j] = src[i+j] ^ key[j]
		}
		counter++
		binary.LittleEndian.PutUint64(n[8:], counter)
	}
	return
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:25,代碼來源:util.go

示例5: ECBEncrypt

func ECBEncrypt(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.Encrypt(dst[i:i+size], src[i:i+size])
	}
}
開發者ID:ysmolsky,項目名稱:cryptopals-matasano-solutions,代碼行數:9,代碼來源:util.go

示例6: 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

示例7: get_key

func get_key(master cipher.Block, ctr uint64) (cipher.Block, []byte) {
	plain := make([]byte, 16)
	new_key := make([]byte, 16)
	binary.BigEndian.PutUint64(plain, ctr)
	master.Encrypt(new_key, plain)
	new_cipher, err := aes.NewCipher(new_key)
	check(err)
	return new_cipher, new_key
}
開發者ID:arthaud,項目名稱:write-ups-2016,代碼行數:9,代碼來源:prover.go

示例8: updateMAC

// updateMAC reseeds the given hash with encrypted seed.
// it returns the first 16 bytes of the hash sum after seeding.
func updateMAC(mac hash.Hash, block cipher.Block, seed []byte) []byte {
	aesbuf := make([]byte, aes.BlockSize)
	block.Encrypt(aesbuf, mac.Sum(nil))
	for i := range aesbuf {
		aesbuf[i] ^= seed[i]
	}
	mac.Write(aesbuf)
	return mac.Sum(nil)[:16]
}
開發者ID:ruflin,項目名稱:go-ethereum,代碼行數:11,代碼來源:rlpx.go

示例9: Encrypt

// Encrypt encrypts the plaintext src and outputs the corresponding ciphertext into dst.
// Besides outputting a ciphertext into dst, Encrypt also outputs an authentication tag
// of ocb2.TagSize bytes into tag, which should be used to verify the authenticity of the
// message on the receiving side.
//
// To ensure both authenticity and secrecy of messages, each invocation to this function must
// be given an unique nonce of ocb2.NonceSize bytes.  The nonce need not be secret (it can be
// a counter), but it needs to be unique.
//
// The block cipher used in function must work on a block size equal to ocb2.BlockSize.
// The tag slice used in this function must have a length equal to ocb2.TagSize.
// The nonce slice used in this function must have a length equal to ocb2.NonceSize.
// If any of the above are violated, Encrypt will panic.
func Encrypt(cipher cipher.Block, dst []byte, src []byte, nonce []byte, tag []byte) {
	if cipher.BlockSize() != BlockSize {
		panic("ocb2: cipher blocksize is not equal to ocb2.BlockSize")
	}
	if len(nonce) != NonceSize {
		panic("ocb2: nonce length is not equal to ocb2.NonceSize")
	}

	var (
		checksum [BlockSize]byte
		delta    [BlockSize]byte
		tmp      [BlockSize]byte
		pad      [BlockSize]byte
		calcTag  [NonceSize]byte
		off      int
	)

	cipher.Encrypt(delta[0:], nonce[0:])
	zeros(checksum[0:])

	remain := len(src)
	for remain > BlockSize {
		times2(delta[0:])
		xor(tmp[0:], delta[0:], src[off:off+BlockSize])
		cipher.Encrypt(tmp[0:], tmp[0:])
		xor(dst[off:off+BlockSize], delta[0:], tmp[0:])
		xor(checksum[0:], checksum[0:], src[off:off+BlockSize])
		remain -= BlockSize
		off += BlockSize
	}

	times2(delta[0:])
	zeros(tmp[0:])
	num := remain * 8
	tmp[BlockSize-2] = uint8((uint32(num) >> 8) & 0xff)
	tmp[BlockSize-1] = uint8(num & 0xff)
	xor(tmp[0:], tmp[0:], delta[0:])
	cipher.Encrypt(pad[0:], tmp[0:])
	copied := copy(tmp[0:], src[off:])
	if copied != remain {
		panic("ocb2: copy failed")
	}
	if copy(tmp[copied:], pad[copied:]) != (BlockSize - remain) {
		panic("ocb2: copy failed")
	}
	xor(checksum[0:], checksum[0:], tmp[0:])
	xor(tmp[0:], pad[0:], tmp[0:])
	if copy(dst[off:], tmp[0:]) != remain {
		panic("ocb2: copy failed")
	}

	times3(delta[0:])
	xor(tmp[0:], delta[0:], checksum[0:])
	cipher.Encrypt(calcTag[0:], tmp[0:])
	copy(tag, calcTag[:])
}
開發者ID:carriercomm,項目名稱:grumble,代碼行數:69,代碼來源:ocb2.go

示例10: aesEncrypt

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

示例11: AESEncryptBytes

func AESEncryptBytes(block cipher.Block, plain []byte) (cipherBytes []byte, err error) {
	blockSize := block.BlockSize()
	plain = utils.PadBytes(plain, blockSize)
	length := len(plain)

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

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

示例12: DecryptCTR

func DecryptCTR(ciph cipher.Block, iv []byte, ciphertext []byte) ([]byte, error) {
	var pad []byte
	var mixin big.Int
	mixin.SetBytes(iv)
	for i := 0; i <= len(ciphertext)/16; i++ {
		if i != 0 {
			mixin.Add(&mixin, big.NewInt(1)) // mixin++
		}
		encrypted := make([]byte, 16)
		ciph.Encrypt(encrypted, mixin.Bytes())
		pad = append(pad, encrypted...)
	}
	return mobytes.XOR(ciphertext, pad), nil
}
開發者ID:phayes,項目名稱:cryptocourse,代碼行數:14,代碼來源:main.go

示例13: encryptBlocks

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

示例14: blockEncrypt

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

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

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

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

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

示例15: tabulateL

// tabulateL - calculate L_i for messages up to a length of m cipher blocks
func tabulateL(bc cipher.Block, m int) [][]byte {
	/* set L0 = 2*AESenc(K; 0) */
	eZero := make([]byte, 16)
	Li := make([]byte, 16)
	bc.Encrypt(Li, eZero)

	LTable := make([][]byte, m)
	// Allocate pool once and slice into m pieces in the loop
	pool := make([]byte, m*16)
	for i := 0; i < m; i++ {
		multByTwo(Li, Li)
		LTable[i] = pool[i*16 : (i+1)*16]
		copy(LTable[i], Li)
	}
	return LTable
}
開發者ID:ncw,項目名稱:rclone,代碼行數:17,代碼來源:eme.go


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