Go secures data with the standard crypto packages: use AES-256-GCM from crypto/aes and crypto/cipher for authenticated encryption, crypto/rand for keys and nonces, crypto/tls for encrypted network traffic, and a password hash such as bcrypt or Argon2id for stored passwords. Do not write your own algorithms, and do not use unauthenticated modes such as CFB or CBC for new code.
Encryption protects confidentiality, but you also need integrity (nobody altered the data), authenticity (you know who sent it) and sound key handling. This article shows each piece with code that runs on current Go releases.
Symmetric encryption uses one secret key for both encryption and decryption. In Go the right default is AES in Galois/Counter Mode (GCM), which encrypts and authenticates in one step. If someone changes a single byte of the ciphertext, decryption fails instead of returning corrupted data.
Three rules matter here. The key must be exactly 16, 24 or 32 bytes. The nonce must be unique for every message under the same key, so generate it with crypto/rand and store it next to the ciphertext. The last argument of Seal and Open is additional authenticated data, which you can use to bind the ciphertext to context such as a user ID.
Many older tutorials, including earlier versions of this answer, used cipher.NewCFBEncrypter. CFB gives no integrity protection and is deprecated in the standard library, so prefer GCM or ChaCha20-Poly1305.
Never use math/rand for anything secret. crypto/rand reads from the operating system's cryptographically secure source. Since Go 1.24, rand.Text() returns a random base32 string that is convenient for tokens.
Key management is usually the weak point, not the cipher:
crypto/hkdf in the standard library since Go 1.24, earlier in golang.org/x/crypto/hkdf).A hash such as SHA-256 fingerprints data but is not encryption and cannot be reversed. To prove that a message came from someone holding a shared secret, use an HMAC and compare in constant time.
Passwords need a slow, salted hash, never plain SHA-256 and never MD5 or SHA-1. Use golang.org/x/crypto/bcrypt or Argon2id from golang.org/x/crypto/argon2:
Asymmetric cryptography uses a key pair. For signatures, Ed25519 in crypto/ed25519 is fast, has small keys and is hard to misuse.
For encryption with RSA, use OAEP (rsa.EncryptOAEP) rather than the PKCS#1 v1.5 functions, and use at least 2048-bit keys. In practice, RSA or elliptic-curve keys usually protect a random AES key, and AES protects the bulk data.
For network traffic, use TLS instead of building your own protocol. Go's crypto/tls and net/http negotiate modern cipher suites by default. Set a minimum version explicitly and never turn off certificate verification.
A client created with http.Client{} verifies server certificates against the system roots. Setting InsecureSkipVerify: true removes that protection and exposes you to man-in-the-middle attacks, so keep it out of production. For service-to-service traffic, mutual TLS adds a client certificate through ClientAuth: tls.RequireAndVerifyClientCert and a ClientCAs pool.
crypto/rand, never math/rand, for keys, nonces and tokens.golang.org/x/crypto current, and run govulncheck in CI to catch known vulnerabilities.Use AES-256-GCM through cipher.NewGCM, or ChaCha20-Poly1305 from golang.org/x/crypto/chacha20poly1305. Both provide encryption and integrity in one operation.
No. Reusing a nonce with the same key can reveal plaintext relationships and break authentication. Generate a fresh random nonce for every message.
Hash them with bcrypt or Argon2id and a per-password salt, which both libraries create for you. Never encrypt passwords for later retrieval and never use a fast hash.
Yes. http.Client and tls.Dial verify the server certificate chain and host name unless you set InsecureSkipVerify.
Go's standard library covers most security needs: AES-GCM for confidential and tamper-proof data, crypto/rand for secrets, HMAC and Ed25519 for authenticity, bcrypt or Argon2id for passwords, and crypto/tls for transport. Use the high-level primitives, protect your keys, and avoid legacy modes such as CFB and weak hashes.