本文整理汇总了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)
}
}
}
示例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
}
示例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
}
示例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
}
示例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])
}
}
示例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
}
}
示例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
}
示例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]
}
示例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[:])
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}