Created
July 29, 2023 16:55
-
-
Save adityajoshi12/5596efa33187b5ba8f9171b413531e62 to your computer and use it in GitHub Desktop.
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" | |
"crypto/rand" | |
"encoding/hex" | |
"fmt" | |
"io" | |
) | |
// Function to pad the plaintext to a multiple of the block size | |
func padPlaintext(plaintext []byte, blockSize int) []byte { | |
padding := blockSize - len(plaintext)%blockSize | |
padText := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(plaintext, padText...) | |
} | |
// Function to remove padding from the decrypted plaintext | |
func unpadPlaintext(plaintext []byte) []byte { | |
padding := int(plaintext[len(plaintext)-1]) | |
return plaintext[:len(plaintext)-padding] | |
} | |
// Function to perform AES encryption | |
func aesEncrypt(plaintext []byte, key []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
// Pad the plaintext to a multiple of the block size | |
plaintext = padPlaintext(plaintext, block.BlockSize()) | |
// Generate a random IV (Initialization Vector) | |
iv := make([]byte, block.BlockSize()) | |
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | |
return nil, err | |
} | |
// Use the CBC mode of operation | |
mode := cipher.NewCBCEncrypter(block, iv) | |
ciphertext := make([]byte, len(plaintext)) | |
mode.CryptBlocks(ciphertext, plaintext) | |
// Prepend the IV to the ciphertext | |
ciphertext = append(iv, ciphertext...) | |
return ciphertext, nil | |
} | |
// Function to perform AES decryption | |
func aesDecrypt(ciphertext []byte, key []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
// Extract the IV from the ciphertext | |
iv := ciphertext[:block.BlockSize()] | |
ciphertext = ciphertext[block.BlockSize():] | |
// Use the CBC mode of operation | |
mode := cipher.NewCBCDecrypter(block, iv) | |
plaintext := make([]byte, len(ciphertext)) | |
mode.CryptBlocks(plaintext, ciphertext) | |
// Remove padding from the decrypted plaintext | |
plaintext = unpadPlaintext(plaintext) | |
return plaintext, nil | |
} | |
func main() { | |
plaintext := "Hello, World!" | |
key := []byte("AES256Key-32Characters1234567890") // 32-byte key for AES-256 | |
// Encryption | |
encryptedText, err := aesEncrypt([]byte(plaintext), key) | |
if err != nil { | |
fmt.Println("Encryption error:", err) | |
return | |
} | |
fmt.Println("Plaintext:", plaintext) | |
fmt.Println("Encrypted:", hex.EncodeToString(encryptedText)) | |
// Decryption | |
decryptedText, err := aesDecrypt(encryptedText, key) | |
if err != nil { | |
fmt.Println("Decryption error:", err) | |
return | |
} | |
fmt.Println("Decrypted:", string(decryptedText)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment