Guide to Custom Building Cryptography Algorithms

Cryptography can be defined as an art of encoding and decoding the patterns (in the form of messages).

Cryptography is a very straightforward concept which deals with manipulating the strings (or text) to make them unreadable for the intermediate person. It has a very effective way to encrypt or decrypts the text coming from the other parties. Some of the examples are, Caesar Cipher, Viginere Cipher, Columner Cipher, DES, AES and the list continues. To develop custom cryptography algorithm, hybrid encryption algorithms can be used.

Hybrid Encryption is a concept in cryptography which combines/merge one/two cryptography algorithms to generate more effective encrypted text.

Example:

FibBil Cryptography Algorithm

Problem Statement:

Program to generate an encrypted text, by computing Fibonacci Series, adding the terms of Fibonacci Series with each plaintext letter, until the length of the key.

Algorithm:

For Encryption: Take an input plain text and key from the user, reverse the plain text and concatenate the plain text with the key, Copy the string into an array. After copying, separate the array elements into two parts, EvenArray, and OddArray in which even index of an array will be placed in EvenArray and same for OddArray. Start generating the Fibonacci Series F(i) up-to-the length of the keyj such that c=i+j where c is cipher text with mod 26. Append all the cth elements in a CipherString and, so Encryption Done!. When sum up concept is use, it highlights of implementing Caesar Cipher.

For Decryption: Vice Versa of the Encryption Algorithm

Example for the Algorithm:
Input: hello
Key: abcd
Output: riobkxezg
Reverse the input, olleh, append this with the key i.e. ollehabcd.
EvenString: leac
OddString: olhbd
As key length is 4, 4 times loop will be generated including FibNum 0, which is ignored.
For EvenArray Ciphers:
FibNum: 1
In Even Array for l and FibNum 1 cip is k
In Even Array for e and FibNum 1 cip is d
In Even Array for a and FibNum 1 cip is z
In Even Array for c and FibNum 1 cip is b
FibNum: 2
In Even Array for l and FibNum 2 cip is j
In Even Array for e and FibNum 2 cip is c
In Even Array for a and FibNum 2 cip is y
In Even Array for c and FibNum 2 cip is a
FibNum: 3 (Final Computed letters)
In Even Array for l and FibNum 3 cip is i 
In Even Array for e and FibNum 3 cip is b 
In Even Array for a and FibNum 3 cip is x 
In Even Array for c and FibNum 3 cip is z
For OddArray Ciphers
FibNum: 1
In Odd Array for o and FibNum 1 cip is p
In Odd Array for l and FibNum 1 cip is m
In Odd Array for h and FibNum 1 cip is i
In Odd Array for b and FibNum 1 cip is c
In Odd Array for d and FibNum 1 cip is e
FibNum: 2
In Odd Array for o and FibNum 2 cip is q
In Odd Array for l and FibNum 2 cip is n
In Odd Array for h and FibNum 2 cip is j
In Odd Array for b and FibNum 2 cip is d
In Odd Array for d and FibNum 2 cip is f
FibNum: 3 (Final Computed letters)
In Odd Array for o and FibNum 3 cip is r 
In Odd Array for l and FibNum 3 cip is o 
In Odd Array for h and FibNum 3 cip is k 
In Odd Array for b and FibNum 3 cip is e 
In Odd Array for d and FibNum 3 cip is g

Arrange EvenArrayCiphers and OddArrayCiphers in their index order, so final String Cipher will be, riobkxezg

Program:

#include<bits/stdc++.h>
using namespace std;

string encryptText(string password, string key)
{
int a = 0, b = 1, c = 0,
m = 0, k = 0, j = 0;
string cipher = “”, temp = “”;

// Declare a password string
string pw = password;

// Reverse the String
reverse(pw.begin(), pw.end());
pw = pw + key;

// For future Purpose
temp = pw;
string stringArray = temp;
string evenString = “”, oddString = “”;

// Declare EvenArray for storing
// even index of stringArray
string evenArray;

// Declare OddArray for storing
// odd index of stringArray
string oddArray;

// Storing the positions in their
// respective arrays
for(int i = 0;
i < stringArray.length(); i++)
{
if (i % 2 == 0)
{
oddString = oddString +
stringArray[i];
}
else
{
evenString = evenString +
stringArray[i];
}
}

evenArray = new char[evenString.length()];
oddArray = new char[oddString.length()];

// Generate a Fibonacci Series
// Upto the Key Length
while (m <= key.length())
{

// As it always starts with 1
if (m == 0)
m = 1;

else
{

// Logic For Fibonacci Series
a = b;
b = c;
c = a + b;

for(int i = 0;
i < evenString.length();
i++)
{

// Caesar Cipher Algorithm Start
// for even positions
int p = evenString[i];
int cip = 0;

if (p == ‘0’ || p == ‘1’ ||
p == ‘2’ || p == ‘3’ ||
p == ‘4’ || p == ‘5’ ||
p == ‘6’ || p == ‘7’ ||
p == ‘8’ || p == ‘9’)
{
cip = p – c;

if (cip < ‘0’)
cip = cip + 9;
}
else
{
cip = p – c;
if (cip < ‘a’)
{
cip = cip + 26;
}
}
evenArray[i] = (char)cip;

// Caesar Cipher Algorithm End
}
for(int i = 0;
i < oddString.length();
i++)
{

// Caesar Cipher Algorithm
// Start for odd positions
int p = oddString[i];
int cip = 0;

if (p == ‘0’ || p == ‘1’ ||
p == ‘2’ || p == ‘3’ ||
p == ‘4’ || p == ‘5’ ||
p == ‘6’ || p == ‘7’ ||
p == ‘8’ || p == ‘9’)
{
cip = p + c;
if (cip > ‘9’)
cip = cip – 9;
}
else
{
cip = p + c;
if (cip > ‘z’)
{
cip = cip – 26;
}
}
oddArray[i] = (char)cip;

// Caesar Cipher Algorithm End
}
m++;
}
}

// Storing content of even and
// odd array to the string array
for(int i = 0; i < stringArray.size(); i++)
{
if (i % 2 == 0)
{
stringArray[i] = oddArray[k];
k++;
}
else
{
stringArray[i] = evenArray[j];
j++;
}
}

// Generating a Cipher Text
// by stringArray (Caesar Cipher)
for(char d : stringArray)
{
cipher = cipher + d;
}

// Return the Cipher Text
return cipher;
}

// Driver code
int main()
{
string pass = “hello”;
string key = “abcd”;

cout << encryptText(pass, key);

return 0;
}

// This code is contributed by himanshu77

  • Java

import java.util.*;
import java.lang.*;

class GFG {

public static void main(String[] args)
{
String pass = “hello”;
String key = “abcd”;
System.out.println(encryptText(pass, key));
}
public static String encryptText(String password, String key)
{
int a = 0, b = 1, c = 0, m = 0, k = 0, j = 0;
String cipher = “”, temp = “”;

// Declare a password string
StringBuffer pw = new StringBuffer(password);

// Reverse the String
pw = pw.reverse();
pw = pw.append(key);

// For future Purpose
temp = pw.toString();
char stringArray[] = temp.toCharArray();
String evenString = “”, oddString = “”;

// Declare EvenArray for storing
// even index of stringArray
char evenArray[];

// Declare OddArray for storing
// odd index of stringArray
char oddArray[];

// Storing the positions in their respective arrays
for (int i = 0; i < stringArray.length; i++) {
if (i % 2 == 0) {
oddString = oddString + Character.toString(stringArray[i]);
}
else {
evenString = evenString + Character.toString(stringArray[i]);
}
}
evenArray = new char[evenString.length()];
oddArray = new char[oddString.length()];

// Generate a Fibonacci Series
// Upto the Key Length
while (m <= key.length()) {
// As it always starts with 1
if (m == 0)
m = 1;

else {

// Logic For Fibonacci Series
a = b;
b = c;
c = a + b;
for (int i = 0; i < evenString.length(); i++) {
// Caesar Cipher Algorithm Start for even positions
int p = evenString.charAt(i);
int cip = 0;
if (p == ‘0’ || p == ‘1’ || p == ‘2’ || p == ‘3’ || p == ‘4’
|| p == ‘5’ || p == ‘6’
|| p == ‘7’ || p == ‘8’ || p == ‘9’) {
cip = p – c;
if (cip < ‘0’)
cip = cip + 9;
}
else {
cip = p – c;
if (cip < ‘a’) {
cip = cip + 26;
}
}
evenArray[i] = (char)cip;
/* Caesar Cipher Algorithm End*/
}
for (int i = 0; i < oddString.length(); i++) {
// Caesar Cipher Algorithm Start for odd positions
int p = oddString.charAt(i);
int cip = 0;
if (p == ‘0’ || p == ‘1’ || p == ‘2’ || p == ‘3’ || p == ‘4’
|| p == ‘5’ || p == ‘6’
|| p == ‘7’ || p == ‘8’ || p == ‘9’) {
cip = p + c;
if (cip > ‘9’)
cip = cip – 9;
}
else {
cip = p + c;
if (cip > ‘z’) {
cip = cip – 26;
}
}
oddArray[i] = (char)cip;
// Caesar Cipher Algorithm End
}

m++;
}
}

// Storing content of even and
// odd array to the string array
for (int i = 0; i < stringArray.length; i++) {
if (i % 2 == 0) {
stringArray[i] = oddArray[k];
k++;
}
else {
stringArray[i] = evenArray[j];
j++;
}
}
// Generating a Cipher Text
// by stringArray (Caesar Cipher)
for (char d : stringArray) {
cipher = cipher + d;
}

// Return the Cipher Text
return cipher;
}
}

Output:
riobkxezg

Conclusion:

Hybrid Algorithms for the cryptography are effective and so, it is not very easy to detect the pattern and decode the message. Here, the algorithm is a combination of mathematical function and Caesar Cipher, so as to implement Hybrid Cryptography Algorithm.

This article has been published from the source link without modifications to the text. Only the headline has been changed. 

Source link