Ejemplo n.º 1
0
func (e *Block) DecryptCBC(input, iv []byte) ([]byte, error) {
	length := len(input)
	plaintext := make([]byte, length)

	if len(iv) != e.BlockSize {
		return plaintext, IVSizeError
	}

	// Loop over each block
	for a, b := 0, e.BlockSize; b <= length; a, b = a+e.BlockSize, b+e.BlockSize {
		plainslice := make([]byte, e.BlockSize)
		plainslice, err := e.DecryptCBCBlock(input[a:b], iv)
		if err != nil {
			return plaintext, err
		}

		copy(plaintext[a:b], plainslice)

		iv = input[a:b]
	}

	if conversion.DetectPKCS(plaintext) {
		plaintext = conversion.UnPadPKCS(plaintext)
	}

	return plaintext, nil
}
Ejemplo n.º 2
0
func (e *Block) DecryptECB(input []byte) ([]byte, error) {
	length := len(input)
	plaintext := make([]byte, length)

	// Loop over each block
	for a, b := 0, e.BlockSize; b <= length; a, b = a+e.BlockSize, b+e.BlockSize {
		plainslice := plaintext[a:b]
		plainslice, err := e.DecryptECBBlock(input[a:b])
		if err != nil {
			return plaintext, err
		}

		copy(plaintext[a:b], plainslice)
	}

	if conversion.DetectPKCS(plaintext) {
		plaintext = conversion.UnPadPKCS(plaintext)
	}

	return plaintext, nil
}