In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
The DES, 3DES and AES encryption algorithms introduced earlier can only encrypt plaintext of fixed length. If you need to encrypt any length of plaintext, you need to encrypt the plaintext in groups. DES, 3DES, AES and so on are also called block ciphers, and there are many modes of grouping, such as: ECB mode, CBC mode, CFB mode, OFB mode, CTR mode, which will be introduced one by one below.
ECB mode
ECB mode, the full name of Electronic Codebook mode, is translated into electronic codebook mode, that is, plaintext is independently encrypted in groups with the same password. ECB mode is the simplest mode, because the same plaintext grouping is encrypted into the same ciphertext grouping, so there is some risk.
The following is a schematic diagram of the ECB mode:
In addition, when the content of the last plaintext packet is less than the packet length, it needs to be filled with specific data.
CBC mode
CBC mode, the full name of Cipher Block Chaining mode, is translated into ciphertext packet link mode, that is, the input of the encryption algorithm is the XOR of the previous ciphertext packet and the next plaintext packet. Because the contents of the previous ciphertext packet and the next plaintext packet are mixed and encrypted, the defect of ECB mode can be avoided. When encrypting the first plaintext packet, since there is no previous ciphertext packet, it is necessary to prepare an initialization vector IV as long as the packet to replace the previous ciphertext packet.
The following is a schematic diagram of the CBC mode:
The CBC schema code in the go standard library is as follows:
Type cbc struct {/ / b is the encryption algorithm For example, plaintext packet length blockSize int / / initialization vector IV iv [] byte / / temporary variable tmp [] byte} type cbcEncrypter cbc// supported by DES and AES b Block / / encryption algorithms specify the encryption algorithm and IVfunc NewCBCEncrypter (b Block) Iv [] byte) BlockMode {if len (iv)! = b.BlockSize () {panic ("cipher.NewCBCEncrypter: IV length must equal block size")} if cbc, ok: = b. (cbcEncAble) Ok {return cbc.NewCBCEncrypter (iv)} return (* cbcEncrypter) (newCBC (b, iv))} / / encrypt func (x * cbcEncrypter) CryptBlocks (dst, src [] byte) {if len (src)% x.blockSize! = 0 {panic ("crypto/cipher: input not full blocks")} if len (dst)
< len(src) { panic("crypto/cipher: output smaller than input") } iv := x.iv for len(src) >0 {/ / XOR of the previous ciphertext packet and the next plaintext packet / / when encrypting the first plaintext packet Use the initialization vector IV xorBytes (dst [: x.blockSize], src [: x.blockSize], iv) / / to execute the encryption algorithm x.b.Encrypt (dst [: x.blockSize], dst [: x.blockSize]) iv = dst [: x.blockSize] src = src [x.blockSize:] dst = dst [x.blockSize:]} copy (x.iv Iv)} / / Code location src/crypto/cipher/cbc.goCFB mode
CFB mode, the full name of Cipher FeedBack mode, is translated into ciphertext feedback mode, that is, the last ciphertext packet is used as the input of the encryption algorithm, and the ciphertext output is different from the plaintext or as the next packet. In CFB mode, there is only one XOR between plaintext grouping and ciphertext grouping.
The following is a schematic diagram of the CFB mode:
CFB mode is similar to one-time codebook, which generates ciphertext by XOR operation between plaintext and random bit sequence. However, because the output of the cryptographic algorithm in CFB mode is obtained by calculation, it is not a real random number, so it does not have the property that the one-time password book is theoretically unbreakable. CFB mode can be seen as a way to implement stream ciphers using grouping.
The CFB schema code in the go standard library is as follows:
Type cfb struct {/ / encryption algorithm b Block / / input next [] byte / / encrypted output out [] byte outUsed int decrypt bool} / / encryption or decryption / / decrypt indicates decryption of func (x * cfb) XORKeyStream (dst) for true Src [] byte) {for len (src) > 0 {if x.outUsed = = len (x.out) {x.b.Encrypt (x.out, x.next) x.outUsed = 0} if x.decrypt {copy (x.next[ x.outUsed:] Src)} / / encrypted output is different from plaintext or ciphertext as the next packet n: = xorBytes (dst, src, x.out [x.outUsed:]) if! x.decrypt {/ / the last ciphertext packet is used as input to the encryption algorithm copy (x.next[ x.outUsed:] Dst)} dst = dst [n:] src = src [n:] x.outUsed + = n}} / / func NewCFBEncrypter (block Block, iv [] byte) Stream {return newCFB (block, iv, false)} / / decryptor func NewCFBDecrypter (block Block, iv [] byte) Stream {return newCFB (block, iv, true)} func newCFB (block Block, iv [] byte Decrypt bool) Stream {/ / packet length blockSize: = block.BlockSize () if len (iv)! = blockSize {/ / initialization vector requires equal length to packet length panic ("cipher.newCFB: IV length must equal block size")} x: = & cfb {b: block, out: make ([] byte, blockSize), next: make ([] byte, blockSize) OutUsed: blockSize, decrypt: decrypt,} / / encrypted input copy (x.next, iv) return x} / / Code location src/crypto/cipher/cfb.goOFB mode
OFB mode, the full name of Output Feedback mode, is translated as output feedback mode. OFB mode is similar to CFB mode, except that the input to the encryption algorithm is the output of the last encryption. In OFB mode, the key stream required by XOR can be generated by cryptographic algorithm in advance, that is, the operation of generating keystream can be parallel to XOR operation.
OFB mode encryption and processing decryption logic is the same, plaintext and key flow may generate ciphertext, ciphertext and key flow may generate plaintext.
The following is a schematic diagram of the OFB mode:
The OFB schema code in the go standard library is as follows:
Type ofb struct {/ / encryption algorithm b Block / / input cipher [] byte out [] byte outUsed int} func NewOFB (b Block, iv [] byte) Stream {/ / packet length blockSize: = b.BlockSize () if len (iv)! = blockSize {return nil} / / const streamBufferSize = 512 bufSize: = streamBufferSize if bufSize
< blockSize { bufSize = blockSize } x := &ofb{ b: b, cipher: make([]byte, blockSize), out: make([]byte, 0, bufSize), outUsed: 0, } //加密的输入 copy(x.cipher, iv) return x}//生成密钥流func (x *ofb) refill() { bs := x.b.BlockSize() remain := len(x.out) - x.outUsed if remain >X.outUsed {return} copy (x.out, x.out [x.outUsed:]) x.out = x.out [: cap (x.out)] for remain
< len(x.out)-bs { x.b.Encrypt(x.cipher, x.cipher) copy(x.out[remain:], x.cipher) remain += bs } x.out = x.out[:remain] x.outUsed = 0}func (x *ofb) XORKeyStream(dst, src []byte) { for len(src) >0 {if x.outUsed > = len (x.out)-x.b.BlockSize () {/ / generate key stream x.refill ()} / / XOR with key stream n: = xorBytes (dst, src) X.out [x.outUsed:]) dst = dst [n:] src = src [n:] x.outUsed + = n}} / / Code location src/crypto/cipher/ofb.goCTR mode
CTR mode, full name Counter mode, translated as counter mode. In CTR mode, each packet corresponds to a counter that accumulates one by one, and a key stream is generated by encrypting the counter. That is to say, the final ciphertext packet is obtained by XOR operation between the bit sequence encrypted by the counter and the plaintext packet.
The following is a schematic diagram of the CTR mode:
The CTR schema code in the go standard library is as follows:
Type ctr struct {/ / encryption algorithm b Block / / input ctr [] byte out [] byte outUsed int} const streamBufferSize = 512type ctrAble interface {NewCTR (iv [] byte) Stream} func NewCTR (block Block, iv [] byte) Stream {if ctr, ok: = block. (ctrAble) Ok {return ctr.NewCTR (iv)} if len (iv)! = block.BlockSize () {panic ("cipher.NewCTR: IV length must equal block size")} bufSize: = streamBufferSize if bufSize
< block.BlockSize() { bufSize = block.BlockSize() } return &ctr{ b: block, ctr: dup(iv), out: make([]byte, 0, bufSize), outUsed: 0, }}//生成密钥流func (x *ctr) refill() { remain := len(x.out) - x.outUsed copy(x.out, x.out[x.outUsed:]) x.out = x.out[:cap(x.out)] bs := x.b.BlockSize() for remain = 0; i-- { x.ctr[i]++ if x.ctr[i] != 0 { break } } } x.out = x.out[:remain] x.outUsed = 0}func (x *ctr) XORKeyStream(dst, src []byte) { for len(src) >0 {if x.outUsed > = len (x.out)-x.b.BlockSize () {/ / generate key stream x.refill ()} / / XOR with key stream n: = xorBytes (dst, src) The AES encryption implementation of CBC mode in x.out [x.outUsed:]) dst = dst [n:] src = src [n:] x.outUsed + = n}} Fabric
The code is as follows:
/ / AES encryption, CBC mode, PKCS7 padding algorithm func AESCBCPKCS7Encrypt (key, src [] byte) ([] byte, error) {/ / PKCS7 padding algorithm tmp: = pkcs7Padding (src) / / AES encryption, CBC mode return aesCBCEncrypt (key, tmp)} / / PKCS7 padding algorithm / / PKCS7 means that the padding string consists of a sequence of bytes Func pkcs7Padding (src [] byte) [] byte {padding: = aes.BlockSize-len (src)% aes.BlockSize padtext: = bytes.Repeat ([] byte {byte (padding)}, padding) return append (src, padtext...)} / / AES encryption, CBC mode func aesCBCEncrypt (key, s [] byte) ([] byte, error) {if len (s)% aes.BlockSize! = 0 {return nil Errors.New ("Invalid plaintext. It must be a multiple of the block size ")} block, err: = aes.NewCipher (key) if err! = nil {return nil, err} ciphertext: = make ([] byte, aes.BlockSize+len (s)) / / initial vector IV iv: = ciphertext [: aes.BlockSize] if _, err: = io.ReadFull (rand.Reader, iv) Err! = nil {return nil, err} mode: = cipher.NewCBCEncrypter (block, iv) mode.CryptBlocks (ciphertext [aes.BlockSize:], s) return ciphertext, nil} / / Code location github.com/hyperledger/fabric/bccsp/sw/aes.go postscript
The ECB model should no longer be used because of its high risk. CBC mode, CFB mode, OFB mode, CTR mode can be used. CBC mode is used in Fabric.
To be continued.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.