Example #1
0
func DecryptCBC(ciph cipher.Block, iv []byte, ciphertext []byte) ([]byte, error) {
	var resultblocks [][]byte
	blocks := mobytes.SplitEvery(ciphertext, 16)
	for i, block := range blocks {
		decrypted := make([]byte, 16)
		ciph.Decrypt(decrypted, block)
		if i == 0 {
			decrypted = mobytes.XOR(decrypted, iv)
		} else {
			decrypted = mobytes.XOR(decrypted, blocks[i-1])
		}
		resultblocks = append(resultblocks, decrypted)
	}
	return bytes.Join(resultblocks, nil), nil
}
Example #2
0
func DecryptCTR(ciph cipher.Block, iv []byte, ciphertext []byte) ([]byte, error) {
	var pad []byte
	var mixin big.Int
	mixin.SetBytes(iv)
	for i := 0; i <= len(ciphertext)/16; i++ {
		if i != 0 {
			mixin.Add(&mixin, big.NewInt(1)) // mixin++
		}
		encrypted := make([]byte, 16)
		ciph.Encrypt(encrypted, mixin.Bytes())
		pad = append(pad, encrypted...)
	}
	return mobytes.XOR(ciphertext, pad), nil
}