需要帮助算法 [英] need help with algorithm

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

问题描述




我正在尝试用C ++编写一个加法加密算法,它将通过在每个字符中添加一个随机数字来加密文本。一个字符串。

代码看起来类似于:


for(int i = 0; i< = tlength-1; i ++)/// tlength是字符串的长度

加密

{

ctext [i] + = x + i; ///// x是随机数和ctext是char *

ctext [i] + = cpass [i%plength] + tlength; /////// cpass是关键

(密码)

ctext [tlength-1-i] + = xi;

}


问题在于将char

的每个char的值添加为0,这将被解释为字符串的结尾NULL字符

。所以我需要的是一种可逆的方法,即使用其他值替换所有0值,然后在

解密加密字符串恢复它们。我不能只用另一个值替换

0值,因为该值也可能是在解密时在另一个char中遇到的
并且将是错误的

转换为0.请帮忙。


谢谢。

解决方案

Nemok写道:



我正在尝试用C ++编写一个加法加密算法,它将通过在每个字符中添加一个随机数字来加密文本。字符串。
代码看起来类似于:

for(int i = 0; i< = tlength-1; i ++)/// tlength是字符串到加密
{
ctext [i] + = x + i; ///// x是随机数,ctext是char *
ctext [i] + = cpass [ i%plength] + tlength; /////// cpass是关键
(密码)
ctext [tlength-1-i] + = xi;
}

问题是,虽然添加char的每个char的值可能是0,这将被解释为结束字符串的NULL字符。所以我需要的是一种可逆的方法,即用其他值替换所有0值,然后对加密字符串进行解密还原它们。我不能只用另一个值替换0值,因为该值也可能在解密时在另一个char中遇到,并且将被错误地转换为0.请帮助。

谢谢。




尝试使用std :: vector< char>而不是字符指针。你需要定义自己的输入/输出操作(这应该相当简单),但是你可以使用std :: vector<>: :size()而不是

std :: strlen来获得字符串的准确长度。或者,你可以用
跟踪char字符串的长度并阅读

并在不使用格式化IO的情况下编写它们(例如,std :: printf,

std :: cout)


干杯! --M


Nemok写道:



我正在尝试写一个添加剂C ++中的加密算法,它通过向字符串中的每个字符添加一个随机数字来加密文本。
代码看起来类似于:

for(int i = 0 ; i< = tlength-1; i ++)/// tlength是要加密的字符串的长度
{
ctext [i] + = x + i; ///// x是随机数,ctext是char *
ctext [i] + = cpass [i%plength] + tlength; /////// cpass是关键
(密码)
ctext [tlength-1-i] + = xi;
}

问题是,在添加每个char的值时,char
可能为0被解释为字符串的结尾NULL字符。所以我需要的是一种可逆的方法,即用其他值替换所有0值,然后对加密字符串进行解密还原它们。我不能只用另一个值替换0值,因为该值也可能在解密时在另一个char中遇到,并且将被错误地转换为0.请帮助。

谢谢。




最简单的解决方案是忽略输出中的0字节,并且

考虑密文作为二进制数据而不是字符串。你的
加密算法将输入视为二进制数据,所以没有

将输出转换为其他任何东西的简单方法。


一种解决方法是将您的算法转换为知道字符串使用的字符集的大小。例如,如果你只需要关注标准美国键盘上的字符,那么

你可以执行模数字符集大小的所有操作
并将每个明文和密文字节编码为

字符集的索引。然而,这比你刚才描述的简单的

循环复杂得多。


如果你的输出必须是一个字符串而你不能更改算法,

考虑在输出上运行第二次传递以转义所有0字节。要做到这一点,请选择另一个字节值,例如1,并将其用作

转义字符。运行密文,用两个1字节替换所有1个字节

,并用1个字节替换所有0个字节,后跟一个

2个字节。然后,在解密之前,运行字符串替换所有

双1字节,单个1字节,所有1,2字节模式,0

字节。任何其他1,x字节模式都是无效的。


请记住,你所描述的密码系统可以在几分钟内用

铅笔和纸张打破,所以不要用任何真实的东西。


Rennie deGraaf


一般来说,N个项目的[有限]映射N个项目是

可逆,当且仅当它是一对一(或上)时。


为什么不使用像Crypto ++这样的罐头库?

http://www.eskimo.com /~weidai/cryptlib.html


我还没用过它,但好像和其他人一样

相当容易合并到你的代码中,并且它也会更加安全。


--RY


Hi,

I am trying to write an additive encryption algorithm in C++ that will
encrypt a text by adding a random numer to each character in a string.
The code looks similar to this:

for(int i=0;i<=tlength-1;i++)///tlength is the length of the string to
encrypt
{
ctext[i]+=x+i;/////x is a random number and ctext is a char*
ctext[i]+=cpass[i%plength]+tlength;///////cpass is the key
(password)
ctext[tlength-1-i]+=x-i;
}

The problem is that while adding to the values of each char that char
might be 0 which will be interpreted like the ending NULL character of
the string. So what I need is a reversable method of
elliminating\replacing all 0 values with other values and then on
decryption of the encrypted string restore them. I can''t just replace
the 0 values with another value because that value might also be
encountered in another char on decryption and will be wrongfully
transformed to 0. Please help.

Thanks.

解决方案

Nemok wrote:

Hi,

I am trying to write an additive encryption algorithm in C++ that will
encrypt a text by adding a random numer to each character in a string.
The code looks similar to this:

for(int i=0;i<=tlength-1;i++)///tlength is the length of the string to
encrypt
{
ctext[i]+=x+i;/////x is a random number and ctext is a char*
ctext[i]+=cpass[i%plength]+tlength;///////cpass is the key
(password)
ctext[tlength-1-i]+=x-i;
}

The problem is that while adding to the values of each char that char
might be 0 which will be interpreted like the ending NULL character of
the string. So what I need is a reversable method of
elliminating\replacing all 0 values with other values and then on
decryption of the encrypted string restore them. I can''t just replace
the 0 values with another value because that value might also be
encountered in another char on decryption and will be wrongfully
transformed to 0. Please help.

Thanks.



Try using a std::vector<char> instead of a character pointer. You''ll
need to define your own input/output operations (which should be fairly
simple), but then you can use std::vector<>::size() instead of
std::strlen to get an accurate length of the string. Alternately, you
could just keep track of how long the char strings are by hand and read
and write them without using formatted IO (e.g., std::printf,
std::cout)

Cheers! --M


Nemok wrote:

Hi,

I am trying to write an additive encryption algorithm in C++ that will
encrypt a text by adding a random numer to each character in a string.
The code looks similar to this:

for(int i=0;i<=tlength-1;i++)///tlength is the length of the string to
encrypt
{
ctext[i]+=x+i;/////x is a random number and ctext is a char*
ctext[i]+=cpass[i%plength]+tlength;///////cpass is the key
(password)
ctext[tlength-1-i]+=x-i;
}

The problem is that while adding to the values of each char that char
might be 0 which will be interpreted like the ending NULL character of
the string. So what I need is a reversable method of
elliminating\replacing all 0 values with other values and then on
decryption of the encrypted string restore them. I can''t just replace
the 0 values with another value because that value might also be
encountered in another char on decryption and will be wrongfully
transformed to 0. Please help.

Thanks.



The easiest solution would be to ignore the 0 bytes in the output, and
consider the ciphertext as binary data rather than a string. Your
encryption algorithm treats the input as binary data, so there''s no
simple way of converting the output to anything else.

One work-around would be to convert your algorithm to be aware of the
size of the character set used by your strings. If, for instance, you
are only concerned with the characters on a standard US keyboard, then
you could perform all operations modulo the size of your character set
and encode each plaintext and ciphertext byte to an index into your
character set. This would be a lot more complicated than the simple
loop you just described, however.

If your output must be a string and you can''t change the algorithm,
consider running a second pass on the output to escape all 0 bytes. To
do that, pick another byte value, for instance 1, and use it as an
escape character. Run through the ciphertext, replacing all 1 bytes
with two 1 bytes, and replacing all 0 bytes with a 1 byte followed by a
2 byte. Then, before decrypting, run through the string replacing all
double 1 bytes with single 1 bytes, and all 1,2 byte patterns with 0
bytes. Any other 1,x byte pattern would be invalid.

Bear in mind that the cryptosystem you described can be broken with
pencil and paper within minutes, so don''t use it for anything real.

Rennie deGraaf


In general, a [finite] mapping from N items to N items is
invertible if and only if it is one-to-one (or onto).

Why not used a canned library like Crypto++?

http://www.eskimo.com/~weidai/cryptlib.html

I haven''t used it, but it seems like it and others
of that ilk would be fairly easy to incorporate into
your code, and it would be more secure, too.

--RY


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

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