How is information protected using encryption and decryption methods?
sobes.tech AI
Answer from AI
Information protection using encryption and decryption methods involves transforming data into a format that is inaccessible without a special key or algorithm.
Main stages:
-
Encryption — the original data (plaintext) is transformed into encrypted text (ciphertext) using an algorithm and a key. This prevents unauthorized access.
-
Decryption — the reverse process, where the encrypted text is transformed back into the original data using a key.
In Go, packages from the standard library are often used for this, such as crypto/aes for symmetric encryption or crypto/rsa for asymmetric encryption.
Example of simple symmetric encryption with AES:
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
)
func encrypt(plaintext []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext, nil
}
Thus, encryption ensures data confidentiality, and decryption allows authorized users to restore the data.