Sitemap
Abhishek Pandey iOS

Abhishek Pandey iOS

I am an iOS Engineer passionate about building secure, user‑friendly mobile applications.

Swift Crypto in Action: Symmetric Encryption with Curve25519 Signatures

3 min readApr 8, 2026

--

In a world where data flows endlessly across networks, keeping messages private and authentic is essential. Encryption ensures confidentiality, while digital signatures guarantee authenticity. Together, they form the backbone of secure communication. Let’s explore how to combine symmetric encryption with Curve25519 signing in Swift using CryptoKit.

Press enter or click to view image in full size

🔐 Symmetric Encryption: Locking the Message

Symmetric encryption uses the same key to encrypt and decrypt data. It’s fast, efficient, and widely used in secure messaging systems.

import CryptoKit
import Foundation

func encryptMessage(_ message: String, using key: SymmetricKey) throws -> Data {
let plaintext = Data(message.utf8)
let nonce = AES.GCM.Nonce()
let sealedBox = try AES.GCM.seal(plaintext, using: key, nonce: nonce)
return Data(nonce) + sealedBox.ciphertext + sealedBox.tag
}

func decryptMessage(_ combined: Data, using key: SymmetricKey) throws -> String {
let nonceData = combined.prefix(12) // AES-GCM nonce size
let tag = combined.suffix(16) // AES-GCM tag size
let ciphertext = combined.dropFirst(12).dropLast(16)

let nonce = try AES.GCM.Nonce(data: nonceData)
let sealedBox = try AES.GCM.SealedBox(nonce: nonce, ciphertext: ciphertext, tag: tag)
let decryptedData = try AES.GCM.open(sealedBox, using: key)

return String(data: decryptedData, encoding: .utf8) ?? ""
}

✍️ Curve25519 Signatures: Proving Identity

Encryption hides the content, but signatures prove who sent it. With Curve25519:

  • The sender signs the encrypted data using their private key.
  • The receiver verifies the signature using the sender’s public key.
// Generate a signing key pair
let privateKey = Curve25519.Signing.PrivateKey()
let publicKey = privateKey.publicKey

// Sign data
func signMessage(_ data: Data, using privateKey: Curve25519.Signing.PrivateKey) throws -> Data {
return try privateKey.signature(for: data)
}

// Verify signature
func verifySignature(_ signature: Data, for data: Data, using publicKey: Curve25519.Signing.PublicKey) -> Bool {
return publicKey.isValidSignature(signature, for: data)
}

Get Abhishek Pandey iOS’s stories in your inbox

Join Medium for free to get updates from this writer.

🚀 Putting It All Together

Here’s how encryption and signing work side by side:

let key = SymmetricKey(size: .bits256)
let message = "The treasure is buried under the oak tree."

// Encrypt the message
let encrypted = try encryptMessage(message, using: key)

// Sign the encrypted data
let signature = try signMessage(encrypted, using: privateKey)

// Verify before decrypting
if verifySignature(signature, for: encrypted, using: publicKey) {
let decrypted = try decryptMessage(encrypted, using: key)
print("Verified & decrypted message: \(decrypted)")
} else {
print("Signature verification failed — message may be tampered!")
}
Press enter or click to view image in full size

Output:

Verified & decrypted message: The treasure is buried under the oak tree.

🛡️ Why This Matters

  • Confidentiality: Symmetric encryption keeps the message secret.
  • Authenticity: Curve25519 signatures prove the sender’s identity.
  • Integrity: Verification ensures the message hasn’t been altered.

Together, they provide end‑to‑end security: only the intended recipient can read the message, and they can trust it came from the right sender.

✨ Final Thought

Symmetric encryption is the lock, and digital signatures are the seal. With Swift’s CryptoKit, you can combine both in just a few lines of code — protecting not only the privacy of your messages but also their authenticity. In a world full of digital noise, that combination is priceless.

--

--

Abhishek Pandey iOS
Abhishek Pandey iOS

I am an iOS Engineer passionate about building secure, user‑friendly mobile applications.