模加密问题 [英] modulo encrypt problem

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

问题描述




我正在尝试使用模数10算法获得一个非常简单的字符串加密函数




void ccencode(UNSIGNED8 * pattern){


UNSIGNED8 key [] = CCKEY;

UNSIGNED8 digval = 0;

UNSIGNED8计数;


for(count = 0; count<(strlen(pattern)-4); count ++){


digval =((*(pattern + strlen(pattern)-count-1) - ''0'')+ key [strlen(pattern)-count-1] + digval)%10;

*(模式+ 4 +计数)= digval +''0'';

}


} / * ccencode * /

要加密的模式是一串ASCII数字。这个函数

从最后一位开始,并将它添加到键中的相应数字
(整数的关键数组是#defined其他地方),然后模数除以它

乘以10总是以0到9的数字结束。然后将数字添加到''0'来加上'0''来制作一个ASCII字符并写入模式。数字

然后累积到下一个等等。


另外两个特征是模式的前4位数是

故意未加密,其余部分不仅加密,而且

也按顺序颠倒。


我的问题是前6位数字处理正确但是

余额没有。接下来的3个加密数字比它们应该的数字大1美元,然后剩下的3个数字出来了。


当然这不能成为舍入效应%是一个

整数除以整数余数。该算法在Borland C ++编译器上运行良好

但在gcc上运行不正常。请注意,由于其他原因,我必须在ISO模式下运行gcc




任何想法有什么问题?

谢谢提前

John Blackburn

Hi,

I am trying to get an extremely simple character string encryption function
to work using modulo 10 arithmetic.

void ccencode(UNSIGNED8* pattern) {

UNSIGNED8 key[] = CCKEY;
UNSIGNED8 digval = 0;
UNSIGNED8 count;

for (count=0; count < (strlen(pattern)-4); count++) {

digval = ((*(pattern+strlen(pattern)-count-1)-''0'')+key[strlen(pattern)-count-1]+digval)%10;
*(pattern+4+count) = digval+''0'';
}

} /* ccencode */
The pattern to be encrypted is a string of ASCII numeric digits. This function
starts with the last digit and adds it to the corresponding digit in the key
(the key array of integers is #defined elsewhere) and then modulo divides it
by 10 to always end up with a digit in the range 0 - 9. The digit is then
added to ''0'' to make an ASCII character and written to the pattern. the digit
then accumulates into the next and so on.

Two other characteristcs are that the top 4 digits of the pattern are
deliberately unencrypted and the remainder are not only encrypted, but
also reversed in sequence.

My problem is that the first 6 digits are processed correctly but the
remainder do not. The next 3 encrypted digits are 1 greater than they should be
and the remaining 3 are then way out.

Surely this cannot be a rounding effect as % is an
integer division giving an integer remainder. The algorithm works fine
on a Borland C++ compiler but not on gcc. Note that I have to run gcc
in ISO mode for other reasons.

Any ideas what is wrong ?
Thanks in advance
John Blackburn

推荐答案

" john blackburn"写道:
"john blackburn" writes:
我试图用一个非常简单的字符串加密功能
使用模10运算工作。

void ccencode(UNSIGNED8 *模式){

UNSIGNED8 key [] = CCKEY;
UNSIGNED8 digval = 0;
UNSIGNED8计数;

for(count = 0; count<(strlen(pattern)-4); count ++){

digval =
((*(pattern + strlen(pattern)-count-1) - ''0 '')+ key [strlen(pattern)-count-1] + digval)%10;
*(pattern + 4 + count)= digval +''0'';
}

} / * ccencode * /

要加密的模式是一串ASCII数字。这个
函数从最后一个数字开始并将它添加到
键中的相应数字
(整数的键数组是#defined其他地方)然后模数除以数字然后累积到下一个等等。

其他两个特征是模式的前4位数字是故意未加密的,其余的是不仅加密,而且还按顺序颠倒。

我的问题是前6位数字处理正确,但
余数没有。接下来的3个加密数字比它们应该是
大1,然后剩下的3个数字出来。

这肯定不是一个舍入效应,因为%是一个
整数除法给出整数余数。该算法在Borland C ++编译器上运行良好,但在gcc上运行不正常。请注意,由于其他原因,我必须在ISO模式下运行gcc


任何想法有什么问题?
I am trying to get an extremely simple character string encryption
function
to work using modulo 10 arithmetic.

void ccencode(UNSIGNED8* pattern) {

UNSIGNED8 key[] = CCKEY;
UNSIGNED8 digval = 0;
UNSIGNED8 count;

for (count=0; count < (strlen(pattern)-4); count++) {

digval =
((*(pattern+strlen(pattern)-count-1)-''0'')+key[strlen(pattern)-count-1]+digval)%10;
*(pattern+4+count) = digval+''0'';
}

} /* ccencode */
The pattern to be encrypted is a string of ASCII numeric digits. This
function
starts with the last digit and adds it to the corresponding digit in the
key
(the key array of integers is #defined elsewhere) and then modulo divides
it
by 10 to always end up with a digit in the range 0 - 9. The digit is then
added to ''0'' to make an ASCII character and written to the pattern. the
digit
then accumulates into the next and so on.

Two other characteristcs are that the top 4 digits of the pattern are
deliberately unencrypted and the remainder are not only encrypted, but
also reversed in sequence.

My problem is that the first 6 digits are processed correctly but the
remainder do not. The next 3 encrypted digits are 1 greater than they
should be
and the remaining 3 are then way out.

Surely this cannot be a rounding effect as % is an
integer division giving an integer remainder. The algorithm works fine
on a Borland C++ compiler but not on gcc. Note that I have to run gcc
in ISO mode for other reasons.

Any ideas what is wrong ?




我没有我试着分析一下。但要注意char的默认值
" signage"标准中未定义;它可以是签名或未签名,

它是供应商选项。因此,如果您在

程序中的任何位置使用char类型,请务必明确指定为无符号。




I didn''t try to analyze that. But be aware that the default for char
"signage" is undefined in the standard; it can be either signed or unsigned,
it is a vendor option. So if you are using char type anywhere in your
program be sure to explicitly specify it as unsigned.



osmium写道:
osmium wrote:
我没有尝试分析它。但要注意char的默认值
signage标准中未定义;它可以是签名或
未签名,
它是供应商选项。因此,如果您在
程序中的任何位置使用char类型,请确保将其明确指定为unsigned。
I didn''t try to analyze that. But be aware that the default for char
"signage" is undefined in the standard; it can be either signed or
unsigned,
it is a vendor option. So if you are using char type anywhere in your
program be sure to explicitly specify it as unsigned.




在主头文件中,它指出: -

#define unsigned char UNSIGNED8



In the main header file it states :-
#define unsigned char UNSIGNED8


john blackburn< jo ****************** ***@lintonhealy.co.uk>写道:
john blackburn <jo*********************@lintonhealy.co.uk> wrote:
for(count = 0; count<(strlen(pattern)-4); count ++){

digval =((*(pattern + strlen(pattern)-count-1) - ''0'')+ key [strlen(pattern)-count-1] + digval)%10;
*(pattern + 4 + count)= digval +' '0'';
for (count=0; count < (strlen(pattern)-4); count++) {

digval = ((*(pattern+strlen(pattern)-count-1)-''0'')+key[strlen(pattern)-count-1]+digval)%10;
*(pattern+4+count) = digval+''0'';




假设strlen(模式)> 5,你在自己的输入上涂鸦

缓冲区。当count == 0时,你从模式[strlen(pattern)-1]中读取,但是你

写入模式[4]。当计数到达strlen(模式)-5时,你

写入模式[strlen(pattern)-1],但你从模式[4]中读取...

已包含其新值。早期

模式成员的原始价值甚至从未被读过。当你仍然需要模式的原始值时,你需要使用第二个缓冲区

来保存输出。


Richard



Assuming that strlen(pattern)>5, you''re scribbling over your own input
buffer. When count==0, you read from pattern[strlen(pattern)-1], but you
write to pattern[4]. By the time count gets to strlen(pattern)-5, you
write to pattern[strlen(pattern)-1], but you read from pattern[4]...
which already contains its new value. The original values of the early
members of pattern never even get read. You need to use a second buffer
to hold the output while you still need the original value of pattern.

Richard


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

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