Last active
July 16, 2024 12:05
-
-
Save paintmeyellow/913fd91150f6259cd9cee70712bb1e15 to your computer and use it in GitHub Desktop.
AES-128-GSM Cipher Golang+PHP
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
<?php | |
function handle(): void | |
{ | |
$method = 'AES-128-GCM'; | |
$msg = 'message'; | |
$pass = 'password'; | |
$tagLength = 16; | |
$key = hash('md5', $pass, true); | |
$encHex = $this->encrypt($method, $msg, $key, $tagLength); | |
$dec = $this->decrypt($method, $encHex, $key, $tagLength); | |
$this->info($msg === $dec ? 'Success' : 'Failed'); | |
} | |
function encrypt(string $method, string $message, string $key, int $tagLength): string | |
{ | |
$nonce = random_bytes(openssl_cipher_iv_length($method)); | |
$ciphertext = openssl_encrypt($message, $method, $key, OPENSSL_RAW_DATA, $nonce, $tag, "", $tagLength); | |
return bin2hex($nonce.$ciphertext.$tag); | |
} | |
function decrypt(string $method, string $encryptedHex, string $key, int $tagLength): string | |
{ | |
$encryptedHex = hex2bin($encryptedHex); | |
$nonceLength = openssl_cipher_iv_length($method); | |
[$nonce, $content, $tag] = [ | |
substr($encryptedHex, 0, $nonceLength), | |
substr($encryptedHex, $nonceLength, -$tagLength), | |
substr($encryptedHex, -$tagLength), | |
]; | |
return openssl_decrypt($content, $method, $key, OPENSSL_RAW_DATA, $nonce, $tag); | |
} |
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
import ( | |
"crypto/aes" | |
"crypto/cipher" | |
"crypto/md5" | |
"crypto/rand" | |
"fmt" | |
"io" | |
) | |
func main() { | |
key := createHash("password") | |
data := []byte("message") | |
ciphertext := encrypt(data, key) | |
fmt.Printf("Encrypted: %x\n\n", ciphertext) | |
plaintext := decrypt(ciphertext, key) | |
fmt.Printf("Decrypted: %s\n", plaintext) | |
} | |
func createHash(key string) []byte { | |
h := md5.New() | |
h.Write([]byte(key)) | |
return h.Sum(nil) | |
} | |
func encrypt(data []byte, key []byte) []byte { | |
block, _ := aes.NewCipher(key) | |
gcm, err := cipher.NewGCM(block) | |
if err != nil { | |
panic(err.Error()) | |
} | |
nonce := make([]byte, gcm.NonceSize()) | |
if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | |
panic(err.Error()) | |
} | |
ciphertext := gcm.Seal(nonce, nonce, data, nil) | |
return ciphertext | |
} | |
func decrypt(ciphertext []byte, key []byte) []byte { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
panic(err.Error()) | |
} | |
gcm, err := cipher.NewGCM(block) | |
if err != nil { | |
panic(err.Error()) | |
} | |
nonceSize := gcm.NonceSize() | |
nonce, content := ciphertext[:nonceSize], ciphertext[nonceSize:] | |
plaintext, err := gcm.Open(nil, nonce, content, nil) | |
if err != nil { | |
panic(err.Error()) | |
} | |
return plaintext | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment