Last active
July 13, 2023 03:33
-
-
Save xsephiroth/ed645eb6f8289c983ee70ec3da2fcbb5 to your computer and use it in GitHub Desktop.
AES decrypt encrypt with CryptoJs & Golang
This file contains 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
<template> | |
<div> | |
</div> | |
</template> | |
<script> | |
import CryptoJs from "crypto-js" | |
export default { | |
name: "", | |
data(){ | |
return {} | |
}, | |
mounted(){ | |
let str = "exampleplaintext"; | |
let key = "example key 1234"; | |
let r = this.encrypt(str, key); | |
console.log(r); //dpepq82KQTeL+9qqcNJ5DMCFXlQH3Zc2Kh49+Ro1gHY= | |
console.log(this.decrypt(r, key)) //exampleplaintext | |
}, | |
methods: { | |
encrypt(str, key){ | |
key = CryptoJs.enc.Utf8.parse(key); | |
let iv = CryptoJs.enc.Utf8.parse("1234567812345678"); | |
let encrypted = CryptoJs.AES.encrypt(str, key, { | |
iv: iv, | |
padding: CryptoJs.pad.Pkcs7 | |
}); | |
return encrypted.toString(); | |
}, | |
decrypt(str, key) { | |
key = CryptoJs.enc.Utf8.parse(key); | |
let iv = CryptoJs.enc.Utf8.parse("1234567812345678"); | |
let encrypted = CryptoJs.AES.decrypt(str.toString(), key, { | |
iv: iv, | |
padding: CryptoJs.pad.Pkcs7 | |
}); | |
return encrypted.toString(CryptoJs.enc.Utf8); | |
} | |
} | |
} | |
</script> |
This file contains 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 ( | |
"bytes" | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/base64" | |
"fmt" | |
) | |
func main() { | |
key := []byte("example key 1234") | |
result, err := AesEncrypt([]byte("Hello World!"), key) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(base64.StdEncoding.EncodeToString(result)) //VRMzYJwT5xx0bvqud3Np+g== | |
r, _ := base64.StdEncoding.DecodeString("dpepq82KQTeL+9qqcNJ5DMCFXlQH3Zc2Kh49+Ro1gHY=") // use CryptoJs encrypted | |
//r := result // decrypt go encrypted | |
origData, err := AesDecrypt(r, key) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(string(origData)) // exampleplaintext | |
} | |
func AesEncrypt(origData, key []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
blockSize := block.BlockSize() | |
origData = PKCS5Padding(origData, blockSize) | |
iv := []byte("1234567812345678") | |
blockMode := cipher.NewCBCEncrypter(block, iv) | |
crypted := make([]byte, len(origData)) | |
blockMode.CryptBlocks(crypted, origData) | |
return crypted, nil | |
} | |
func AesDecrypt(crypted, key []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
iv := []byte("1234567812345678") | |
blockMode := cipher.NewCBCDecrypter(block, iv) | |
origData := make([]byte, len(crypted)) | |
blockMode.CryptBlocks(origData, crypted) | |
origData = PKCS5UnPadding(origData) | |
return origData, nil | |
} | |
func PKCS5Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
func PKCS5UnPadding(origData []byte) []byte { | |
length := len(origData) | |
unpadding := int(origData[length-1]) | |
return origData[:(length - unpadding)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment