试图做书密码。内存错误(访问冲突)

时间:2018-05-24 00:15:39

标签: c++ encryption memory

我试图做一本书密码。我生成一个随机数的向量,然后把它放到表中然后我有我的加密算法。它没有用,当我把算法输入cout时 - 它会向我显示正确的加密数字。当我尝试将它移动到我的int指针时,它在main函数中显示我的内存错误。

#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <ctime>

using namespace std;

void generate( int tab[26][26] )
{
        int h;
        vector<int> table;
        for(int i = 0; i<676; i++)
        {
                table.push_back(i+1);
        }
        int g = 0;
        random_shuffle(table.begin(), table.end());
        for(int x = 0; x < 26; x++)
        {
                for(int y = 0; y < 26; y++)
                {
                        tab[x][y] = table[g];
                        g++;
                }
        }
}

void print_cipher_table(int tab[26][26])
{
        char sign = 'A';
        cout << "    ";
        for (int n = 0; n<26; n++)
        {
                cout <<  sign++ << "   ";
        }
        cout << "\n";
        sign = 'A';
        for(int x = 0; x<26; x++)
        {
                cout << sign++ << "  ";
                for(int y = 0; y<26; y++)
                {
                        if(tab[x][y] < 10) cout << "00" << tab[x][y] << " ";
                        else if(tab[x][y] < 100) cout << "0" << tab[x][y] << " ";
                        else cout << tab[x][y] << " ";
                }
                cout << "\n";

        }
}

void encrypting(int tab[26][26], string s_true, string s_false, int *result)
{
        int row, column, i=0;

        while(s_true[i])                // pêtla wykonuje siê dopóki nie napotka na koniec stringu
        {
                if(s_true[i]>64 && s_true[i]<91)
                {
                        row = s_true[i] -'A';
                        column = s_false[i] - 'A';
                }
                else {
                        cout << "NIE PODAŁEŚ WIELKICH LITER!" << endl;
                        exit(0);
                }

                cout << "\t" << tab[row][column];
                result[i] = tab[row][column];
                i++;
        }
}

int main()
{
        int tab[26][26], slength, *crypted;
        string s_true, s_false;
        string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        cout << "Prosze podac tekst prawdziwy: ";
        cin >> s_true;
        cout << "Prosze podac tekst falszywy: ";
        cin >> s_false;
        if(s_true.length() != s_false.length())
        {
                cout << "Chujowa dlugosc tekstu. Wypierdalaj";
                exit(1);
        }
        slength = s_true.length();
        generate( tab );
        print_cipher_table( tab );
        cout << "\n\n\n";
        encrypting(tab, s_true, s_false, crypted);
        cout << *crypted;
        return 0;
}

0 个答案:

没有答案