📜  定制建筑密码算法(混合密码)

📅  最后修改于: 2021-04-29 11:31:14             🧑  作者: Mango

密码术是一个非常简单的概念,涉及处理字符串(或文本)以使中间人无法理解的内容。它具有一种非常有效的方式来加密或解密来自其他方的文本。某些示例包括Caesar密码,Viginere密码,Columner密码,DES,AES,并且列表继续。要开发自定义密码算法,可以使用混合加密算法。

混合加密是密码学中的一个概念,它组合/合并一个/两个密码学算法以生成更有效的加密文本。

例子:

FibBil密码学算法

问题陈述:

程序通过计算Fibonacci系列,将Fibonacci系列的条款与每个纯文本字母相加,直到密钥的长度,以生成加密的文本。

算法:

对于加密:从用户那里获取输入的纯文本和密钥,反转纯文本并将纯文本与密钥连接起来,然后将字符串复制到数组中。复制后,将数组元素分为两部分,EvenArray和OddArray,其中数组的偶数索引将放置在EvenArray中,OddArray相同。开始生成最接近密钥j长度的斐波那契数列F(i),使得c = i + j ,其中c是带有mod 26的密文。将所有c元素附加到CipherString中,然后加密完成! 。使用总结概念时,它是实施Caesar Cipher的重点。

解密:加密算法的副词

算法示例:

程序:

C++14
#include
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;
    }
}


输出:
riobkxezg

结论:

用于密码学的混合算法是有效的,因此,检测模式并解码消息不是很容易。这里,该算法是数学函数和凯撒密码的结合,从而实现了混合密码学算法。