C
C
chincharovpc2022-02-09 14:40:19
go
chincharovpc, 2022-02-09 14:40:19

How to encrypt data in parts?

I receive data in parts (about 10 parts)
It is necessary to encrypt each part and write it to one file.

When decrypted, only the first part is correctly decrypted.
As I understand it, additional bytes are added during encryption, so the decryptor does not work correctly.
How to encrypt correctly?

I use the DES algorithm (you need to use it)
Encryption function

func Encrypt(key, iv, plainText []byte) (ciphertext []byte, err error) {
  block, err := des.NewCipher(key)
  if err != nil {
    return nil, err
  }

  blockSize := block.BlockSize()
  origData := pkcs5Padding(plainText, blockSize)
  blockMode := cipher.NewCBCEncrypter(block, iv)
  cryted := make([]byte, len(origData))
  blockMode.CryptBlocks(cryted, origData)

  return cryted, nil
}

func pkcs5Padding(src []byte, blockSize int) []byte {
  padding := blockSize - len(src)%blockSize
  padtext := bytes.Repeat([]byte{byte(padding)}, padding)

  return append(src, padtext...)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hint000, 2022-02-09
@hint000

Before each encrypted part, write the length of the plaintext part to the file (in some fixed format, for example 4 bytes, if the length of the pieces does not exceed 4 GB). When decrypting, use these values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question