加密数字 C++

发布于 2024-12-10 09:15:32 字数 1053 浏览 1 评论 0原文

因此,我尝试通过将数字加七然后将整个数字除以十来加密四位整数。在我的程序中,我分别取每个数字,然后我需要将整个数字除以十。如何将所有单独的 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);
    }
}

正如您可能注意到的那样,我将每个数字分开。我需要一起做它们。然后我还创建一个解密,我将在一个单独的程序中返回原始号码。

So I am trying to encrypt a four digit integer by adding seven to the digit then dividing the whole digit by ten. 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?

#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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

影子的影子 2024-12-17 09:15:32

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

如果您想使用凯撒密码方法逐位加密,您应该使用模算术 使得每个数字都有一个唯一的加密值,可以在解密过程中检索到。如果这确实是您想要的,那么您应该执行类似以下操作来使用 7 进行加密:

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

要解密,您减去 7,这在 mod 10 算术中是加 3,所以这将是:

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

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

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 (/). 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).

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:

    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:

    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).

携余温的黄昏 2024-12-17 09:15:32

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

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

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.

But this is a one-way algorithm; you will never be able to retrieve the original four-digit number from this.

世态炎凉 2024-12-17 09:15:32

这就是您可能正在寻找的:

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

This is what youd'd probably be looking for :

int e = (a*1000)+(b*100)+(c*10)+d;
e=e/10;
烟花肆意 2024-12-17 09:15:32

从你的描述中不清楚加法是否应该以10为模;如果是的话

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

如果你不想要模 10

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

It's not clear from your description whether the addition should be modulo 10 or not; if so

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

if you don't want the modulo 10

(((((a * 10) + b) * 10) + c) * 10) + d 
海的爱人是光 2024-12-17 09:15:32

抛开你几乎肯定想要取模而不是除法这一事实(正如@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,您将获得 stringstream。

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!

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:

// 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. 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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文