Blocks To Go …
Published in
2 min readNov 2, 2019
With block encryption we take blocks of data and encrypt each block. DES, for example, uses a 64-bit block size (8 bytes) and AES uses a 128-bit block size (16 bytes). Often the data will not fill all of the blocks so we need to pack bytes into the data. We then encrypt, and then decrypt. After this we then unpad the padding bytes. Three common methods are:
- PKCS7. This pads with the value which equals the number of padding bytes.
- ANSI X.923. This pads with zero until the last byte, and which is the number of padding bytes).
- ISO 10126. This pads with random bytes until the last byte, and which defines the number of padding bytes.
So let’s use Golang to create a demo [here]:
package mainimport (
"strconv"
"github.com/enceve/crypto/pad"
"fmt"
"os"
)
func main() { msg:="Hello"
argCount := len(os.Args[1:])
blocksize := 16 if (argCount>0) {msg = string(os.Args[1])}
if (argCount>1) {blocksize,_ = strconv.Atoi(os.Args[2])} m := []byte(msg)
pkcs7 := pad.NewPKCS7(blocksize)
pad1 := pkcs7.Pad(m)
fmt.Printf("PKCS7 Pad:\t%x",pad1)
res,_:=pkcs7.Unpad(pad1)
fmt.Printf("\n Unpad:\t%s",res) x923 := pad.NewX923(blocksize)…