You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
945 B
64 lines
945 B
package main
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
func main() {
|
|
|
|
// cipher key
|
|
key := "thisis32bitlongpassphraseimusing"
|
|
|
|
// plaintext
|
|
pt := "This is a secret"
|
|
|
|
c := EncryptAES([]byte(key), pt)
|
|
|
|
// plaintext
|
|
fmt.Println((pt))
|
|
|
|
fmt.Println([]byte(pt))
|
|
|
|
// ciphertext
|
|
|
|
fmt.Println((c))
|
|
fmt.Println([]byte(c))
|
|
|
|
key2 := "thisis32bitlongpassphraseimusing"
|
|
|
|
// decrypt
|
|
DecryptAES([]byte(key2), c)
|
|
}
|
|
|
|
func EncryptAES(key []byte, plaintext string) string {
|
|
|
|
c, err := aes.NewCipher(key)
|
|
CheckError(err)
|
|
|
|
out := make([]byte, len(plaintext))
|
|
|
|
c.Encrypt(out, []byte(plaintext))
|
|
|
|
return hex.EncodeToString(out)
|
|
}
|
|
|
|
func DecryptAES(key []byte, ct string) {
|
|
ciphertext, _ := hex.DecodeString(ct)
|
|
|
|
c, err := aes.NewCipher(key)
|
|
CheckError(err)
|
|
|
|
pt := make([]byte, len(ciphertext))
|
|
c.Decrypt(pt, ciphertext)
|
|
|
|
s := string(pt[:])
|
|
fmt.Println("DECRYPTED:", s)
|
|
}
|
|
|
|
func CheckError(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|