Skip to content

Instantly share code, notes, and snippets.

@G33kNoob
Forked from DaaaaanB/AES_Example.go
Created July 2, 2022 13:28
Show Gist options
  • Select an option

  • Save G33kNoob/813f255107187daa0444e4c88e606b90 to your computer and use it in GitHub Desktop.

Select an option

Save G33kNoob/813f255107187daa0444e4c88e606b90 to your computer and use it in GitHub Desktop.

Revisions

  1. @DaaaaanB DaaaaanB revised this gist Apr 14, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions AES_Example.go
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,9 @@
    * FIRST VERSION : 2020-04-12
    * DESCRIPTION :
    * The function(s) in this file make up example code for encryption and decryption of a block of text
    * using the Golang standard library AES implementation. DISCLAIMER: There is no way that this a secure
    * implementation of AES. This is only for my personal learning. So help you God if this ends up in
    * some commercial application.
    * using the Golang standard library AES implementation using the Cipher Feedback mode of encryption (CFB).
    * DISCLAIMER: There is no way that this a secure implementation of AES. This is only for my personal learning.
    * So help you God if this ends up in some commercial application.
    */
    package main

  2. @DaaaaanB DaaaaanB revised this gist Apr 13, 2020. 1 changed file with 24 additions and 24 deletions.
    48 changes: 24 additions & 24 deletions AES_Example.go
    Original file line number Diff line number Diff line change
    @@ -71,17 +71,17 @@ func main() {
    }

    /*
    * FUNCTION : encrypt
    * DESCRIPTION :
    * This function takes a string and a cipher key and uses AES to encrypt the message
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string message : String containing the message to encrypt
    *
    * RETURNS :
    * string encoded : String containing the encoded user input
    * error err : Error message
    * FUNCTION : encrypt
    * DESCRIPTION :
    * This function takes a string and a cipher key and uses AES to encrypt the message
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string message : String containing the message to encrypt
    *
    * RETURNS :
    * string encoded : String containing the encoded user input
    * error err : Error message
    */
    func encrypt(key []byte, message string) (encoded string, err error) {
    //Create byte array from the input string
    @@ -109,25 +109,25 @@ func encrypt(key []byte, message string) (encoded string, err error) {
    stream.XORKeyStream(cipherText[aes.BlockSize:], plainText)

    //Return string encoded in base64
    return base64.URLEncoding.EncodeToString(cipherText), err
    return base64.RawStdEncoding.EncodeToString(cipherText), err
    }

    /*
    * FUNCTION : decrypt
    * DESCRIPTION :
    * This function takes a string and a key and uses AES to decrypt the string into plain text
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string secure : String containing an encrypted message
    *
    * RETURNS :
    * string decoded : String containing the decrypted equivalent of secure
    * error err : Error message
    * FUNCTION : decrypt
    * DESCRIPTION :
    * This function takes a string and a key and uses AES to decrypt the string into plain text
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string secure : String containing an encrypted message
    *
    * RETURNS :
    * string decoded : String containing the decrypted equivalent of secure
    * error err : Error message
    */
    func decrypt(key []byte, secure string) (decoded string, err error) {
    //Remove base64 encoding:
    cipherText, err := base64.URLEncoding.DecodeString(secure)
    cipherText, err := base64.RawStdEncoding.DecodeString(secure)

    //IF DecodeString failed, exit:
    if err != nil {
  3. @DaaaaanB DaaaaanB revised this gist Apr 13, 2020. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions AES_Example.go
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * FILE : AES_Example.go
    * PROJECT : INFO-1340 - Block Ciphers
    * PROGRAMMER : Daniel Pieczewski, ref: https://github.com/mickelsonm
    * FIRST VERSION : 2020-04-12
    * FIRST VERSION : 2020-04-12
    * DESCRIPTION :
    * The function(s) in this file make up example code for encryption and decryption of a block of text
    * using the Golang standard library AES implementation. DISCLAIMER: There is no way that this a secure
    @@ -71,17 +71,17 @@ func main() {
    }

    /*
    * FUNCTION : encrypt
    * DESCRIPTION :
    * FUNCTION : encrypt
    * DESCRIPTION :
    * This function takes a string and a cipher key and uses AES to encrypt the message
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string message : String containing the message to encrypt
    *
    * RETURNS :
    * RETURNS :
    * string encoded : String containing the encoded user input
    * error err : Error message
    * error err : Error message
    */
    func encrypt(key []byte, message string) (encoded string, err error) {
    //Create byte array from the input string
    @@ -113,17 +113,17 @@ func encrypt(key []byte, message string) (encoded string, err error) {
    }

    /*
    * FUNCTION : decrypt
    * DESCRIPTION :
    * FUNCTION : decrypt
    * DESCRIPTION :
    * This function takes a string and a key and uses AES to decrypt the string into plain text
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string secure : String containing an encrypted message
    *
    * RETURNS :
    * RETURNS :
    * string decoded : String containing the decrypted equivalent of secure
    * error err : Error message
    * error err : Error message
    */
    func decrypt(key []byte, secure string) (decoded string, err error) {
    //Remove base64 encoding:
  4. @DaaaaanB DaaaaanB revised this gist Apr 13, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion AES_Example.go
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@
    * FIRST VERSION : 2020-04-12
    * DESCRIPTION :
    * The function(s) in this file make up example code for encryption and decryption of a block of text
    * using the Golang standard library AES implementation.
    * using the Golang standard library AES implementation. DISCLAIMER: There is no way that this a secure
    * implementation of AES. This is only for my personal learning. So help you God if this ends up in
    * some commercial application.
    */
    package main

  5. @DaaaaanB DaaaaanB revised this gist Apr 13, 2020. No changes.
  6. @DaaaaanB DaaaaanB revised this gist Apr 13, 2020. 2 changed files with 157 additions and 82 deletions.
    157 changes: 157 additions & 0 deletions AES_Example.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,157 @@
    /*
    * FILE : AES_Example.go
    * PROJECT : INFO-1340 - Block Ciphers
    * PROGRAMMER : Daniel Pieczewski, ref: https://github.com/mickelsonm
    * FIRST VERSION : 2020-04-12
    * DESCRIPTION :
    * The function(s) in this file make up example code for encryption and decryption of a block of text
    * using the Golang standard library AES implementation.
    */
    package main

    import (
    "bufio"
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "errors"
    "fmt"
    "io"
    "log"
    "os"
    )

    func main() {
    cipherKey := []byte("asuperstrong32bitpasswordgohere!") //32 bit key for AES-256
    //cipherKey := []byte("asuperstrong24bitpasswor") //24 bit key for AES-192
    //cipherKey := []byte("asuperstrong16bi") //16 bit key for AES-128

    reader := bufio.NewReader(os.Stdin)

    var message string

    //IF no command line argument is given:
    if len(os.Args) != 2 {
    //Get user input
    fmt.Printf("\n\tNo command line argument found, getting user input\n")
    fmt.Printf("\tEnter a string to test: ")
    message, _ = reader.ReadString('\n')
    } else { //Make the message equal to the command line argument
    message = os.Args[1]
    }

    //Encrypt the text:
    encrypted, err := encrypt(cipherKey, message)

    //IF the encryption failed:
    if err != nil {
    //Print error message:
    log.Println(err)
    os.Exit(-2)
    }

    //Print the key and cipher text:
    fmt.Printf("\n\tCIPHER KEY: %s\n", string(cipherKey))
    fmt.Printf("\tENCRYPTED: %s\n", encrypted)

    //Decrypt the text:
    decrypted, err := decrypt(cipherKey, encrypted)

    //IF the decryption failed:
    if err != nil {
    log.Println(err)
    os.Exit(-3)
    }

    //Print re-decrypted text:
    fmt.Printf("\tDECRYPTED: %s\n\n", decrypted)
    }

    /*
    * FUNCTION : encrypt
    * DESCRIPTION :
    * This function takes a string and a cipher key and uses AES to encrypt the message
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string message : String containing the message to encrypt
    *
    * RETURNS :
    * string encoded : String containing the encoded user input
    * error err : Error message
    */
    func encrypt(key []byte, message string) (encoded string, err error) {
    //Create byte array from the input string
    plainText := []byte(message)

    //Create a new AES cipher using the key
    block, err := aes.NewCipher(key)

    //IF NewCipher failed, exit:
    if err != nil {
    return
    }

    //Make the cipher text a byte array of size BlockSize + the length of the message
    cipherText := make([]byte, aes.BlockSize+len(plainText))

    //iv is the ciphertext up to the blocksize (16)
    iv := cipherText[:aes.BlockSize]
    if _, err = io.ReadFull(rand.Reader, iv); err != nil {
    return
    }

    //Encrypt the data:
    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(cipherText[aes.BlockSize:], plainText)

    //Return string encoded in base64
    return base64.URLEncoding.EncodeToString(cipherText), err
    }

    /*
    * FUNCTION : decrypt
    * DESCRIPTION :
    * This function takes a string and a key and uses AES to decrypt the string into plain text
    *
    * PARAMETERS :
    * byte[] key : Byte array containing the cipher key
    * string secure : String containing an encrypted message
    *
    * RETURNS :
    * string decoded : String containing the decrypted equivalent of secure
    * error err : Error message
    */
    func decrypt(key []byte, secure string) (decoded string, err error) {
    //Remove base64 encoding:
    cipherText, err := base64.URLEncoding.DecodeString(secure)

    //IF DecodeString failed, exit:
    if err != nil {
    return
    }

    //Create a new AES cipher with the key and encrypted message
    block, err := aes.NewCipher(key)

    //IF NewCipher failed, exit:
    if err != nil {
    return
    }

    //IF the length of the cipherText is less than 16 Bytes:
    if len(cipherText) < aes.BlockSize {
    err = errors.New("Ciphertext block size is too short!")
    return
    }

    iv := cipherText[:aes.BlockSize]
    cipherText = cipherText[aes.BlockSize:]

    //Decrypt the message
    stream := cipher.NewCFBDecrypter(block, iv)
    stream.XORKeyStream(cipherText, cipherText)

    return string(cipherText), err
    }
    82 changes: 0 additions & 82 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -1,82 +0,0 @@
    package main

    import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "errors"
    "io"
    "log"
    )

    func main() {
    CIPHER_KEY := []byte("0123456789012345")
    msg := "A quick brown fox jumped over the lazy dog."

    if encrypted, err := encrypt(CIPHER_KEY, msg); err != nil {
    log.Println(err)
    } else {
    log.Printf("CIPHER KEY: %s\n", string(CIPHER_KEY))
    log.Printf("ENCRYPTED: %s\n", encrypted)

    if decrypted, err := decrypt(CIPHER_KEY, encrypted); err != nil {
    log.Println(err)
    } else {
    log.Printf("DECRYPTED: %s\n", decrypted)
    }
    }
    }

    func encrypt(key []byte, message string) (encmess string, err error) {
    plainText := []byte(message)

    block, err := aes.NewCipher(key)
    if err != nil {
    return
    }

    //IV needs to be unique, but doesn't have to be secure.
    //It's common to put it at the beginning of the ciphertext.
    cipherText := make([]byte, aes.BlockSize+len(plainText))
    iv := cipherText[:aes.BlockSize]
    if _, err = io.ReadFull(rand.Reader, iv); err != nil {
    return
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(cipherText[aes.BlockSize:], plainText)

    //returns to base64 encoded string
    encmess = base64.URLEncoding.EncodeToString(cipherText)
    return
    }

    func decrypt(key []byte, securemess string) (decodedmess string, err error) {
    cipherText, err := base64.URLEncoding.DecodeString(securemess)
    if err != nil {
    return
    }

    block, err := aes.NewCipher(key)
    if err != nil {
    return
    }

    if len(cipherText) < aes.BlockSize {
    err = errors.New("Ciphertext block size is too short!")
    return
    }

    //IV needs to be unique, but doesn't have to be secure.
    //It's common to put it at the beginning of the ciphertext.
    iv := cipherText[:aes.BlockSize]
    cipherText = cipherText[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    // XORKeyStream can work in-place if the two arguments are the same.
    stream.XORKeyStream(cipherText, cipherText)

    decodedmess = string(cipherText)
    return
    }
  7. @mickelsonm mickelsonm revised this gist Sep 15, 2014. 1 changed file with 48 additions and 41 deletions.
    89 changes: 48 additions & 41 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -5,71 +5,78 @@ import (
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "fmt"
    "errors"
    "io"
    "log"
    )

    func main() {
    originalText := "encrypt this golang"
    fmt.Println(originalText)

    key := []byte("example key 1234")

    // encrypt value to base64
    cryptoText := encrypt(key, originalText)
    fmt.Println(cryptoText)

    // encrypt base64 crypto to original value
    text := decrypt(key, cryptoText)
    fmt.Printf(text)
    CIPHER_KEY := []byte("0123456789012345")
    msg := "A quick brown fox jumped over the lazy dog."

    if encrypted, err := encrypt(CIPHER_KEY, msg); err != nil {
    log.Println(err)
    } else {
    log.Printf("CIPHER KEY: %s\n", string(CIPHER_KEY))
    log.Printf("ENCRYPTED: %s\n", encrypted)

    if decrypted, err := decrypt(CIPHER_KEY, encrypted); err != nil {
    log.Println(err)
    } else {
    log.Printf("DECRYPTED: %s\n", decrypted)
    }
    }
    }

    // encrypt string to base64 crypto using AES
    func encrypt(key []byte, text string) string {
    // key := []byte(keyText)
    plaintext := []byte(text)
    func encrypt(key []byte, message string) (encmess string, err error) {
    plainText := []byte(message)

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

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    ciphertext := make([]byte, aes.BlockSize+len(plaintext))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    panic(err)
    //IV needs to be unique, but doesn't have to be secure.
    //It's common to put it at the beginning of the ciphertext.
    cipherText := make([]byte, aes.BlockSize+len(plainText))
    iv := cipherText[:aes.BlockSize]
    if _, err = io.ReadFull(rand.Reader, iv); err != nil {
    return
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
    stream.XORKeyStream(cipherText[aes.BlockSize:], plainText)

    // convert to base64
    return base64.URLEncoding.EncodeToString(ciphertext)
    //returns to base64 encoded string
    encmess = base64.URLEncoding.EncodeToString(cipherText)
    return
    }

    // decrypt from base64 to decrypted string
    func decrypt(key []byte, cryptoText string) string {
    ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
    func decrypt(key []byte, securemess string) (decodedmess string, err error) {
    cipherText, err := base64.URLEncoding.DecodeString(securemess)
    if err != nil {
    return
    }

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

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    if len(ciphertext) < aes.BlockSize {
    panic("ciphertext too short")
    if len(cipherText) < aes.BlockSize {
    err = errors.New("Ciphertext block size is too short!")
    return
    }
    iv := ciphertext[:aes.BlockSize]
    ciphertext = ciphertext[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    //IV needs to be unique, but doesn't have to be secure.
    //It's common to put it at the beginning of the ciphertext.
    iv := cipherText[:aes.BlockSize]
    cipherText = cipherText[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)
    // XORKeyStream can work in-place if the two arguments are the same.
    stream.XORKeyStream(ciphertext, ciphertext)
    stream.XORKeyStream(cipherText, cipherText)

    return fmt.Sprintf("%s", ciphertext)
    }
    decodedmess = string(cipherText)
    return
    }
  8. @manishtpatel manishtpatel revised this gist Jan 2, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -46,12 +46,12 @@ func encrypt(key []byte, text string) string {
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

    // convert to base64
    return base64.StdEncoding.EncodeToString(ciphertext)
    return base64.URLEncoding.EncodeToString(ciphertext)
    }

    // decrypt from base64 to decrypted string
    func decrypt(key []byte, cryptoText string) string {
    ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText)
    ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)

    block, err := aes.NewCipher(key)
    if err != nil {
  9. @manishtpatel manishtpatel created this gist Jan 2, 2014.
    75 changes: 75 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    package main

    import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "fmt"
    "io"
    )

    func main() {
    originalText := "encrypt this golang"
    fmt.Println(originalText)

    key := []byte("example key 1234")

    // encrypt value to base64
    cryptoText := encrypt(key, originalText)
    fmt.Println(cryptoText)

    // encrypt base64 crypto to original value
    text := decrypt(key, cryptoText)
    fmt.Printf(text)
    }

    // encrypt string to base64 crypto using AES
    func encrypt(key []byte, text string) string {
    // key := []byte(keyText)
    plaintext := []byte(text)

    block, err := aes.NewCipher(key)
    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 ciphertext.
    ciphertext := make([]byte, aes.BlockSize+len(plaintext))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    panic(err)
    }

    stream := cipher.NewCFBEncrypter(block, iv)
    stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

    // convert to base64
    return base64.StdEncoding.EncodeToString(ciphertext)
    }

    // decrypt from base64 to decrypted string
    func decrypt(key []byte, cryptoText string) string {
    ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText)

    block, err := aes.NewCipher(key)
    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 ciphertext.
    if len(ciphertext) < aes.BlockSize {
    panic("ciphertext too short")
    }
    iv := ciphertext[:aes.BlockSize]
    ciphertext = ciphertext[aes.BlockSize:]

    stream := cipher.NewCFBDecrypter(block, iv)

    // XORKeyStream can work in-place if the two arguments are the same.
    stream.XORKeyStream(ciphertext, ciphertext)

    return fmt.Sprintf("%s", ciphertext)
    }