Security and Cryptography in C#

 

Serialization Methods

Feature

Binary

XML

JSON

Format

Binary

Text (XML)

Text (JSON)

Human Readable

No

Yes

Yes

Performance

Fast

Slower

Faster than XML

Preferred Use

Legacy systems

Interoperability

Modern applications

Security

Less secure

Moderately secure

More secure

 

Note

  • File I/O is handled with classes like File, StreamReader, StreamWriter, and FileStream for efficient reading and writing.
  • Serialization enables storing and transmitting objects. JSON serialization is the preferred modern method due to its simplicity and efficiency.

Security and Cryptography in C#

C# provides robust tools and libraries to implement security and cryptography through namespaces such as System.Security and System.Security.Cryptography. These tools help secure data through encryption, decryption, hashing, digital signatures, and more.

Key Concepts in Security and Cryptography

Category

Purpose

Features

Common Algorithms

Code Snippet/Example

Hashing

Converts input data into a fixed-size hash value for integrity checks.

- One-way operation
- Collision-resistant

SHA256, SHA512, MD5 (not secure)

SHA256 Example:
sha256.ComputeHash(Encoding.UTF8.GetBytes(input));

Symmetric Encryption

Encrypts and decrypts data using the same key.

- Fast and efficient
- Requires secure key sharing

AES, DES, TripleDES

AES Example:
aes.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);

Asymmetric Encryption

Uses a public key for encryption and a private key for decryption.

- Secure
- Slower than symmetric encryption

RSA, ECDSA

RSA Example:
rsa.Encrypt(data, RSAEncryptionPadding.Pkcs1);

Digital Signatures

Verifies the authenticity and integrity of the data.

- Uses private key to sign and public key to verify

RSA, ECDSA

Signature Example:
rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

Secure Communication

Ensures data confidentiality and integrity during transmission.

- TLS/SSL for secure channels
- HTTPS for web communication

TLS, SSL

TLS Example:
Implemented via HTTPS in ASP.NET Core apps or secure socket libraries.


Common Use Cases with Examples

Use Case

Preferred Approach

Example

Password Storage

Hashing with salt

ComputeHash(Encoding.UTF8.GetBytes(password + salt));

Securing Data Transmission

TLS/SSL

Configure HTTPS in web server or use HttpClient with TLS settings.

Storing Sensitive Data

Symmetric Encryption

Encrypt database fields using AES: aes.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);

Verifying Data Integrity

Hashing

Compare hash values: hash1 == hash2

Securing Key Exchange

Asymmetric Encryption

rsa.Encrypt(data, RSAEncryptionPadding.Pkcs1);

Authenticating Messages

Digital Signatures

rsa.SignData(message, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);


Performance and Recommendations

Aspect

Hashing

Symmetric Encryption

Asymmetric Encryption

Digital Signatures

Speed

Very Fast

Fast

Slow

Moderate

Use Case

Integrity, password storage

Large data encryption

Secure key exchange, small data

Authentication, data integrity

Key Management

Not required

Single shared key

Public/Private key pair

Private key for signing

Vulnerabilities

Collision (weak algorithms)

Key compromise

Key length, computational attacks

Weak hashing algorithms

Recommendations

Use SHA256 or better

Use AES

Use RSA with >2048-bit keys

Use strong hashing (e.g., SHA512)

 

Comments

Popular posts from this blog

SQL

C# operations

.NET Web API interview questions