Search code examples
c#sqliteencryptioncaesar-cipher

C# code to decrypt data in SQLite field


A partner was fired, and did not leave source code. We have an sqlite database where a field is encrypted. We have solved the encryption, and it is only a substitution of letters in a random way and case sensitive (Almost like Caesar cipher):

This is our cipher code:

Plain:   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9
Cipher:  I O C N E P Z B J T D U S H X Q K W A R V L F M G Y i p n s w a j e o b q f c t r x d y k z h m u g l v 4 3 9 6 1 7 8 5 0 2

And this is an example:

Plaintext:  My text
Ciphertext: Sl zwgz

Can you help me with some C# code to decrypt it?

Thanks inadvance.


Solution

  • Use a Dictionary and use the first char as key and the second as value.

    Dictionary<char, char> dict = new Dictionary<char, char>();
    dict.Add('A', 'I');
    dict.Add('B', 'O');
    ...
    
    char x = 'A';
    return dict[x];
    

    Or even better, use the position of chars in strings:

    string plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    string cipher = "IOCNEPZBJTDUSHXQKWARVLFMGYipnswajeobqfctrxdykzhmuglv4396178502";
    
    char x = 'A';
    return cipher[plain.IndexOf(x)];
    

    With the second posibility it simple to turn around encryption and decryption easily by swaping plain and cipher