【问题标题】:Encrypt Digit C++加密数字 C++
【发布时间】:2011-10-15 14:56:55
【问题描述】:

所以我试图通过将七加到数字然后将整个数字除以十来加密四位整数。在我的程序中,我分别取每个单个数字,然后我需要将整个数字除以十。如何将所有单独的 int 组合成一个四位数?

#include "stdafx.h"
using namespace std;

int main()
{
    //Define all variables needed
    int a,b,c,d,enc,ext;

    //Print dialog and input each single digit for the four number digit
    cout << "Enter Your First Digit:" << endl;
    cin >> a;
    cout << "Enter Your Second Digit:" << endl;
    cin >> b;
    cout << "Enter Your Third Digit:" << endl;
    cin >> c;
    cout << "Enter Your Fourth Digit:" << endl;
    cin >> d;
    //Add seven to each digit
    a += 7;
    b += 7;
    c += 7;
    d += 7;

    a /= 10;
    b /= 10;
    c /= 10;
    d /= 10;

    cout << "Your encrpyted digits:" << c << d << a << b <<endl;

    cout << "Enter 1 to exit:" <<endl;
    cin >> ext;

    if (ext = 1) {
        exit(EXIT_SUCCESS);
    }
}

您可能已经注意到,我将每个数字分开。我需要一起做。然后我还创建了一个解密程序,我将在一个单独的程序中让我恢复到原始号码。

【问题讨论】:

  • if (ext == 1)。您需要使用 == 运算符而不是 = 运算符。 = 用于分配而不是用于逻辑比较。这个问题有点不清楚。你能解释一下你对 a,b,c,d 的输入范围是多少?
  • “将它们分开”是什么意思?
  • 这是作业吗?如果是这样,请相应地标记。
  • 这是不是加密。这是不可逆的。
  • 除以十后的余数就是这些人所说的取模! :P :D

标签: c++ encryption decimal


【解决方案1】:

根据您的评论,您正在尝试对 Caesar Cipher 进行变体,在这种情况下,您应该使用模数运算符 (%) 而不是整数除法运算符 (/)。使用整数除法会丢失阻止您解密的信息。当您的数字在 {0, 1, 2} 中时,您的除法结果为 0。当它在 {3,4,5,6,7,8,9} 中时,除法结果为 1。您不能将 {0, 1} 解密回原始数字,无需任何额外信息(您已丢弃)。

如果您想使用凯撒密码方法逐位加密,您应该使用modulo arithmetic,以便每个数字都有一个唯一的加密值,可以在解密期间检索该值。如果这确实是您正在寻找的内容,那么您应该执行以下操作以使用 7 进行加密:

    a = (a + 7) % 10;
    b = (b + 7) % 10;
    c = (c + 7) % 10;
    d = (d + 7) % 10;

要 decrpyt,你减去 7,在 mod 10 算术中是 3 的加法,所以是:

    a = (a + 3) % 10;
    b = (b + 3) % 10;
    c = (c + 3) % 10;
    d = (d + 3) % 10;

这当然前提是您已经正确验证了您的输入(在上面的示例中并非如此)。

【讨论】:

    【解决方案2】:

    将单个数字组合成一个四位数字很简单;只需将第一个数字乘以 1000,然后将第二个数字乘以 100,依此类推。

    但这是一种单向算法;您将永远无法从中检索到原始的四位数字。

    【讨论】:

      【解决方案3】:

      这可能是你要找的东西:

      int e = (a*1000)+(b*100)+(c*10)+d;
      e=e/10;
      

      【讨论】:

      • 如果你愿意,你也可以在这个步骤中加入 7 的加法!
      • 谢谢!我也被告知这不是加密。我正在努力学习我的书,但遇到了麻烦。
      【解决方案4】:

      从您的描述中不清楚加法是否应为模 10;如果是这样

      ((((((a % 10) * 10) + (b % 10)) * 10) + (c % 10)) * 10) + (d % 10) 
      

      如果你不想要模 10

      (((((a * 10) + b) * 10) + c) * 10) + d 
      

      【讨论】:

        【解决方案5】:

        撇开您几乎肯定想要 mod 而不是除法这一事实(正如@Andand 所说),将数字转换为数字的方法不止一种!

        如今,许多使用解释性语言的人可能希望以象征性的方式进行。 C++ 也可以做到这一点,事实上相当巧妙:

        // create a string stream that you can write to, just like
        // writing to cout, except the results will be stored
        // in a string
        
        stringstream ss (stringstream::in | stringstream::out);
        
        // write the digits to the string stream
        ss << a << b << c << d;
        
        cout << "The value stored as a string is " << ss.str() << endl;
        
        // you can also read from a string stream like you're reading
        // from cin.  in this case we are reading the integer value
        // that we just symbolically stored as a character string
        
        int value;
        ss >> value;
        
        cout << "The value stored as an integer is " << value << endl;
        

        在这种 4 位数字的狭窄情况下,它不会像乘法那样有效,因为往返于字符串和返回。但很高兴知道这项技术。它也是一种更容易维护和适应的编码风格。

        如果您#include &lt;sstream&gt;,您将获得字符串流。

        【讨论】:

          猜你喜欢
          • 2019-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-06
          • 2015-10-08
          • 1970-01-01
          • 1970-01-01
          • 2010-09-18
          相关资源
          最近更新 更多