The Belaso cipher is a classic cipher that uses as key a word and encrypts plain text by dividing it in blocks the size of the key, and adding to each position of the alphabet corresponding to the letter in the block , the position in the alphabet of the letter of the key that shares the same index(comparing the plaintext block and key text)
This was an exercise to get back into programming. It turned out a fun mix of windows forms, C# and C lol.
First variant: Using ASCII code. This one respects a 26 english letter alphabet, from A to Z.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string plainTxt, key;
StringBuilder encrText = new StringBuilder();
StringBuilder decrText = new StringBuilder();
public Form1()
{
InitializeComponent();
}
public String decryption()
{
plainTxt = plainText.Text.ToUpper();
key = keyText.Text;
int ksize = key.Length;
int msize = plainTxt.Length;
char[] txt = plainTxt.ToCharArray();
for (int i = 0; i < msize; i++)
{
char decr = txt[i] - getShift(key, i) < 65 ? (char)((txt[i] - getShift(key, i)) + 27) : (char)((txt[i] - getShift(key, i)) - 1);
decrText.Append(decr);
}
return decrText.ToString();
}
public String encryption()
{
plainTxt = plainText.Text.ToUpper();
key = keyText.Text.ToUpper();
int ksize = key.Length;
int msize = plainTxt.Length;
if (ksize > msize) { return "Key size must be smaller than plaintext size"; }
char[] txt = plainTxt.ToCharArray();
for (int i = 0; i < msize; i++)
{
char encr = txt[i] + getShift(key, i) > 90 ? (char)((txt[i]+ getShift(key,i))- 26) : (char)((txt[i] + getShift(key, i))+1);
encrText.Append(encr);
}
return encrText.ToString();
}
private int getShift(string key, int i)
{
char[] k = key.ToCharArray();
int temp = i%key.Length;
return ((int)key[temp]) - 65;
}
private void encrButton_Click(object sender, EventArgs e)
{
String encrypted = encryption();
resultBox.Clear();
resultBox.AppendText(encrypted);
}
private void decrButton_Click(object sender, EventArgs e)
{
String decrypted = decryption();
resultBox.Clear();
resultBox.AppendText(decrypted);
}
}
}
Second variant: Using a predefined 27 char alphabet, including space. This was more fun to write since my C# knowledge is definitely not the best and there were issues that actually raised some 20mins and cig break to think about lol In the end my C knowledge beats my C# lol.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BelasoCipher2
{
public partial class Form1 : Form
{
string alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string plainTxt, cdkey;
public Form1()
{
InitializeComponent();
}
//Return integer values to plaintext
public String convertS(List<int> intCrypt)
{
int size = intCrypt.Count;
StringBuilder rez= new StringBuilder();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < 27; j++)
{
if (intCrypt[i] == j) //if integer value = position add letter to string
rez.Append(alpha[j]);
}
}
return rez.ToString();
}
//Modulo function for Z classes
private int mod(int x, int m)
{
int r = x % m;
return r < 0 ? r + m : r;
}
//Encryption/decryption algorithm based on integer values of alphabet/key/plaintext
private string decriEncryAlg(int n){
plainTxt = ptextBox.Text.ToUpper();
cdkey = keyBox.Text.ToUpper();
int size = plainTxt.Length;
String result;
List<int> kLi = new List<int>();
List<int> pLi = new List<int>();
List<int> resList = new List<int>();
int klen = cdkey.Length;
if (klen > size) { return "Key size must be smaller than plaintext size"; }
kLi = keyCode(cdkey); // get integer position value in the alphabet of key
pLi = divideString(plainTxt, klen); //get integer position value in the alphabet of plaintext
int pLiSize = pLi.Count;
int temp = 0;
int index = 0;
while (temp <= pLiSize) // going on chunks of key size up to integer value of plaintext list size
{
for (int i = 0; i < klen; i++)
{
if (index >= pLiSize) //If index >= size then return the result of encr/decr
{
result = convertS(resList);
return result;
}
int tempAux;
if(n == 0) tempAux = pLi[index]+kLi[i]; //if 0 then encryption
else tempAux = pLi[index]-kLi[i]; //else decryption
resList.Add(mod(tempAux, 27));
index++;
}
temp += klen;
}
result = convertS(resList);
return result;
}
public string decryption()
{
string result = decriEncryAlg(1);
return result;
}
public string encryption()
{
string result = decriEncryAlg(0);
return result;
}
//Get integer values according to alphabet of key word
public List<int> keyCode(string key)
{
List<int> list = new List<int>();
int len = key.Length;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < 27; j++)
{
if (key[i].Equals(alpha[j])) list.Add(j);
}
}
return list;
}
//Divide plaintext in chunks <=the size of key word and get their integer value from the alphabet
public List<int> divideString(string s, int k)
{
List<int> list = new List<int>();
int size = plainTxt.Length;
int temp = 0, ke=k;
StringBuilder aux = new StringBuilder();
do
{
for (int z = temp; z < ke; z++)
{
if (z >= size) break;
aux.Append(plainTxt[z]);
}
for (int i = temp; i < ke; i++)
{
if (i >= size) return list;
for (int j = 0; j < 27; j++)
{
if (aux[i].Equals(alpha[j])) list.Add(j);
}
}
temp = temp + k;
ke += ke;
} while (temp <= size+k);
return list;
}
private void encButton_Click(object sender, EventArgs e)
{
String encrypted = encryption();
textBox3.Clear();
textBox3.AppendText(encrypted);
}
private void decrButton_Click(object sender, EventArgs e)
{
String decrypted = decryption();
textBox3.Clear();
textBox3.AppendText(decrypted);
}
}
}