Skip to content

Instantly share code, notes, and snippets.

@islishude
Created February 28, 2019 06:07
Show Gist options
  • Save islishude/acb3a544f9d582875c9b6e83a3f01ce2 to your computer and use it in GitHub Desktop.
Save islishude/acb3a544f9d582875c9b6e83a3f01ce2 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"log"
)
func main() {
{
iv := make([]byte, 16)
io.ReadFull(rand.Reader, iv)
fmt.Println(iv)
key, _ := hex.DecodeString("6368616e676520746869732070617373")
plaintext := []byte("some plaintext")
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
stream := cipher.NewCTR(block, iv)
ciphertext := make([]byte, len(plaintext))
stream.XORKeyStream(ciphertext, plaintext)
fmt.Println(plaintext)
fmt.Println(ciphertext)
}
fmt.Println()
{
key, _ := hex.DecodeString("6368616e676520746869732070617373")
plaintext := []byte("some plaintext")
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
iv := make([]byte, 16)
io.ReadFull(rand.Reader, iv)
blockcbc := cipher.NewCBCEncrypter(block, iv)
paddingSize := blockcbc.BlockSize() - len(plaintext)%blockcbc.BlockSize()
padding := bytes.Repeat([]byte{byte(paddingSize)}, paddingSize)
plaintext = append(plaintext, padding...)
ciphertext := make([]byte, len(plaintext))
blockcbc.CryptBlocks(ciphertext, plaintext)
fmt.Println(plaintext)
fmt.Println(ciphertext)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment