← back to articles

❯ cat verman-encryption-decryption.md

Verman Encryption-Decryption

1 min readtr#C##Vernam

One-time pad belirli usullerle karıştırılmış harflerden oluşturulan tek kullanımlık şifreleme yöntemidir. Bir diğer adı ise Vernam şifreleme yöntemidir.

public static class VernamEncryption
{
    public static string Encrypt(string Input, string Key)
    {
        return Process(Input, Key);
    }
    public static string Decrypt(string Input, string Key)
    {
        return Process(Input, Key);
    }
    private static string Process(string Input, string Key)
    {
        if (string.IsNullOrEmpty(Input))                 throw new ArgumentNullException("Input");
        if (string.IsNullOrEmpty(Key))                 throw new ArgumentNullException("Key");
        if (Input.Length != Key.Length)
        {
            throw new ArgumentException("Key is not the same length as the input string");
        }
        ASCIIEncoding Encoding = new ASCIIEncoding();
        byte[] InputArray = Encoding.GetBytes(Input);
        byte[] KeyArray = Encoding.GetBytes(Key);
        byte[] OutputArray = new byte[InputArray.Length];
        for (int x = 0; x < InputArray.Length; ++x)
        {
            OutputArray[x] = (byte)(InputArray[x] ^ Key[x]);
        }
        return Encoding.GetString(OutputArray);
    }
}