Custom Building Cryptography Algorithms (Hybrid Cryptography)
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, Columnar 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 gArrange EvenArrayCiphers and OddArrayCiphers in their index order, so final String Cipher will be, riobkxezg
Program:
C++14
#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; } } |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Driver Code public static void Main( string [] args) { string pass = "hello" ; string key = "abcd" ; Console.WriteLine(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 char [] pw = password.ToCharArray(); // Reverse the String Array.Reverse(pw); // For future Purpose temp = new string (pw) + key; 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 + Char.ToString(stringArray[i]); } else { evenString = evenString + Char.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[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.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) foreach ( char d in stringArray) { cipher = cipher + d; } // Return the Cipher Text return cipher; } } // This code is contributed by phasing17 |
Python3
# Python3 program to implement the approach def encryptText(password, key): a = 0 b = 1 c = 0 m = 0 k = 0 j = 0 cipher = "" temp = "" # Declare a password string pw = password # Reverse the String pw = pw[:: - 1 ] pw = pw + key # For future Purpose temp = pw stringArray = list (temp) evenString = "" oddString = "" # Declare EvenArray for storing # even index of stringArray evenArray = "" # Declare OddArray for storing # odd index of stringArray oddArray = "" # Storing the positions in their # respective arrays for i in range ( len (stringArray)): if (i % 2 = = 0 ): oddString = oddString + stringArray[i] else : evenString = evenString + stringArray[i] evenArray = ["" for _ in range ( len (evenString))] oddArray = ["" for _ in range ( len (oddString))] # Generate a Fibonacci Series # Upto the Key Length while (m < = len (key)): # As it always starts with 1 if (m = = 0 ): m = 1 else : # Logic For Fibonacci Series a = b b = c c = a + b for i in range ( len (evenString)): # Caesar Cipher Algorithm Start # for even positions p = evenString[i] cip = 0 if p in "1234567890" : cip = ord (p) - c if (cip < ord ( '0' )): cip = cip + 9 else : cip = ord (p) - c if (cip < ord ( 'a' )): cip = cip + 26 evenArray[i] = chr (cip) # Caesar Cipher Algorithm End for i in range ( len (oddString)): # Caesar Cipher Algorithm # Start for odd positions p = oddString[i] cip = 0 if p in "0123456789" : cip = ord (p) + c if (cip > ord ( '9' )): cip = cip - 9 else : cip = ord (p) + c if (cip > ord ( 'z' )): cip = cip - 26 oddArray[i] = chr (cip) # Caesar Cipher Algorithm End m + = 1 # Storing content of even and # odd array to the string array for i in range ( len (stringArray)): if (i % 2 = = 0 ): stringArray[i] = oddArray[k] k + = 1 else : stringArray[i] = evenArray[j] j + = 1 # Generating a Cipher Text # by stringArray (Caesar Cipher) for d in stringArray: cipher = cipher + d # Return the Cipher Text return cipher # Driver code pw = "hello" key = "abcd" print (encryptText(pw, key)) # This code is contributed by phasing17 |
Javascript
// Javascript code implementation function encryptText(password, key) { let a = 0, b = 1, c = 0, m = 0, k = 0, j = 0; let cipher = "" ; let temp = "" ; // Declare a password string let pass = password; let pw = Array.from(pass); // Reverse the String pw.reverse(); for (let i = 0; i < Array.from(key).length; i++){ pw.push(Array.from(key)[i]); } // For future Purpose temp = pw.join( "" ); let stringArray = temp.split( '' ); let evenString = "" ; let oddString = "" ; // Storing the positions in their respective arrays for (let i = 0; i < stringArray.length; i++) { if (i % 2 == 0) { oddString = oddString + stringArray[i]; } else { evenString = evenString + stringArray[i]; } } // Declare EvenArray for storing // even index of stringArray let evenArray = new Array(evenString.length); // Declare OddArray for storing // odd index of stringArray let oddArray = new Array(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 (let i = 0; i < evenString.length; i++) { // Caesar Cipher Algorithm Start for even positions let p = evenString[i]; let 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.charCodeAt(0) - c; if (cip < '0' .charCodeAt(0)) cip = cip + 9; } else { cip = p.charCodeAt(0) - c; if (cip < 'a' .charCodeAt(0)) { cip = cip + 26; } } evenArray[i] = String.fromCharCode(cip); /* Caesar Cipher Algorithm End*/ } for (let i = 0; i < oddString.length; i++) { // Caesar Cipher Algorithm Start for odd positions let p = oddString[i]; let 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' .charCodeAt(0)) cip = cip - 9; } else { cip = p.charCodeAt(0)+ c; if (cip > 'z' .charCodeAt(0)) { cip = cip - 26; } } // console.log(cip); oddArray[i] = String.fromCharCode(cip); // Caesar Cipher Algorithm End } m++; } } // Storing content of even and // odd array to the string array for (let 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 (let i = 0; i < stringArray.length; i++){ cipher = cipher + stringArray[i]; } // Return the Cipher Text return cipher; } let pass = "hello" ; let key = "abcd" ; console.log(encryptText(pass, key)); // The code is contributed by Nidhi goel. |
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.
Contact Us