Last active
September 6, 2022 06:06
-
-
Save diki-haryadi/3b0514aaed09e21bf91bad744c5b0b9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/aes" | |
| "crypto/cipher" | |
| "crypto/rand" | |
| "encoding/hex" | |
| "errors" | |
| "fmt" | |
| "io" | |
| ) | |
| func Encrypt(unencrypted string) (string, error) { | |
| key := []byte("6368616e676520746869732070617373") | |
| plainText := []byte(unencrypted) | |
| plainText, err := Pad(plainText, aes.BlockSize) | |
| if err != nil { | |
| return "", fmt.Errorf(`plainText: "%s" has error`, plainText) | |
| } | |
| if len(plainText)%aes.BlockSize != 0 { | |
| err := fmt.Errorf(`plainText: "%s" has the wrong block size`, plainText) | |
| return "", err | |
| } | |
| block, err := aes.NewCipher(key) | |
| if err != nil { | |
| return "", err | |
| } | |
| cipherText := make([]byte, aes.BlockSize+len(plainText)) | |
| iv := cipherText[:aes.BlockSize] | |
| if _, err := io.ReadFull(rand.Reader, iv); err != nil { | |
| return "", err | |
| } | |
| mode := cipher.NewCBCEncrypter(block, iv) | |
| mode.CryptBlocks(cipherText[aes.BlockSize:], plainText) | |
| return fmt.Sprintf("%x", cipherText), nil | |
| } | |
| // Decrypt decrypts cipher text string into plain text string | |
| func Decrypt(encrypted string) (string, error) { | |
| key := []byte("6368616e676520746869732070617373") | |
| cipherText, _ := hex.DecodeString(encrypted) | |
| block, err := aes.NewCipher(key) | |
| if err != nil { | |
| panic(err) | |
| } | |
| if len(cipherText) < aes.BlockSize { | |
| panic("cipherText too short") | |
| } | |
| iv := cipherText[:aes.BlockSize] | |
| cipherText = cipherText[aes.BlockSize:] | |
| if len(cipherText)%aes.BlockSize != 0 { | |
| panic("cipherText is not a multiple of the block size") | |
| } | |
| mode := cipher.NewCBCDecrypter(block, iv) | |
| mode.CryptBlocks(cipherText, cipherText) | |
| cipherText, _ = Unpad(cipherText, aes.BlockSize) | |
| fmt.Sprintf("cipherText: %s", cipherText) | |
| return fmt.Sprintf("%s", cipherText), nil | |
| } | |
| func Pad(buf []byte, size int) ([]byte, error) { | |
| bufLen := len(buf) | |
| padLen := size - bufLen%size | |
| padded := make([]byte, bufLen+padLen) | |
| copy(padded, buf) | |
| for i := 0; i < padLen; i++ { | |
| padded[bufLen+i] = byte(padLen) | |
| } | |
| return padded, nil | |
| } | |
| func Unpad(padded []byte, size int) ([]byte, error) { | |
| if len(padded)%size != 0 { | |
| return nil, errors.New("pkcs7: Padded value wasn't in correct size.") | |
| } | |
| bufLen := len(padded) - int(padded[len(padded)-1]) | |
| buf := make([]byte, bufLen) | |
| copy(buf, padded[:bufLen]) | |
| return buf, nil | |
| } | |
| func main() { | |
| data, _ := Encrypt("Hello World") | |
| Decrypt, _ := Decrypt(data) | |
| fmt.Println("data encrypted=> ", data) | |
| fmt.Println("data decrypted=> ", Decrypt) | |
| } | |
| ## References | |
| - https://gist.github.com/brettscott/2ac58ab7cb1c66e2b4a32d6c1c3908a7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment