// The CBC mode and the ECB mode, there are strict limits on the size of the groups, // During encryption, if the last group of the plaintext does not meet the group size, // You need to fill in the group size // By convention, the value of the supplementary array to be filled // should be (block size - current size). // If the clear text is just big enough to be grouped, // then by convention you should also populate a whole set of data with a value of block size funcpaddingLastGroup(plaintext []byte, blockSize int) []byte { padNum := blockSize - len(plaintext)%blockSize char := []byte{byte(padNum)} newPlain := bytes.Repeat(char, padNum)
returnappend(plaintext, newPlain...) }
// According to the previous fill specification, // the fill data in the plaintext after decryption needs to be removed funcunPaddingLastGroup(plaintext []byte) []byte { length := len(plaintext) return plaintext[:length - int(plaintext[length - 1])] }
// The CBC mode and the ECB mode, there are strict limits on the size of the groups, // During encryption, if the last group of the plaintext does not meet the group size, // You need to fill in the group size // By convention, the value of the supplementary array to be filled // should be (block size - current size). // If the clear text is just big enough to be grouped, // then by convention you should also populate a whole set of data with a value of block size funcpaddingLastGroup(plaintext []byte, blockSize int) []byte { padNum := blockSize - len(plaintext)%blockSize char := []byte{byte(padNum)} newPlain := bytes.Repeat(char, padNum)
returnappend(plaintext, newPlain...) }
// According to the previous fill specification, // the fill data in the plaintext after decryption needs to be removed funcunPaddingLastGroup(plaintext []byte) []byte { length := len(plaintext) return plaintext[:length-int(plaintext[length-1])] }
funcdesEncrypt(plaintext, key, iv []byte) []byte { block, err := des.NewCipher(key) if err != nil { panic(err) }