rskibbe.Encryption.Base64 – A Base64 implementation

rskibbe.Encryption.Base64 - A Base64 encryption implementation sub-package
rskibbe.Encryption.Base64 – A Base64 encryption implementation sub-package

What is rskibbe.Encryption.Base64?

As the name already suggests, it is a sub-package of the „rskibbe.Encryption“ infrastructure package implementing the Base64 encryption (encoding). This means, it helps you encrypt / encode or decrypt / decode data with the Base64 algorithm. I needed some common denominator for my apps and I didn’t want to start over and over again. I didn’t want to have separated code bases and some sort of common base – so I came up with these two libraries.

infoKeep in mind that Base64 rather is an Encoding than an Encryption, but in terms of this lib, I called it Encryption, because you can transfer from A to B and back from B to A.

Quick example

A quick example for rskibbe.Encryption.Base64 based encryption
A quick example for rskibbe.Encryption.Base64 based encryption

Step 1 – Install the package

Open the NuGet Package Manager and execute the following commands (or install it by GUI). This command will install the package to enable you using its code.

Install-Package rskibbe.Encryption
Install-Package rskibbe.Encryption.Base64

Step 2 – Instant usage without DI conceptions, etc.

If you „just want to get it going“ you can – I provided a singleton property for that. Keep in mind though, that you should think about using the more DI / IoC based approach (creating and sharing instances over DI and using like the IEncryptor or inherited interfaces around your app) in bigger apps. This can be especially helpful, if you create Encoders for specific and different problems.

// encoding
// 1. using the normal version (uses UTF8 by default)
string encodedText = Base64Encryptor.Instance.Encode("hello!");

// or 2. changing the encoding globally (for the singleton instance)
Base64Encryptor.Instance.Encoding = System.Text.Encoding.<WhateverEncodingYouLike>;
string encodedText = Base64Encryptor.Instance.Encode("hello!");

// decoding
string decodedText = Base64Encryptor.Instance.Decode("aGVsbG8h");
' encoding
' 1. using the normal version (uses UTF8 by default)
Dim encodedText As String = Base64Encryptor.Instance.Encode("hello!")

' or 2. changing the encoding globally (for the singleton instance)
Base64Encryptor.Instance.Encoding = System.Text.Encoding.<WhateverEncodingYouLike>
Dim encodedText As String = Base64Encryptor.Instance.Encode("hello!")

' decoding
Dim decodedText As String = Base64Encryptor.Instance.Decode("aGVsbG8h")

Step 3 – Create an instance of the specific encryptor

As we are using the Base64 implementation as encryption algorithm, you will need to create a corresponding object instance of the class „Base64Encryptor“. You can also specify an encoding (System.TextEncoding) while constructing. The encryptor will use UTF8 as default / if not specified.

// longer version
Base64Encryptor base64Encryptor = new Base64Encryptor();

// or short..
var base64Encryptor = new Base64Encryptor();
' longer version
Dim base64Encryptor As Base64Encryptor = new Base64Encryptor()

' or short..
Dim base64Encryptor = New Base64Encryptor()

Step 4 – Using the encryptor

With our common denominator / interface coming from the base package „rskibbe.Encryption“, we can now just call the corresponding functions.

Encrypting / Encoding

Encoding raw text to their Base64 counterpart is now pretty easy as you can just call the „Encode“ method on your created „Base64Encryptor“ instance – implementing the „IEncryptor“ interface.

// having our instance from the previous examples
string plainText = "hello!";
string encodedText = base64Encryptor.Encode(plainText);

// will output "aGVsbG8h"
System.Diagnostics.Debug.WriteLine(encodedText);
' having our instance from the previous examples
Dim plainText As String = "hello!"
Dim encodedText As String = base64Encryptor.Encode(plainText)

' will output "aGVsbG8h"
System.Diagnostics.Debug.WriteLine(encodedText)

Decrypting / Decoding

Decoding is just as simple as encoding, just call the corresponding „Decode“ method being implemented from the interface as well.

// having our instance from the previous examples
string encodedText = "aGVsbG8h";
string text = base64Encryptor.Decode(encodedText);

// will output "hello!"
System.Diagnostics.Debug.WriteLine(text);
' having our instance from the previous examples
Dim encodedText As String = "aGVsbG8h"
Dim text As String = base64Encryptor.Decode(encodedText)

' will output "hello!"
System.Diagnostics.Debug.WriteLine(text)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert