aes

package
v1.2.38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CBCDecrypt

func CBCDecrypt(ciphertext, key []byte) ([]byte, error)

func CBCEncrypt

func CBCEncrypt(key, plaintext []byte, iv []byte) ([]byte, error)
Example
package main

import (
	"encoding/hex"
	"fmt"

	aes_ "github.com/searKing/golang/go/crypto/aes"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	plaintext := []byte("exampleplaintext")

	ciphertext, err := aes_.CBCEncrypt(key, plaintext, nil)
	if err != nil {
		panic(err)
	}

	// It's important to remember that ciphertexts must be authenticated
	// (i.e. by using crypto/hmac) as well as being encrypted in order to
	// be secure.

	fmt.Println("encrypted:")
	fmt.Printf("%x\n", ciphertext)

	plaintext, err = aes_.CBCDecrypt(ciphertext, key)
	if err != nil {
		panic(err)
	}
	fmt.Println("decrypted:")
	fmt.Printf("%s\n", string(plaintext))
}
Output:

encrypted:
00000000000000000000000000000000f42512e1e4039213bd449ba47faa1b7408eac45dbf536e5016511f86035707c6
decrypted:
exampleplaintext

func CBCEncryptRandom

func CBCEncryptRandom(key, plaintext []byte) ([]byte, error)

func CFBDecrypt

func CFBDecrypt(ciphertext, key []byte) ([]byte, error)

func CFBEncrypt

func CFBEncrypt(key, plaintext []byte, iv []byte) ([]byte, error)

func CFBEncryptRandom

func CFBEncryptRandom(key, plaintext []byte) ([]byte, error)

func CTRDecrypt

func CTRDecrypt(ciphertext, key []byte) ([]byte, error)

func CTREncrypt

func CTREncrypt(key, plaintext []byte, iv []byte) ([]byte, error)
Example
package main

import (
	"encoding/hex"
	"fmt"

	aes_ "github.com/searKing/golang/go/crypto/aes"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	plaintext := []byte("exampleplaintext")

	ciphertext, err := aes_.CTREncrypt(key, plaintext, nil)
	if err != nil {
		panic(err)
	}

	// It's important to remember that ciphertexts must be authenticated
	// (i.e. by using crypto/hmac) as well as being encrypted in order to
	// be secure.

	fmt.Println("encrypted:")
	fmt.Printf("%x\n", ciphertext)

	plaintext, err = aes_.CTRDecrypt(ciphertext, key)
	if err != nil {
		panic(err)
	}
	fmt.Println("decrypted:")
	fmt.Printf("%s\n", string(plaintext))
}
Output:

encrypted:
00000000000000000000000000000000d91399c43f6adaef3d909876e79904a93b1144928bc98a4e78f38c23b706610a
decrypted:
exampleplaintext

func CTREncryptRandom

func CTREncryptRandom(key, plaintext []byte) ([]byte, error)

func GCMDecrypt

func GCMDecrypt(ciphertext, key []byte) ([]byte, error)

func GCMEncrypt

func GCMEncrypt(key, plaintext []byte, nonce []byte) ([]byte, error)
Example
package main

import (
	"encoding/hex"
	"fmt"

	aes_ "github.com/searKing/golang/go/crypto/aes"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	plaintext := []byte("exampleplaintext")

	ciphertext, err := aes_.GCMEncrypt(key, plaintext, nil)
	if err != nil {
		panic(err)
	}

	// It's important to remember that ciphertexts must be authenticated
	// (i.e. by using crypto/hmac) as well as being encrypted in order to
	// be secure.

	fmt.Println("encrypted:")
	fmt.Printf("%x\n", ciphertext)

	plaintext, err = aes_.GCMDecrypt(ciphertext, key)
	if err != nil {
		panic(err)
	}
	fmt.Println("decrypted:")
	fmt.Printf("%s\n", string(plaintext))
}
Output:

encrypted:
0000000000000000000000000b2591cb60a33bdfeb61a0d35207a4196f1e5d1f6fe2b32198e09765ee28bd17d08567996caca50cd7049c07d1db15b5
decrypted:
exampleplaintext

func GCMEncryptRandom

func GCMEncryptRandom(key, plaintext []byte) ([]byte, error)

func OFBDecrypt

func OFBDecrypt(ciphertext, key []byte) ([]byte, error)

func OFBEncrypt

func OFBEncrypt(key, plaintext, iv []byte) ([]byte, error)
Example
package main

import (
	"encoding/hex"
	"fmt"

	aes_ "github.com/searKing/golang/go/crypto/aes"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	plaintext := []byte("exampleplaintext")

	ciphertext, err := aes_.OFBEncrypt(key, plaintext, nil)
	if err != nil {
		panic(err)
	}

	// It's important to remember that ciphertexts must be authenticated
	// (i.e. by using crypto/hmac) as well as being encrypted in order to
	// be secure.

	fmt.Println("encrypted:")
	fmt.Printf("%x\n", ciphertext)

	plaintext, err = aes_.OFBDecrypt(ciphertext, key)
	if err != nil {
		panic(err)
	}
	fmt.Println("decrypted:")
	fmt.Printf("%s\n", string(plaintext))
}
Output:

encrypted:
00000000000000000000000000000000d91399c43f6adaef3d909876e79904a9729f985422616b9f45b757ac9e7a879e
decrypted:
exampleplaintext

func OFBEncryptRandom

func OFBEncryptRandom(key, plaintext []byte) ([]byte, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL