加密数字C++
所以我试图加密一个四位数的整数,在这个数字上加七,然后把整个数字除以十。在我的程序中,我分别取每个数字,然后我需要将整个数字除以10。如何将所有独立的整数组合成一个四位数加密数字C++,c++,encryption,decimal,C++,Encryption,Decimal,所以我试图加密一个四位数的整数,在这个数字上加七,然后把整个数字除以十。在我的程序中,我分别取每个数字,然后我需要将整个数字除以10。如何将所有独立的整数组合成一个四位数 #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 fo
#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);
}
}
正如你可能注意到的,我将每个数字分开。我需要一起做。然后我还创建了一个解密程序,我将在一个单独的程序中使我返回到原始编号。这可能是您想要的:
int e = (a*1000)+(b*100)+(c*10)+d;
e=e/10;
这就是你可能要寻找的:
int e = (a*1000)+(b*100)+(c*10)+d;
e=e/10;
将单个数字组合成一个四位数字很简单;只需将第一个数字乘以1000,将第二个数字乘以100,依此类推
但这是一个单向算法;您将永远无法从中检索原始的四位数字。将单个数字组合成一个四位数字很简单;只需将第一个数字乘以1000,将第二个数字乘以100,依此类推
但这是一个单向算法;您将永远无法从中检索原始的四位数。从您的描述中不清楚加法是否应为模10;如果是的话
((((((a % 10) * 10) + (b % 10)) * 10) + (c % 10)) * 10) + (d % 10)
如果你不想要模10
(((((a * 10) + b) * 10) + c) * 10) + d
从你的描述中不清楚加法是否应该是模10;如果是的话
((((((a % 10) * 10) + (b % 10)) * 10) + (c % 10)) * 10) + (d % 10)
如果你不想要模10
(((((a * 10) + b) * 10) + c) * 10) + d
根据您的评论,您正在尝试对进行修改,在这种情况下,您应该使用模数运算符%,而不是整数除法运算符/。使用整数除法会丢失阻止解密的信息。当数字位于{0,1,2}时,除法结果为0。当它在{3,4,5,6,7,8,9}中时,除法的结果是1。如果没有已丢弃的一些附加信息,则无法将{0,1}解密回原始数字 如果要使用Caesar密码方法逐位加密,则应使用,以便每个数字都有一个唯一的加密值,可以在解密过程中检索该值。如果这真的是您正在寻找的,那么您应该执行以下操作以使用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;
当然,前提是您已经正确验证了输入,而不是上述示例中的情况。根据您的评论,您正在尝试对进行修改,在这种情况下,您应该使用模运算符%,而不是整数除法运算符/。使用整数除法会丢失阻止解密的信息。当数字位于{0,1,2}时,除法结果为0。当它在{3,4,5,6,7,8,9}中时,除法的结果是1。如果没有已丢弃的一些附加信息,则无法将{0,1}解密回原始数字 如果要使用Caesar密码方法逐位加密,则应使用,以便每个数字都有一个唯一的加密值,可以在解密过程中检索该值。如果这真的是您正在寻找的,那么您应该执行以下操作以使用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;
这当然前提是你已经正确地验证了你的输入,而不是你上面的例子。撇开你几乎肯定想要mod而不是像@And那样除法的事实不谈,还有不止一种方法可以把数字变成数字 现在很多使用口译语言的人可能都想象征性地使用口译。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位数字的狭义情况下,它的效率不如乘法,因为往返于字符串。但是很高兴知道这个技巧。此外,它是一种更易于维护和适应的编码风格
如果包含,您将获得stringstream。忽略一个事实,即您几乎肯定想要mod而不是像@And那样除法。而且,有多种方法可以将数字转换为数字 现在很多使用口译语言的人可能都想象征性地使用口译。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位数字的狭义情况下,它的效率不如乘法,因为往返于字符串。但是很高兴知道这个技巧。此外,它是一种更易于维护和适应的编码风格
如果包含,您将得到stringstream。如果ext==1。您需要使用==运算符,但不能使用==运算符用于赋值,不用于逻辑比较。这个问题有点不清楚。你能解释一下你对a,b,c,d的输入范围吗?你把它们分开是什么意思
一起?这是家庭作业吗?如果是,请相应地标记。这不是加密。这是不可逆的。除以10后的余数就是人们所说的模!:P:Dif ext==1。您需要使用==运算符,但不能使用==运算符用于赋值,不用于逻辑比较。这个问题有点不清楚。你能解释一下你对a,b,c,d的输入范围吗?你把它们分开是什么意思?这是家庭作业吗?如果是,请相应地标记。这不是加密。这是不可逆的。除以10后的余数就是人们所说的模!:P:D如果你愿意,你也可以在这一步中加上7!谢谢还有人告诉我这不是加密。我正试图按照我的书,但遇到了麻烦。如果你想,你也可以在这一步中加入7的加法!谢谢还有人告诉我这不是加密。我正试着看懂我的书,但遇到了麻烦。