Knowledge
  • Read Me
  • Programming
    • ASP.NET
      • .NET Libraries
      • ASP.NET Core
        • Helper
          • Encryption
          • CSV Helper
          • String Helper
        • Logging
          • Simple Serilog
        • Middlewares
          • IP Restrictions
          • Request Throttling
          • Request Logging
        • Console
          • Command Line with arguments
        • JSON
      • ASP.NET Framework
      • Testing
        • Resources
        • xUnit.net
      • Naming Conventions
      • REST API Guidelines
    • Database
      • SQL Style Guide
      • MSSQL
        • Installation
          • Install MSSQL on MacOS M1 (ARM64)
        • Looping
        • Table Valued Functions
        • Session State
        • SQL Cheat Sheet
        • Export Pipe Delimited CSV With cmdshell
      • Redis
        • Redis Installation on Mac OS
        • Redis Installation on Docker
    • Java
      • AWS SDK - SSM
      • mTLS HTTP Connection
      • Read Resource Files
    • Javascript
      • Javascript Libraries
    • Python
    • OpenSSL
      • One Way SSL & Two Way SSL
      • Common OpenSSL Commands
      • Create Self-Signed Certificate
    • Misc
      • Git Commands
      • Windows Commands
      • PowerShell Commands
      • Vulnerabilities Dependency Check
      • Replace Filename Command
      • JSON Web Token (JWT)
      • Rabbit MQ Message-Broker
      • Pandoc Convert Document
  • DevOps
    • What is DevOps
    • CI & CD
    • Azure DevOps
  • Tools
    • Development Tools
Powered by GitBook
On this page
  • Sample Usage
  • Base64
  • MD5 Hash
  • SHA-1
  • Data Encryption Standard (DES)
  • Triple DES (3DES)
  • Advanced Encryption Standard (AES)

Was this helpful?

  1. Programming
  2. ASP.NET
  3. ASP.NET Core
  4. Helper

Encryption

PreviousHelperNextCSV Helper

Last updated 6 years ago

Was this helpful?

A sample encryption helper for common usage. You can also get it from in GitHub.

Sample Usage

Base64

string plainText = "hello world";
string encrypted = Encryption.Base64Encrypt(plainText);
string decrypted = Encryption.Base64Decrypt(encrypted);
Console.WriteLine($"{plainText} | {encrypted} | {decrypted}");

MD5 Hash

The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities.

An MD5 hash is a one-way transaction and as such it is almost impossible to reverse engineer an MD5 hash to retrieve the original string.

string plainText = "hello world";
string encrypted = Encryption.MD5Hash(plainText);
Console.WriteLine($"{plainText} | {encrypted}");

SHA-1

string plainText = "hello world";
string encrypted = Encryption.SHA1Hash(plainText);
Console.WriteLine($"{plainText} | {encrypted}");

Data Encryption Standard (DES)

Note: secret key and vector must be 8 characters length

string plainText = "hello world";
string secretKey = "secret99"; //must be exact 8 chars length
string initVector = "!@#$%^&*"; //must be exact 8 chars length
string encrypted = Encryption.DesEncrypt(plainText, secretKey, initVector);
string decrypted = Encryption.DesDecrypt(encrypted, secretKey, initVector);
Console.WriteLine($"{plainText} | {encrypted} | {decrypted}");

Triple DES (3DES)

Note: secret key must be 24 characters length and vector must be 8 characters length

string plainText = "hello world";
string secretKey = "secret99asdfghjkqwerty12"; //must be exact 24 chars length
string initVector = "!@#$%^&*"; //must be exact 8 chars length
string encrypted = Encryption.TripleDESEncrypt(plainText, secretKey, initVector);
string decrypted = Encryption.TripleDESDecrypt(encrypted, secretKey, initVector);
Console.WriteLine($"{plainText} | {encrypted} | {decrypted}");

Advanced Encryption Standard (AES)

Note: AES key accept for three different key lengths:

  • 128 Bit - 16 ASCII characters, 1 byte each

  • 192 Bit - 24 ASCII characters, 1 byte each

  • 256 Bit - 32 ASCII characters, 1 byte each

string plainText = "hello world";
string key = "!@#$%^&*()1234567890qwer"; //128 Bit
string encrypted = Encryption.AesEncrypt(plainText, key);
string decrypted = Encryption.AesDecrypt(encrypted, key);
Console.WriteLine($"{plainText} | {encrypted} | {decrypted}");

In cryptography, SHA-1 is a cryptographic hash function which takes an input and produces a 160-bit hash value known as a message digest – typically rendered as a hexadecimal number, 40 digits long. It was designed by the United States National Security Agency, and is a U.S. Federal Information Processing Standard.

The Data Encryption Standard is a symmetric-key algorithm for the encryption of electronic data. Although insecure, it was highly influential in the advancement of modern cryptography.

In cryptography, Triple DES, officially the Triple Data Encryption Algorithm, is a symmetric-key block cipher, which applies the DES cipher algorithm three times to each data block.

The Advanced Encryption Standard, also known by its original name Rijndael, is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology in 2001.

Wikipedia
Wikipedia
Wikipedia
Wikipedia
NLibrary
Wikipedia
Base64
MD5 Hash
SHA-1
Data Encryption Standard (DES)
Triple DES (3DES)
Advanced Encryption Standard (AES)