为什么我的矩阵打印在3行和多列,而我需要相反的?我怎样才能解密消息?

  • 本文关键字:消息 解密 打印 3行 c++ testing encryption
  • 更新时间 :
  • 英文 :


我正试图通过将其打印在3列和尽可能多的行来加密消息。我还需要帮助逆转我的加密
我很难打印我的结果。我正在使用一个铁路围栏密码密钥N,其中规定了列的数量。N =列数,但在我的代码中,我使它默认= 3。我的条件是当明文写入时,每个字母在列上的垂直位置的顺序在一个重复的循环中左右变化然后在列
中读出密文。

int main()
{
int num_cols, num_spaces=0 ,f=0;

string message;

// accept the message from the user
cout<<" Enter the message to encrypt : ";
getline(cin,message);
num_cols=3;

// first we need to remove any spaces that might be there in the message
for(int i = 0 ; i<message.size(); ++i)
{
if(message[i] == ' ')
{
++num_spaces;
}
}
remove(message.begin(), message.end(),' ');
message.resize(message.size() - num_spaces);


cout<<"n The equivalent cipher text would be : "<<endl;
vector<vector<char>> matrix(3,vector<char>(message.size(),' '));

// encrypt the message
for(int i=0,j=0; i<message.size(); i++)
{
matrix[j][i] = message[i];
if(j == 3-1)
{
f=1;
}
else if(j==0)
f=0;
if(f==0)
{
j++;
}
else j--;
}

// Printing the grid
for (int i = 0; i < 3; i++) 
{
for (int j = 0; j < message.size(); j++) 
{
cout << matrix[i][j];
}
cout << endl;
}

return 0;
// end
}

那么,您的第一个问题,正确的输出很容易解决。你只是打了个小错字。加密后的文本将打印为:

// Printing the grid
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < matrix[i].size(); j++)
{
if (matrix[i][j] != ' ')
std::cout << matrix[i][j];
}
}

如果你想看到rails,那么请使用:

std::cout << "nn";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < matrix[i].size(); j++)
{
std::cout << matrix[i][j];
}
std::cout << 'n';
}

那么,问题就解决了。

破译也很简单。

您再次创建rails,并对字符串的长度执行与之前相同的操作。但是,不要在那个位置放一个字母,而是一个标记,例如"*"。

然后逐行(一行一行)通过您的轨道,每次当您找到标记时,将该标记替换为加密文本中的下一个字符。

然后再走你的轨道之字形,并收集所有字符在相应的位置。结果将是加密后的文本。

我准备了一个完整的加密和解密解决方案。轨道的数量可以在顶部指定。在我的例子中,我使用5。如果你愿意,请改成3。我说得很直白。为了更容易理解。代码还需要进一步优化。例如,有很多重复可以放入函数中。

。这只是一个例子:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// Please specify the number of rails:
constexpr size_t NumberOfRails = 5u;
constexpr char EmptyRailPosition = '.';
constexpr char UsedRailPosition = '*';
// Some aliases for easier writing and understanding
using Rail = std::vector<char>;
using Rails = std::vector<Rail>;
std::string clearText{ "abcd efgh ijkl mnopqrst uv wxyz" };
int main() {
// Remove all spaces from string
clearText.erase(std::remove_if(clearText.begin(), clearText.end(), ::isspace), clearText.end());
// Create the rails for the encryption
Rails encryptedRails(NumberOfRails, Rail(clearText.length(), EmptyRailPosition));
// Now encrypt
int railIndexIncrementor{ -1 };
size_t railIndex{};
// Go thorugh all characters in the clear string
for (size_t columnIndexInClearText{}; columnIndexInClearText < clearText.length(); ++columnIndexInClearText) {
// Write character on rail
encryptedRails[railIndex][columnIndexInClearText] = clearText[columnIndexInClearText];
// If we are at the top or at the bottom of our rails, then change direction
if (0 == (columnIndexInClearText % (NumberOfRails-1))) 
railIndexIncrementor = -railIndexIncrementor;

// Modify rail index. Increment or decrement. Depending on railIncrementor
railIndex = static_cast<int>(railIndex) + railIndexIncrementor;
}

// Here we will store the encrypted message
std::string encryptedMessage{};
// Show rails
std::cout << "nRails: nn";
for (const Rail& rail : encryptedRails) {
for (const char c : rail) {
std::cout << c;
// Build string with encrypted message
if (c != EmptyRailPosition) encryptedMessage += c;
}
std::cout << 'n';
}
// Show encrypted message
std::cout << "nnEncrypted message: nn" << encryptedMessage << "nn";

// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------

// Decryption

// ------------------------------------------------------------------------------------
// Build empty rails
Rails decryptedRails(NumberOfRails, Rail(clearText.length(), '.'));
railIndexIncrementor = -1;
railIndex = 0;
// Go through all necessary positions in the decryption rails
for (size_t columnIndexInEncryptedText{}; columnIndexInEncryptedText < encryptedMessage.length(); ++columnIndexInEncryptedText) {
// Write makrker on rail
decryptedRails[railIndex][columnIndexInEncryptedText] = UsedRailPosition;
// If we are at the top or at the bottom of our rails, then change direction
if (0 == (columnIndexInEncryptedText % (NumberOfRails - 1)))
railIndexIncrementor = -railIndexIncrementor;
// Modify rail index. Increment or decrement. Depending on railIncrementor
railIndex = static_cast<int>(railIndex) + railIndexIncrementor;
}
// Show rails with markers
std::cout << "nnRails with markers: nn";
for (const Rail& rail : decryptedRails) {
for (const char c : rail)  std::cout << c;
std::cout << 'n';
}
// Now exchange the markers with the characters of the encrypted string
size_t indexInEncryptedMessage{};
// Now, put a character from the string at positions with marker
for (Rail& rail : decryptedRails)
for (char& c : rail)
if (c == UsedRailPosition) c = encryptedMessage[indexInEncryptedMessage++];
// Show rails with replaced markers
std::cout << "nRails with replaced markers, so decrypted rails:nn";
for (const Rail& rail : encryptedRails) {
for (const char c : rail)  std::cout << c;
std::cout << 'n';
}

// Read back string from rails
railIndexIncrementor = -1;
railIndex = 0;
std::string decrytpedMessage{};
// Go through all necessary positions in the decryption rails
for (size_t columnIndexInEncryptedText{}; columnIndexInEncryptedText < encryptedMessage.length(); ++columnIndexInEncryptedText) {
// Read character from rail
decrytpedMessage += decryptedRails[railIndex][columnIndexInEncryptedText];
// If we are at the top or at the bottom of our rails, then change direction
if (0 == (columnIndexInEncryptedText % (NumberOfRails - 1)))
railIndexIncrementor = -railIndexIncrementor;
// Modify rail index. Increment or decrement. Depending on railIncrementor
railIndex = static_cast<int>(railIndex) + railIndexIncrementor;
}
// and, show decrypted message
std::cout << "nnDecypted message:nn" << decrytpedMessage << "nn";

return 0;
}

最新更新