예제 #1
0
파일: cbc.go 프로젝트: leijou/matasano-go
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
}
예제 #2
0
파일: ecb.go 프로젝트: leijou/matasano-go
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
}