C ++中的基本加密

时间:2017-03-31 15:58:35

标签: arrays math encryption

我应该为仿射密码编写一个程序,它接受两个密钥a和b的输入,检查它们是否为素数,并将它们应用于公式[y =(a * x + b) mod26]其中x是要加密的字符,y是加密字符,如果提供了有效值,请使用这些值加密用户提供给您的字符串并输出结果。我的问题是,在检查了密钥后,程序只是闲置而且没有进展,我不知道该怎么做。它要求两个键并检查它们是否为素数,但在检查第二个键后,程序没有做任何事情,包括退出。

#include <iostream>
#include <string>
#include <array>

using namespace std;
const int S = 11;
array<char, 52> LETTERS = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
array<char, 52> ENCRYPT;

string String() 
{
    char Array[S];
    string hellocipher;
    const string MESSAGE = "Please Enter Text Up To 11 Characters: ";

    do 
    {
        if(hellocipher.size() == 0)
            cout << MESSAGE << endl;
        else if(hellocipher.size() > S)
        {
            cout << "Wrong Amount Char" << endl;
            cout << MESSAGE << endl;
        }
        cin >> hellocipher;
    } while (hellocipher.size() == 0 || hellocipher.size() > S);

    return hellocipher;
}

bool IsPrime(int intNumber) 
{
    int i = 2;
    for (; i <= intNumber; i++)
    {
        if (intNumber % i == 0) 
            break;
    }
    return i == intNumber;
}

int GetKey() 
{
    int a = 0;
    const string MESSAGE = "Please Enter A Key value. Value must be prime number between 1 and 101 : ";
    do
    {
        if(a==0)
            cout << MESSAGE << endl;
        else 
        {
            cout << "This key value is not prime.\n";
            cout << MESSAGE << endl;
        }
        cin >> a;
    } while (a == 0 || !IsPrime(a)); 

    return a;
}

char Encrypt(char ch, int a, int b) 
{
    return (char)((((((int)ch)*a) + b) % 26)+65);
}

array<char, 52> EncArr(array<char, 52> arr,int A, int B) 
{
    //int size = arr.size;
    array<char, 52> arrReturn;
    for (int i = 0; i < arr.size(); i++)
    {
        arrReturn[i]=Encrypt(arr[i],A,B);
    }
    return arrReturn;
}

string  Process(string strEncrpt, array<char, 52> arrMapNonEnc, array<char, 52> arrMapEnc) 
{
    string strReturn="";

    for (int i = 0; i < strEncrpt.length(); i++)
    {
        for (int inx = 0; inx < 51; inx++)
        {
            if (arrMapNonEnc[inx] == strEncrpt[i]) 
            {
                strReturn.append(string(1, arrMapEnc[inx]));
                break;
            }
        }
    }
    return strReturn;
}

void PrintToScreen(string str ) 
{
    cout << str << endl;
}

int main()
{
    //Step 1: Get String for encription
    string nonencryptedcipher = String();
    //Step 2: Get Key 1 number
    int a = GetKey();
    //Step 3: Get Key 2 number
    int b = GetKey();

    array<char, 52> EncArr(array < char, 52 > arr, int a, int b);  //replace char with LETTERS
    //Step 4: Encript string
    string strEncpt = Process(nonencryptedcipher, LETTERS, ENCRYPT);
    //Step 5: Present on screen
    PrintToScreen(strEncpt);

    system("pause>null");
    return EXIT_SUCCESS;
}

0 个答案:

没有答案