Пример #1
0
func TestEncryptAES_ECB_Manual(t *testing.T) {

	key := "YELLOW SUBMARINE"

	expectedHexCipher := "d1aa4f6578926542fbb6dd876cd20508"
	expectedHexCipherPadding := "d1aa4f6578926542fbb6dd876cd20508be02542eda8a4ae9cd80e9ce20751237"

	text := "YELLOW SUBMARINE"
	textPadding := "YELLOW SUBMARINE!"

	cipherText := EncryptAES_ECB_Manual(text, key)
	cipherTextHex := secutils.BytesToHex(cipherText)
	if expectedHexCipher != cipherTextHex {
		t.Error("Test Encrypt AES_ECB Manually FAILED !")
	}
	fmt.Printf("3. Test EncryptAES_ECB_Manual: %s\n", cipherTextHex)

	cipherTextPadding := EncryptAES_ECB_Manual(textPadding, key)
	cipherTextHexPadding := secutils.BytesToHex(cipherTextPadding)
	if expectedHexCipherPadding != cipherTextHexPadding {
		t.Error("Test Encrypt AES_ECB Manually + PADDING FAILED !")
	}
	fmt.Printf("3. Test EncryptAES_ECB_Manual + PADDING: %s\n", cipherTextHexPadding)

}
Пример #2
0
func TestEncryptAES_CBC_Manual(t *testing.T) {
	text := "YELLOW SUBMARINE"
	key := "YELLOW SUBMARINE"
	cipherTextBytes := EncryptAES_CBC_Manual(text, key)
	cipherHex := secutils.BytesToHex(cipherTextBytes)
	fmt.Printf("5. Test EncryptAES_CBC Manual: %s\n", cipherHex)
}
Пример #3
0
// THIS IS PROBABLY ONE OF THE BEST
func EncryptAES_CTR(plainText, key string) (string, error) {

	keyBytes := secutils.ASCIIStringToBytes(key)
	plaintextBytes := secutils.ASCIIStringToBytes(plainText)

	block, err := aes.NewCipher(keyBytes)
	if err != nil {
		panic(err)
	}

	// The IV needs to be unique, but not secure. Therefore it's common to
	// include it at the beginning of the ciphertextBytes.
	ciphertextBytes := make([]byte, BlockSize+len(plaintextBytes))
	iv := ciphertextBytes[:BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}

	stream := cipher.NewCTR(block, iv)
	stream.XORKeyStream(ciphertextBytes[BlockSize:], plaintextBytes)
	hexCipherText := secutils.BytesToHex(ciphertextBytes)
	return hexCipherText, nil

	// CTR mode is the same for both encryption and decryption, so we can
	// also decrypt that ciphertextBytes with NewCTR.

	// plaintext2 := make([]byte, len(plaintextBytes))
	// stream = cipher.NewCTR(block, iv)
	// stream.XORKeyStream(plaintext2, ciphertextBytes[aes.BlockSize:])

}