Custom Building Cryptography Algorithms (Hybrid Cryptography)
Hybrid cryptography combines two or more encryption techniques. It combines asymmetric and symmetric encryption to take advantage of their respective capabilities. The solution uses public-key cryptography to share keys and symmetric encryption to encrypt messages efficiently.
A hybrid encryption scheme combines the advantages of an asymmetric encryption method with the effectiveness of a symmetric encryption method.
For encryption of a message, initially generate a symmetric key.The person one want to send the message to then share her public key while keeping the secret key private. After that, encrypt the symmetric key with the receiver's public key and transmit it to them.
To decrypt a communication, the receiver decrypts the encrypted symmetric key with the help of her private key to get the decryption key, which she then uses to decode the message.
See the below image to get the visual undersatnding of Hybrid Cryptography:
Here is an explanation of the steps we havementioned in the above image −
- Generate Public & Private Keys − The recipient first creates a pair of keys: a public key and a private key.
- Send Public Key to Sender − The recipient sends his public key to the sender. The public key can be openly distributed as it is used for encryption only.
- Encrypt File with Symmetric Encryption using Public Key Received − The sender wants to send a file securely to the recipient. The sender encrypts the file with the help of symmetric encryption but besides using his own key he uses the recipient's public key. This means that only the recipient who has the corresponding private key can decrypt the file.
- Send Private Key with Asymmetric Cryptogram − The sender now will send the recipient's private key back to him but it is encrypted with the help of asymmetric encryption. This encrypted private key makes sure that only the intended recipient who has the respective private key can decrypt it.
Implementation of Hybrid Cryptography
We will implement Hybrid Cryptography with the help of different programming languages liek Python, Java and C++. So refer the below section for code −
Using Python
The custom encryption in this implementation is based on shifting the ASCII value of each character in the concatenated key. For even-indexed characters, each is shifted by the length of public key, and for odd-indexed characters, each is shifted by the length of symmetric key. This is a simple type of encryption that still uses the method combining keys and applying customized transformations. So see the Python implementation below −
def hybrid_encrypt(message, public_key, symmetric_key):
# Reverse the message
reversed_message = message[::-1]
# Combine reversed message with symmetric key
combined_key = reversed_message + symmetric_key
# change combined key to list of characters
key_characters = list(combined_key)
# Apply custom encryption
encrypted_characters = []
for i, char in enumerate(key_characters):
if i % 2 == 0:
encrypted_char = chr(ord(char) + len(public_key)) # Shift ASCII value by length of public key
else:
encrypted_char = chr(ord(char) - len(symmetric_key)) # Shift ASCII value by length of symmetric key
encrypted_characters.append(encrypted_char)
# Combine encrypted characters
encrypted_message = ''.join(encrypted_characters)
return encrypted_message
# our message, public key and symmetric key
message = "hellotutorialspoint"
public_key = "qwer"
symmetric_key = "private"
# function execution
encrypted_message = hybrid_encrypt(message, public_key, symmetric_key)
print("Our Encrypted Message:", encrypted_message)
Output
Our Encrypted Message: xgmhtlpZmksmymsep^livbzZx^
Using Java
This Java program demonstrates a hybrid encryption technique applied to a plaintext message using a given key. The main method initializes a plaintext message and a key, then calls the encryptMessage function to encrypt the plaintext.
import java.util.*;
class HybridEncryption {
public static void main(String[] args) {
String plaintext = "hellotutorialspoint";
String key = "qwer";
System.out.println("Our Encrypted Message is: " + encryptMessage(plaintext, key));
}
public static String encryptMessage(String message, String key) {
int a = 0, b = 1, c = 0, m = 0, k = 0, j = 0;
String encryptedText = "", temp = "";
// Reverse the message
StringBuilder reversedMessage = new StringBuilder(message).reverse();
// Combine the reversed message with the key
StringBuilder combinedString = reversedMessage.append(key);
// change the combined string to a character array
char[] charArray = combinedString.toString().toCharArray();
String evenChars = "", oddChars = "";
// Separate characters into even and odd positions
for (int i = 0; i < charArray.length; i++) {
if (i % 2 == 0) {
oddChars += charArray[i];
}else {
evenChars += charArray[i];
}
}
char[] evenArray = new char[evenChars.length()];
char[] oddArray = new char[oddChars.length()];
// create a Fibonacci series and apply Caesar cipher
while (m <= key.length()) {
if (m == 0)
m = 1;
else {
a = b;
b = c;
c = a + b;
for (int i = 0; i < evenChars.length(); i++) {
int p = evenChars.charAt(i);
int cipher = 0;
if (Character.isDigit(p)) {
cipher = p - c;
if (cipher < '0')
cipher = cipher + 9;
}else {
cipher = p - c;
if (cipher < 'a') {
cipher = cipher + 26;
}
}
evenArray[i] = (char)cipher;
}
for (int i = 0; i < oddChars.length(); i++) {
int p = oddChars.charAt(i);
int cipher = 0;
if (Character.isDigit(p)) {
cipher = p + c;
if (cipher > '9')
cipher = cipher - 9;
}else {
cipher = p + c;
if (cipher > 'z') {
cipher = cipher - 26;
}
}
oddArray[i] = (char)cipher;
}
m++;
}
}
// Combine even and odd characters based on their positions
for (int i = 0; i < charArray.length; i++) {
if (i % 2 == 0) {
charArray[i] = oddArray[k];
k++;
}else {
charArray[i] = evenArray[j];
j++;
}
}
// Generate the encrypted text
for (char d : charArray) {
encryptedText = encryptedText + d;
}
// Return the encrypted text
return encryptedText;
}
}
Output
Our Encrypted Message is: wkllspoxlorqxqriobknzbu
Using C++
This C++ code shows a custom encryption algorithm that combines reverse string manipulation with a Caesar cipher to encrypt a message.
#include <iostream>
#include <string>
#include <algorithm> // Include the algorithm header for the reverse function
using namespace std;
string hybridEncryption(string password, string key) {
int a = 0, b = 1, c = 0,
m = 0, k = 0, j = 0;
string cipherText = "", temp = "";
// Declare a password string
string reversedPassword = password;
// Reverse the String
reverse(reversedPassword.begin(), reversedPassword.end()); // Use the reverse function
reversedPassword = reversedPassword + key;
// For future Purpose
temp = reversedPassword;
string charArray = temp;
string evenChars = "", oddChars = "";
// Declare EvenArray for storing
// even index of charArray
char *evenArray;
// Declare OddArray for storing
// odd index of charArray
char *oddArray;
// Storing the positions in their
// respective arrays
for (int i = 0; i < charArray.length(); i++) {
if (i % 2 == 0) {
oddChars = oddChars + charArray[i];
}else {
evenChars = evenChars + charArray[i];
}
}
evenArray = new char[evenChars.length()];
oddArray = new char[oddChars.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 < charArray.length(); i++) {
// Caesar Cipher Algorithm Start
// for even positions
int p = charArray[i];
int cipher = 0;
if (p == '0' || p == '1' ||
p == '2' || p == '3' ||
p == '4' || p == '5' ||
p == '6' || p == '7' ||
p == '8' || p == '9') {
cipher = p - c;
if (cipher < '0')
cipher = cipher + 9;
}else {
cipher = p - c;
if (cipher < 'a') {
cipher = cipher + 26;
}
}
evenArray[i] = (char)cipher;
// Caesar Cipher Algorithm End
}
for (int i = 0; i < charArray.length(); i++) {
// Caesar Cipher Algorithm
// Start for odd positions
int p = charArray[i];
int cipher = 0;
if (p == '0' || p == '1' ||
p == '2' || p == '3' ||
p == '4' || p == '5' ||
p == '6' || p == '7' ||
p == '8' || p == '9') {
cipher = p + c;
if (cipher > '9')
cipher = cipher - 9;
}else {
cipher = p + c;
if (cipher > 'z') {
cipher = cipher - 26;
}
}
oddArray[i] = (char)cipher;
// Caesar Cipher Algorithm End
}
m++;
}
}
// Storing content of even and
// odd array to the string array
for (int i = 0; i < charArray.length(); i++) {
if (i % 2 == 0) {
charArray[i] = oddArray[k];
k++;
}else {
charArray[i] = evenArray[j];
j++;
}
}
// Generating a Cipher Text
// by charArray (Caesar Cipher)
for (char d : charArray) {
cipherText = cipherText + d;
}
// Return the Cipher Text
return cipherText;
}
// Driver code
int main() {
string pass = "hellotutorialspoint";
string key = "qwer";
cout <<"Our Encrypted Message is: "<< hybridEncryption(pass, key);
return 0;
}
Output
Our Encrypted Message is: wqqklfrlsmvpoidxlfuorlw
Advantages of Hybrid Cryptography
Hybrid encryption is a combination of symmetric and asymmetric encryption, and it provides higher security than previous methods.The transmission of data becomes secure. Encrypting data during transmission gives security benefits exactly like how data security is ensured on all devices.