Key Dependent Encoding
Encryption, or Encoding, simply is the art of leaving data in
obfuscated form in order to keep it secure.
It could be as simple as replacing all characters in a text file with those
from a predetermined set in a manner that has the original text having
a direct correlation with the encrypted / encoded version.
An example of this is seen in the Base-64 Encoding System.
Encryption could also mean adding junk data to an original data
in such a way that the original data is completely defaced but can
still be comfortably extracted from the encrypted version.
Encryption processes must always be consistent: i.e. the same type of
encryption, or encoding, carried out on the same data or file must always
produce exactly the same obfuscated output.
But with key dependent encryption, every unique key produces a completely
different encoded set or obfuscated data.
This ensures better security of data since brute-forcing becomes very difficult
without knowledge of the secret key used.
Also, the longer the key used, the more useless brute-forcing becomes.
This means that different individuals or firms can employ the same encryption process,
but have their own unique secret key (Private Key) to encrypt data with.
Such encrypted data cannot be comfortably decrypted by a second firm using the
same encryption process if the first firm can keep its key secret enough.
Recurrent Sequences or Series
Remember Sequences and Series from Ordinary Level Mathematics;
Recurrent Series to be precise? They become as useful as they can be here!
Recurrent Series has the unique characteristic that all succeeding terms in
a progression are totally dependent on all preceding terms - i.e. for any
Recurrent Series, any n+1th term cannot be determined unless the
value of the nth term and its predecessors are known;
and even more true is the fact that every other term in the progression
is absolutely dependent on the 1st term of the series.
The afore property is what we will be exploiting in the VB.Net algorithm for encrypting
data with reference to single secret keys.
Create a new class file;
Call it SoleKeyEncryption
.
Type out the adjoining VB.Net algorithm for encrypting a chunk of data
with a secret key.
Important: BigInteger is inbuilt in VB.Net.
You only need to import the System.Numerics
library.
You might have to add the above library in the reference
section - Project >> Add Reference...; tick off System.Numerics
- to be able to use it.
VB.Net code for SoleKeyEncryption Class
Imports System.Numerics
Imports System.Globalization
Public Class SoleKeyEncryption
Public Function encodeWord(msg As Char(), key As Char()) As String()
Dim encryption(msg.Length - 1) As String
Dim n As Integer
Dim t1 As Integer
Dim Tn As BigInteger
For i As Integer = 0 To msg.Length - 1
t1 = AscW(msg(i))
n = Convert.ToInt32(key(i Mod (key.Length - 1)).ToString(), 16)
Tn = BigInteger.Divide(BigInteger.Subtract(BigInteger.Multiply(BigInteger.Pow(3, n - 1), 2 * t1 + 1), 1), 2)
encryption(i) = Tn.ToString("X")
Next i
Return encryption
End Function
Public Function decodeWord(code As String(), key As Char()) As String
Dim decryption As String = ""
Dim n As Integer
Dim t1 As BigInteger
Dim Tn As BigInteger
For i As Integer = 0 To code.Length - 1
Tn = BigInteger.Parse(code(i), NumberStyles.HexNumber)
n = Convert.ToInt32(key(i Mod (key.Length - 1)).ToString(), 16)
t1 = BigInteger.Divide(BigInteger.Subtract(BigInteger.Divide(2 * Tn + 1, BigInteger.Pow(3, n - 1)), 1), 2)
decryption &= ChrW(CInt(t1))
Next i
Return decryption
End Function
End Class
Main Class
Module Miscellaneous_SoleKeyEncryption
Sub Main()
Dim message As Char() = "merry xmas".ToCharArray()
Dim key As Char() = "A5FB17C4D8".ToCharArray() ' you might want To avoid zeroes
Dim go_secure As SoleKeyEncryption = New SoleKeyEncryption()
Dim encrypted As String() = go_secure.encodeWord(message, key)
Console.WriteLine("Message is '" & String.Join("", message) & "';" &
Environment.NewLine & "Encrypted version is " & String.Join(", ", encrypted))
Dim decrypted As String = go_secure.decodeWord(encrypted, key)
Console.WriteLine(Environment.NewLine & "Decrypted version is '" & decrypted & "'.")
End Sub
End Module