繁体   English   中英

在移位和 OR 操作后取回原始位

[英]Retrieve Back the Original Bits after a Bitshift and an OR operation

我有 2 个例程,一个发送加密数据,另一个解密它。 我不确定解析加密字段的正确方法。

void send_the_encrypt_code ( UINT32 field, UINT32 index )
{
   UINT32 encrypt_code = ( field << 5 | index );
   pass_the_encrypt_code ( encrypt_code );
}

void pass_the_encrypt_code ( UINT32 encrypt_code )
{
  UINT32 field ;
  UINT32 index;

 /* How do I parse the field and index values from the encrypt_code and assign to the local variables field and index in this routine */??

}

提前致谢。

嗯,正如我在评论中所说的,我认为不可能恢复这种转变......

如果您只需要打包和模糊字段,您只需连接数据并应用简单的 XOR 编码...

这样的事情对你有用吗?

请注意,您不能将打包数据存储在与原始数据大小相同的容器中,因此 char*.

char key[] = {'P','A','S','S','K','E','Y','1'};

void decrypt ( char * encrypted){
   UINT32 field, index;
   UINT32 xor_field, xor_index;

   memcpy(&xor_field,&encrypted[0],sizeof(xor_field));
   memcpy(&xor_index,&encrypted[4],sizeof(xor_index));

   memcpy(&field,&key[0],sizeof(field));
   memcpy(&index,&key[4],sizeof(index));

   field ^= xor_field;
   index ^= xor_index;
}

void encrypt ( UINT32 field, UINT32 index ){
   char encrypted[8];
   UINT32 xor_field, xor_index;

   memcpy(&xor_field,&key[0],sizeof(xor_field));
   xor_field ^= field;
   memcpy(&xor_index,&key[4],sizeof(xor_field));
   xor_index ^= index;

   memcpy(&encrypted[0],&xor_field,sizeof(xor_field));
   memcpy(&encrypted[4],&xor_index,sizeof(xor_index));

   decrypt(encrypted);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM