加密问题 [英] encryption problem

查看:42
本文介绍了加密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用c ++进行编程,并设置了一个简单加密算法的任务。我选择从一个简单的

开始,每个字母都用字母表中的等效字母代替

写回溯,我希望很快就能让它更复杂。下面

是我写的程序,但它不起作用,它只返回你输入的完全相同的文本

。你有没有建议如何分类

这个,并建议任何生成随机字母的方法,

未来参考。


#include< iostream>

#include< string>

使用命名空间std;


char alphabet [27 ] =" ZYXWVUTSRQPONMLKJIHGFEDCBA";

char plain_text [27];

int string_length;


int main()

{

cout<< " text:";

cin> plain_text;

string_length = strlen(plain_text);

for(int i = 0; i< string_length; i ++)

{

plaintext [i] = alphabet [i];

}

cout<< plain_text;

system(" PAUSE");

i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet when
written backmards, i am hoping to make this more complex soon. Below
is the program i wrote but it does not work, it simply returns the
exact same text you enter. Could you please advise on how to sort
this, and also suggest any ways of generating random letters, for
future refernce.

#include <iostream>
#include <string>
using namespace std;

char alphabet[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
char plain_text[27];
int string_length;

int main()
{
cout << "text: ";
cin >plain_text;
string_length = strlen(plain_text);
for(int i=0; i < string_length; i++)
{
plaintext[i] = alphabet[i];
}
cout << plain_text;
system ("PAUSE");

推荐答案

" Wilson"写道:
"Wilson" wrote:

>我正在学习使用c ++编程,并设置了一个简单加密算法的任务。我选择从一个简单的

开始,每个字母都用字母表中的等效字母代替

写回溯,我希望很快就能让它更复杂。下面

是我写的程序,但它不起作用,它只返回你输入的完全相同的文本

。你能告诉我如何分类这个,并建议任何产生随机字母的方法,以便

将来参考。
>i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet when
written backmards, i am hoping to make this more complex soon. Below
is the program i wrote but it does not work, it simply returns the
exact same text you enter. Could you please advise on how to sort
this, and also suggest any ways of generating random letters, for
future refernce.



它没有做你说它对我做的事情。如果我输入mary,我会回来ZYXW。

你提供的向后字母表中的前四个字母作为文字。

这就是代码所说的应该做的。代码说:用Z代替第一个

字母,用Y代替下一个字母,依此类推。你甚至不会检查读取的字符*值*的
。想一想再试一次。 comp.lang.c的常见问题解答中包含

随机字母问题


我不认为编写加密是个好主意消息,即使在名为plain_text的变量中正确完成了
。这令人困惑。如果您只有
有几百字节的内存,请将其命名为buffer。如果你可以填写它,

创建一个名为cipher_text的新变量并将加密文本放在那里。

另外,下次剪切粘贴,你发布的内容不编译。

It didn''t do what you say it does for me. If I type mary I get back ZYXW.
The first four letters in the backwards alphabet you provided as a literal.
That''s what the code said it should do. The code says: replace the first
letter with Z, the next letter with Y and so on. You don''t even examine
the *value* of the character read. Think about it and try again. The
random letter issue is covered in the FAQ for comp.lang.c

I don''t think it is a good idea to write an enciphered message, even if done
properly, in a variable named plain_text. It is confusing. If you only
have a few hundred bytes of memory, name it buffer. If you can cram it in,
create a new variable named cipher_text and put the enciphered text there.
Also, cut and paste next time, what you posted does not compile.


#include< iostream>

#include< string>

using namespace std;


char alphabet [27] =" ZYXWVUTSRQPONMLKJIHGFEDCBA";

char plain_text [27];

int string_length;


int main()

{

cout<< " text:";

cin> plain_text;

string_length = strlen(plain_text);

for(int i = 0; i< string_length; i ++)

{

plaintext [i] = alphabet [i];

}

cout<< plain_text;

system(" PAUSE");
#include <iostream>
#include <string>
using namespace std;

char alphabet[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
char plain_text[27];
int string_length;

int main()
{
cout << "text: ";
cin >plain_text;
string_length = strlen(plain_text);
for(int i=0; i < string_length; i++)
{
plaintext[i] = alphabet[i];
}
cout << plain_text;
system ("PAUSE");



我也是新手所以它花了我花了几分钟才意识到你的代码错误了什么。我确定我会受到抨击(特别是当盲人领导盲人时),但我希望有人会纠正我们这两个人的b $ b我们下次可以做得更好。


这是你的代码:


#include< iostream>

#include< string>

using namespace std;


char alphabet [27] =" ZYXWVUTSRQPONMLKJIHGFEDCBA";

char plain_text [27];

int string_length;


int main()

{

cout << " text:";

cin> plain_text; // plain_text =" test"

string_length = strlen(plain_text); // string_length是4;

for(int i = 0; i< string_length; i ++)//这样做4次

{

plaintext [i] = alphabet [i]; //取字母[i]为Z,然后是Y,

然后是X然后是W并将其分配给明文[0-4],这相当于ZYXW;

}


cout<<纯文本; //输出ZYXW

系统(PAUSE);



-------------- ---------------

以下是我的想法:


#include< iostream>

#include< string.h>

using namespace std;


//这应该是所有可能的字符列表

转换

char decrypted [27] =" ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;;

//对应的转换文本列表

char encrypted [27] =" ZYXWVUTSRQPONMLKJIHGFEDCBA" ;; //加密密钥

char plain_text [80]; //纯文本字符串

char converted_text [80]; //转换后的字符串输出

int string_length; //初始化字符串


int main()

{


cout<< text:" ;; //输入一些文字

cin> plain_text; //将文本数组分配给plain_text

string_length = strlen(plain_text); //找到文本的长度

用户输入的字符串

for(int i = 0; i< string_length; ++ i)//用于当前字符在

字符串(我是当前字符)

{

for(int j = 0; j<(strlen(decrypted)) ; j ++)//代表

可能解密的字符数

{

if(decrypted [j] == plain_text [i]) //如果当前字符

匹配可能的解密字符列表

{

converted_text [i] = encrypted [j]; //然后取相应的

加密字符并将其分配给转换后的文本字符串


}

}

}

cout<< converted_text<< " \ n";

返回0;

}


----------- -------------


以下是我的想法:


- 我使用了解密字样但不完整的可能字符列表

输入。将ascii字符的整个列表与每个字符进行比较并输出偏移量(而不是

,取决于手动的1对1转换,这可能是一个更好的主意。输入角色列表)

这不应该太难。


- 我不确定我是否遵守任何正确的编码习惯通过在一个for循环中使用

变量作为嵌套for

循环的比较基础。这些构造应该是否应该(这是一个合适的词?)允许

混合变量?


- 我应该将for循环和字符比较移动到函数

在main()之外?


任何其他批评都将受到赞赏。请记住我要新手这么多额外的解释和示例会有所帮助。


谢谢!

I''m a newbie too so it took me a few minutes to realize what went
wrong with your code. I''m sure i''ll get flamed (especially when the
blind is leading the blind) but i''m hoping someone will correct both
of us so we can do better next time.

Here is your code:

#include <iostream>
#include <string>
using namespace std;

char alphabet[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
char plain_text[27];
int string_length;

int main()
{
cout << "text: ";
cin >plain_text; // plain_text = "test"
string_length = strlen(plain_text); //string_length is 4;
for(int i=0; i < string_length; i++) // do this 4 times
{
plaintext[i] = alphabet[i]; // take alphabet[i] which is Z, then Y,
then X then W and assign it to plaintext[0-4] which equates too ZYXW;
}

cout << plain_text; // output ZYXW
system ("PAUSE");


-----------------------------
Here are my thoughts:

#include <iostream>
#include <string.h>
using namespace std;

// this should be a list of all possible characters that can be
converted
char decrypted[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// list of the corresponding converted text
char encrypted[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; //encrypted key
char plain_text[80]; //plain text string
char converted_text[80]; //converted string for output
int string_length; // initialize the string

int main()
{

cout << "text: "; // enter some text
cin >plain_text; //assign the array of text to plain_text
string_length = strlen(plain_text); //find the length of the text
string entered by the user
for (int i=0; i<string_length; ++i) // for the current character in
the string (i is the current character)
{
for (int j=0; j<(strlen(decrypted)); j++) //for the number of
possible decrypted characters
{
if (decrypted[j] == plain_text[i]) //if the current character
matches in the possible list of decrypted characters
{
converted_text[i] = encrypted[j]; //then take the corresponding
encrypted character and assign it to the converted text string

}
}
}
cout << converted_text << "\n";
return 0;
}

------------------------

Here are my thoughts:

- i used a "decrypted" but incomplete list of possible characters
entered. Its probably a better idea to compare every character with
the entire list of ascii characters and output an offset (rather than
depending on a 1-to-1 translation on manually entered character list)
That shouldn''t be too difficult.

- i''m not sure i''m adhering to any proper coding practices by using a
variable from one for-loop as a basis for comparison in a nested for
loop. Should these "constructs" (is that an appropriate word?) allow
mingling of variables?

- should I move the for-loops and character comparisons to a function
outside of main()?

Any other critiques would be appreciated. Please keep in mind i''m a
newbie so additional explanation and examples would be helpful.

Thanks!




< do ************** @ gmail.comwrote in message ...

<do**************@gmail.comwrote in message...

我也是新手所以我花了几分钟时间才意识到你的代码出了什么问题。我确定我会受到抨击(特别是当盲人领导盲人时),但我希望有人会纠正我们这两个人的b $ b我们下次可以做得更好。

这是你的代码:

#include< iostream>

#include< string>

-----------------------------

以下是我的想法:


#include< iostream>

#include< string.h>

using namespace std; //对于纯粹的懒惰


//这应该是所有可能的字符的列表,可以是

转换

char decrypted [27] =" ABCDEFGHIJKLMNOPQRSTUVWXYZ英寸;

//对应的转换后的文本

加密炭的列表[27] =" ZYXWVUTSRQPONMLKJIHGFEDCBA英寸; file:// encrypted key

char plain_text [80]; file://纯文本字符串

char converted_text [80]; file://转换后的字符串输出

int string_length; //初始化字符串


int main(){

cout<< text:" ;; //输入一些文字

cin> plain_text; file://将文本数组分配给plain_text

string_length = strlen(plain_text); file://找到文本的长度

用户输入的字符串


for(int i = 0; i< string_length; ++ i) {//对于当前字符

字符串(i是当前字符)

for(int j = 0; j<(strlen(decrypted)); j ++ ){file://表示

可能解密的字符数

if(decrypted [j] == plain_text [i]){file://如果当前字符

匹配可能的解密字符列表

converted_text [i] = encrypted [j]; file://然后取相应的

加密字符并将其分配给转换后的文本字符串

}

}

}

cout<< converted_text<< " \ n";

返回0;

}

--------------- ---------


以下是我的想法:


- 我使用了解密字样但不完整的可能字符列表

输入。将ascii字符的整个列表与每个字符进行比较并输出偏移量(而不是

,取决于手动的1对1转换,这可能是一个更好的主意。输入角色列表)

这不应该太难。
I''m a newbie too so it took me a few minutes to realize what went
wrong with your code. I''m sure i''ll get flamed (especially when the
blind is leading the blind) but i''m hoping someone will correct both
of us so we can do better next time.
Here is your code:
#include <iostream>
#include <string>
-----------------------------
Here are my thoughts:

#include <iostream>
#include <string.h>
using namespace std; // for pure laziness

// this should be a list of all possible characters that can be
converted
char decrypted[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// list of the corresponding converted text
char encrypted[27] = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; file://encrypted key
char plain_text[80]; file://plain text string
char converted_text[80]; file://converted string for output
int string_length; // initialize the string

int main(){
cout << "text: "; // enter some text
cin >plain_text; file://assign the array of text to plain_text
string_length = strlen(plain_text); file://find the length of the text
string entered by the user

for (int i=0; i<string_length; ++i){ // for the current character in
the string (i is the current character)
for (int j=0; j<(strlen(decrypted)); j++){ file://for the number of
possible decrypted characters
if (decrypted[j] == plain_text[i]){ file://if the current character
matches in the possible list of decrypted characters
converted_text[i] = encrypted[j]; file://then take the corresponding
encrypted character and assign it to the converted text string
}
}
}
cout << converted_text << "\n";
return 0;
}
------------------------

Here are my thoughts:

- i used a "decrypted" but incomplete list of possible characters
entered. Its probably a better idea to compare every character with
the entire list of ascii characters and output an offset (rather than
depending on a 1-to-1 translation on manually entered character list)
That shouldn''t be too difficult.



见下文。

See below.


>

- 我是不确定我是否坚持任何正确的编码实践,使用一个for循环中的

变量作为嵌套for

循环的比较基础。这些构造应该是否应该(这是一个合适的词吗?)允许

混合变量?
>
- i''m not sure i''m adhering to any proper coding practices by using a
variable from one for-loop as a basis for comparison in a nested for
loop. Should these "constructs" (is that an appropriate word?) allow
mingling of variables?



你的长篇评论让这些线条充满了瑕疵,我不觉得它会分开来捡起它们。如果我理解你的要求,我认为答案是是。

The lines are so garbaged by your long comments, I don''t feel like picking
it apart. If I understand what you were asking, I think the answer is "yes".


>

- 我应该移动-loops和字符比较函数

在main()之外?
>
- should I move the for-loops and character comparisons to a function
outside of main()?



为什么不呢。其他一切都在main()之外! (全球大战糟糕!)。

Why not. Everything else is outside of main()! (global vars bad!).


>

任何其他批评将不胜感激。请记住我是一个

新手,所以额外的解释和示例会有所帮助。

谢谢!
>
Any other critiques would be appreciated. Please keep in mind i''m a
newbie so additional explanation and examples would be helpful.
Thanks!



OP包括< string>,你应该使用它。


#include< iostream>

#include< string>

#include< algorithm>


int main(){

// - 维护一个列表 -

std :: string decrypted(" ABCDEFGHIJKLMNOPQRSTUVWXYZ");

// - 复制并反转列表 -

std :: string encrypted(decrypted);

std :: reverse(encrypted.begin(),encrypted.end());


std :: cout<< encrypted<< std :: endl;

std :: cout<<" encrypted 9th char is:"

<< ; encrypted.at(8)<< std :: endl; //基于零的索引

返回0;

} // main()


现在查找''std :: string find()'',并使用它与您的第一个''思想''

以上。

提示:您不会在加密/解密中使用嵌套循环。只有''扫描''

输入字符串/ c-string。
http://www.dinkumware.com/manuals/


如果您遇到困难,请回复此处寻求帮助。


如果您阅读这个旧帖子,您可能会得到更多想法。

// - 原始消息 -

来自:Protoman< Pr * *********@gmail.com>

新闻组:comp.lang.c ++

发送时间:2007年7月2日星期一晚上8:00

主题:是否有任何方法可以使这段代码更紧凑,和/或能够更改

运行时?


但请记住,我没有告诉你。

[我不想因为做某人的家庭作业/练习而大喊大叫。 < G>]

-

Bob R

POVrookie

The OP included <string>, you should use that.

#include <iostream>
#include <string>
#include <algorithm>

int main(){
// - maintain one list -
std::string decrypted( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
// - copy and reverse the list -
std::string encrypted( decrypted );
std::reverse( encrypted.begin(), encrypted.end() );

std::cout<<encrypted<<std::endl;
std::cout<<"encrypted 9th char is:"
<<encrypted.at( 8 )<<std::endl; // zero based index
return 0;
} // main()

Now look up ''std::string find()'', and use it with your first ''thought''
above.
Hint: you will NOT use nested loops in the encrypt/decrypt. Only ''scan'' the
input string/c-string.
http://www.dinkumware.com/manuals/.

If you get stuck, post back here for help.

If you read this old thread, you may get some more ideas.
// - Original Message -
From: Protoman <Pr**********@gmail.com>
Newsgroups: comp.lang.c++
Sent: Monday, July 02, 2007 8:00 PM
Subject: Any way to make this code more compact, and/or be able to change at
runtime?

But remember, I didn''t tell you.
[ I don''t want to get yelled at for doing someones homework/exercises. <G>]
--
Bob R
POVrookie


这篇关于加密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆