2

我目前正在使用 Teensy 3.2 进行 arduino 项目。

该项目使用库文件夹中存在的一些加密代码。在构建加密代码时,需要的任何填充都被硬编码,直到其余的加密工作。随着加密的工作,我开始随机化填充过程。

我目前正在使用 Wprogram.h 中的 random() 函数。

使用手动填充时,输出看起来不错。但是,一旦使用随机数生成器,输出就会变得狂野且难以理解。

使用手动填充时:

//st is defined as byte[4][4]
st[0][0]=message; //character from original message, usually 'a' for testing
st[0][1]='1';
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 


//Output
//set state: a123CDEF89fghijk //state prior to encryption
//encrypted: ⸮$2⸮⸮k6⸮tʠ&⸮⸮ //this should look funny
//decrypted to: a123CDEF89fghijk //matches plaintext

使用随机填充时,我尝试了几种不同的方法:

//starting off with one random char for ease of testing
//random value seeded by  randomSeed(analogRead(7));
st[0][0]=message;
st[0][1]=byte(random(33,127));//using 33 to 127 to avoid non-print chars
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 

//-----Also Trying------
//It almost seemed like the data from the random number generator was flawed
//So I put in a scheme to ensure a hard coded character
if(random(100)%2)
  st[0][1]='a';
else
  st[0][1]='b';

//--------Also trying----------
//Since it seems like it is simply the existance of the random value that is
// problematic, I tried calling it without using it

int randval=random(100);
st[0][0]=message;
st[0][1]='1';
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 

//Typical Output
//set state: a123CDEF89fghijk //state prior to encrypting(random char shows)
//encrypted: *3}3⸮⸮⸮⸮⸮⸮⸮⸮⸮N //this should look funny
//decrypted to: ⸮⸮⸮⸮⸮⸮⸮⸮k⸮-⸮⸮ //this should not look funny

通过尝试几种不同的方法来创建随机字符,并且还看到随机值的存在正在造成麻烦。我怀疑 random() 函数在 arduino/teensy 上的工作方式存在一些问题。

在设置了随机数但没有使用它之后,我注意到的另一件事是,当我删除这行代码时,我必须重新编译几次,然后代码才能再次正常工作。这使我更加怀疑该功能或存在错误。

正如我在代码示例中所指出的,我正在使用一个未使用的引脚为随机数生成器播种。尽管我怀疑这将是一个问题,但为了安全起见,我尝试了一些不同的引脚。

所以我想我的问题可能是:有没有人知道可能导致这个问题的原因,或者可能会建议和替代方法来获得随机值?

4

0 回答 0