Created
February 19, 2023 05:31
-
-
Save SKumarSpace/cb5046728d95cd6ce5d65dfd1d554a58 to your computer and use it in GitHub Desktop.
Coldfusion Encrypt Function in Golang
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" | |
| "encoding/base64" | |
| "fmt" | |
| "github.com/andreburgaud/crypt2go/ecb" | |
| "github.com/andreburgaud/crypt2go/padding" | |
| ) | |
| func main() { | |
| keyString := "JZidBZLaYf27huVuM4MNTA==" | |
| key, err := base64.StdEncoding.DecodeString(keyString) | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| inputData := []byte("HELLO") | |
| //Coldfusion: encryptedInput = encrypt( input, encryptionKey, "AES", "base64" ); | |
| encryptedData := encrypt(inputData, key) | |
| encryptedString := base64.StdEncoding.EncodeToString(encryptedData) | |
| fmt.Println("encryptedString: ", encryptedString) | |
| expected := "7CpQsj5z8wkjn4QFR+A1qw==" | |
| fmt.Println("expected: ", expected) | |
| fmt.Println(expected == encryptedString) | |
| } | |
| func encrypt(pt, key []byte) []byte { | |
| block, err := aes.NewCipher(key) | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| mode := ecb.NewECBEncrypter(block) | |
| padder := padding.NewPkcs7Padding(mode.BlockSize()) | |
| pt, err = padder.Pad(pt) // pad last block of plaintext if block size less than block cipher size | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| ct := make([]byte, len(pt)) | |
| mode.CryptBlocks(ct, pt) | |
| return ct | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment