Introduction

Cryptography is the backbone of modern software security. From secure authentication and data protection to blockchain, digital signatures, and encrypted communication, cryptography plays a critical role in today’s applications.

For many .NET developers, cryptography can feel complex or intimidating due to its mathematical foundations. However, modern .NET provides robust, high-level cryptographic APIs that allow developers to implement secure solutions without deep mathematical knowledge.

This article demystifies cryptography for intermediate to advanced .NET developers, focusing on practical usage of cryptography in C#, real-world scenarios, and secure implementation patterns.

Why Cryptography Matters in Modern Applications

In today’s digital ecosystem, applications must ensure:

Without cryptography, applications are vulnerable to:

Cryptography is not optional—it is foundational.

Core Cryptography Concepts Every .NET Developer Must Know

Before diving into code, it’s important to understand key cryptographic concepts:

1. Hashing

2. Symmetric Encryption

3. Asymmetric Encryption

4. Digital Signatures

Hashing in C#: Secure Password Storage

Example: Password Hashing Using SHA-256

using System.Security.Cryptography;
using System.Text;

public static string ComputeHash(string input)
{
    using var sha256 = SHA256.Create();
    var bytes = Encoding.UTF8.GetBytes(input);
    var hash = sha256.ComputeHash(bytes);
    return Convert.ToBase64String(hash);
}

Best Practices

Example: PBKDF2 in C#

using System.Security.Cryptography;

public static byte[] HashPassword(string password, byte[] salt)
{
    return new Rfc2898DeriveBytes(
        password,
        salt,
        iterationCount: 100_000,
        HashAlgorithmName.SHA256).GetBytes(32);
}

Symmetric Encryption with AES in C#

AES is the most widely used symmetric encryption standard.

Example: AES Encryption

using System.Security.Cryptography;

public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
    using var aes = Aes.Create();
    aes.Key = key;
    aes.IV = iv;

    using var encryptor = aes.CreateEncryptor();
    return encryptor.TransformFinalBlock(data, 0, data.Length);
}

Example: AES Decryption

public static byte[] Decrypt(byte[] cipherText, byte[] key, byte[] iv)
{
    using var aes = Aes.Create();
    aes.Key = key;
    aes.IV = iv;

    using var decryptor = aes.CreateDecryptor();
    return decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
}

Use Cases

Asymmetric Cryptography with RSA in C#

RSA enables secure key exchange and digital identity verification.

Example: RSA Key Generation

using var rsa = RSA.Create(2048);
var publicKey = rsa.ExportRSAPublicKey();
var privateKey = rsa.ExportRSAPrivateKey();

Encrypting Data Using Public Key

var encrypted = rsa.Encrypt(
    Encoding.UTF8.GetBytes("Sensitive Data"),
    RSAEncryptionPadding.OaepSHA256);

Decrypting Using Private Key

var decrypted = rsa.Decrypt(encrypted, RSAEncryptionPadding.OaepSHA256);

Digital Signatures in C#

Digital signatures ensure authenticity and integrity.

Example: Signing Data

byte[] data = Encoding.UTF8.GetBytes("Message");

byte[] signature = rsa.SignData(
    data,
    HashAlgorithmName.SHA256,
    RSASignaturePadding.Pkcs1);

Verifying Signature

bool isValid = rsa.VerifyData(
    data,
    signature,
    HashAlgorithmName.SHA256,
    RSASignaturePadding.Pkcs1);

Used in:

Cryptography in ASP.NET Core APIs

Example: Secure Token Generation

public static string GenerateSecureToken()
{
    var bytes = RandomNumberGenerator.GetBytes(32);
    return Convert.ToBase64String(bytes);
}

Used for:

Cryptography and Blockchain: Where C# Fits In

Although many blockchain platforms use other languages, C# is widely used for:

Hashing, digital signatures, and asymmetric cryptography are core to blockchain systems.

Common Cryptography Mistakes Developers Should Avoid

❌ Writing custom encryption algorithms
❌ Using outdated algorithms (MD5, SHA1)
❌ Hardcoding encryption keys
❌ Reusing IVs
❌ Skipping key rotation

Rule: Always use proven cryptographic libraries.

Performance and Security Considerations

The Future of Cryptography in .NET

.NET continues to evolve as a secure-first platform.

Conclusion

Cryptography does not need to be intimidating. With modern .NET APIs, C# developers can implement strong, secure cryptographic solutions using clean and maintainable code.

From hashing passwords and encrypting data to signing messages and building blockchain-enabled systems, C# provides everything required for modern cryptography.

Understanding and correctly applying cryptography is a core responsibility of every modern software engineer.