简体   繁体   English

加密数字C ++

[英]Encrypt Digit C++

So I am trying to encrypt a four digit integer by adding seven to the digit then dividing the whole digit by ten. 因此,我试图通过将数字加7,然后将整个数字除以10来加密四位整数。 In my program I am taking each single digit separately and then I need to divide the whole digit by ten. 在我的程序中,我将每个数字分别取一,然后将整个数字除以十。 How can I combine all the separate int into one four digit number? 如何将所有单独的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);
    }
}

As you probably noticed I am dividing each number separately. 您可能已经注意到,我将每个数字分开。 I need to do them together. 我需要一起做 Then I am also creating a decrypting which I will get me back to the original number in a separate program. 然后,我还要创建一个解密程序,然后在单独的程序中将其恢复为原始编号。

Based on your comment you are trying to do a variation on the Caesar Cipher , in which case you should be using the modulus operator ( % ) not the integer division operator ( / ). 根据您的评论,您尝试对Caesar Cipher进行变体,在这种情况下,您应该使用模数运算符( % )而不是整数除法运算符( / )。 Using integer division loses information which will prevent you from decrypting. 使用整数除法会丢失信息,这将使您无法解密。 When your digit is in {0, 1, 2} your division results in a 0. When it is in {3, 4, 5, 6, 7, 8, 9}, the division results in a 1. You can't decrypt {0, 1} back into the original number without some additional information (which you have discarded). 当数字位于{0,1,2}中时,除法结果为0。当数字位于{3,4,5、6、7、8、9}中时,除法结果为1。将{0,1}解密回原始编号,而无需一些其他信息(已丢弃)。

If you want to encrypt on a digit by digit basis using the Caesar Cipher approach, you should be using modulo arithmetic so that each digit has a unique encrypted value which can be retrieved during decryption. 如果要使用凯撒密码方法逐位加密,则应使用模运算,以便每个数字都有一个唯一的加密值,该值可以在解密期间取回。 If that's really what you are looking for then you should be doing something like the following to encrypt with a 7: 如果这确实是您想要的,那么您应该执行以下类似的操作以使用7进行加密:

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

To decrpyt, you subtract 7, which in mod 10 arithmetic is an addition by 3, so that would be: 要递减,您要减去7,在mod 10算术中是3的加法,因此将是:

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

This of course presupposes you've properly validated your input (which isn't the case in your example above). 当然,这前提是您已经正确验证了您的输入(上面的示例中不是这种情况)。

This is what youd'd probably be looking for : 这可能是您想要的:

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

Combining the individual digits into one four-digit number is simple; 将各个数字组合成一个四位数的数字很简单; just multiple the first digit by 1000, add the second multiplied by 100, and so on. 只需将第一个数字乘以1000,然后将第二个数字乘以100,依此类推。

But this is a one-way algorithm; 但是,这是一种单向算法。 you will never be able to retrieve the original four-digit number from this. 您将永远无法从中检索原始的四位数号码。

It's not clear from your description whether the addition should be modulo 10 or not; 根据您的描述,不清楚该加法是否应取模10。 if so 如果是这样的话

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

if you don't want the modulo 10 如果您不希望取模10

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

Stepping aside the fact that you almost certainly want mod instead of divide (as @Andand has said), there's more than one way to turn the digits into a number! 撇开您几乎肯定要使用mod而不是除法这一事实(如@Andand所说),有多种方法可以将数字转换为数字!

A lot of people using interpreted languages these days would probably want to do it symbolically. 如今,许多使用解释型语言的人可能想象征性地做到这一点。 C++ can do that too, fairly neatly in fact: 实际上,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;

It won't be as efficient as multiplications in this narrow case of a 4 digit number, because of the round trip to a string and back. 在这种狭窄的4位数字情况下,由于往返于字符串并返回,因此效率不如乘法。 But good to know the technique. 但是很高兴知道这项技术。 Also it's a style of coding that can be a lot easier to maintain and adapt. 这也是一种编码风格,可以更轻松地维护和适应。

You'll get stringstream if you #include <sstream> . 如果#include <sstream>则将得到stringstream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM