padding

package
v0.0.0-...-bac2329 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

PCSK#7 填充读写流

直接使用分块加密,往往需要手动完成填充和去填充的过程。流程较为固定,但是比较繁琐,对于文件和流的处理,不是很友好。

PKCS#7填充和去除填充:

  • padding.PKCS7PaddingReader 写入结束是添加填充
  • padding.PKCS7PaddingWriter 读取时去除填充

封装的填充模式加密:

  • padding.P7BlockEnc
  • padding.P7BlockDecrypt

以上方法简化了,对文件和流类型的加密解密过程。

带填充的模式加密和解密

流:实现了 io.Writerio.Reader接口的类型。

流的SM4分块CBC模式加密:

func main() {
    src := bytes.Repeat([]byte{7}, 16)
    srcIn := bytes.NewBuffer(src)
    encOut := bytes.NewBuffer(make([]byte, 0, 1024))

    key := make([]byte, 16)
    iv := make([]byte, 16)
    _, _ = rand.Read(key)
    _, _ = rand.Read(iv)
    fmt.Printf("key: %02X\n", key)
    fmt.Printf("iv : %02X\n", iv)
    block, err := sm4.NewCipher(key)
    if err != nil {
        panic(err)
    }
    encrypter := cipher.NewCBCEncrypter(block, iv)
    // P7填充的CBC加密
    err = padding.P7BlockEnc(encrypter, srcIn, encOut)
    if err != nil {
        panic(err)
    }
    fmt.Printf("原文: %02X\n", src)
    fmt.Printf("加密: %02X\n", encOut.Bytes())
}

流的SM4分块CBC模式解密:

func main() {
    /**
	key: 4C9CA3D17263F6F558D65ADB561465BD
	iv : 221908D1C4BD730BEB011319D1368E49
	原文: 07070707070707070707070707070707
	加密: 310CA2472DCE15CCC58E1BE69B876002F443556CCFB86B1BA0341B6BFBED4C1A
     */
	encOut := bytes.NewBuffer(make([]byte, 0, 1024))
	key,_ := hex.DecodeString("4C9CA3D17263F6F558D65ADB561465BD")
    iv,_ := hex.DecodeString("221908D1C4BD730BEB011319D1368E49")
    block, err := sm4.NewCipher(key)
    if err != nil {
        panic(err)
    }
    ciphertext, _ :=  hex.DecodeString("310CA2472DCE15CCC58E1BE69B876002F443556CCFB86B1BA0341B6BFBED4C1A")
    cipherReader := bytes.NewReader(ciphertext)
    decrypter := cipher.NewCBCDecrypter(block, iv)
    decOut := bytes.NewBuffer(make([]byte, 0, 1024))
    err = padding.P7BlockDecrypt(decrypter, ciphertext, decOut)
    if err != nil {
        panic(err)
    }
    
    fmt.Printf("解密: %02X\n", decOut.Bytes())
}

PKCS#7填充

见测试用例: pkcs7_padding_io_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func P7BlockDecrypt

func P7BlockDecrypt(decrypter cipher.BlockMode, in io.Reader, out io.Writer) error

P7BlockDecrypt 解密密文,并去除PKCS#7填充 decrypter: 块解密器 in: 密文输入流 out: 明文输出流

func P7BlockEnc

func P7BlockEnc(encrypter cipher.BlockMode, in io.Reader, out io.Writer) error

P7BlockEnc 以PKCS#7填充模式填充原文,并加密输出 encrypter: 块加密器 in: 明文输入流 out: 密文输出流

Types

type PKCS7PaddingReader

type PKCS7PaddingReader struct {
	// contains filtered or unexported fields
}

PKCS7PaddingReader 符合PKCS#7填充的输入流

func NewPKCS7PaddingReader

func NewPKCS7PaddingReader(in io.Reader, blockSize int) *PKCS7PaddingReader

NewPKCS7PaddingReader 创建PKCS7填充Reader in: 输入流 blockSize: 分块大小

func (*PKCS7PaddingReader) Read

func (p *PKCS7PaddingReader) Read(buf []byte) (int, error)

type PKCS7PaddingWriter

type PKCS7PaddingWriter struct {
	// contains filtered or unexported fields
}

PKCS7PaddingWriter 符合PKCS#7去除的输入流,最后一个 分组根据会根据填充情况去除填充。

func NewPKCS7PaddingWriter

func NewPKCS7PaddingWriter(out io.Writer, blockSize int) *PKCS7PaddingWriter

NewPKCS7PaddingWriter PKCS#7 填充Writer 可以去除填充

func (*PKCS7PaddingWriter) Final

func (p *PKCS7PaddingWriter) Final() error

Final 去除填充写入最后一个分块

func (*PKCS7PaddingWriter) Write

func (p *PKCS7PaddingWriter) Write(buff []byte) (n int, err error)

Write 保留一个填充大小的数据,其余全部写入输出中

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL