暗号化する必要のある と同じサイズの配列のunion
間でを宣言します。double
uint8_t
暗号化するには、二重に入力し、バイトを暗号化します。復号化するには、バイトを復号化し、double を読み取ります。
このアプローチは、使用する暗号が同じサイズのメッセージを出力する場合、バイト以外のデータ型に拡張できます。
uint8_t *
パディングの場合、「データがwill sometimes work, sometimes not; AES-256 will work with a
double of size 8, but block implementations are liable to crash or corrupt data when working with a
float floatであるものを取得する」というより高速なアプローチでは、of size 4 (they will attempt to read 8 bytes, and write 8 bytes; where only four are actually available). Due to platform and compiler quirks, this may *still* work because after the
利用可能な「メモリ パディング」が存在する可能性があります。
安全のために、たとえば暗号が 256 ビット (32 バイト) にパディングされている場合、同様にパディングされるようにバイト配列の長さを修正する必要があります。これを行うためのあまりきれいではない方法の 1 つは、パディング カウント全体でバイト カウントを増やすことです。
#include <stdio.h>
typedef struct {
double a;
long v;
// Whatever else.
// ...
} payload_t;
union {
payload_t payload;
unsigned char bytes[sizeof(payload_t)+32]; // 256 bits of margin
} data;
int main(void)
{
data.payload.a = 3.14159;
data.payload.v = 123456789;
...
// Encrypt data.bytes, for a length integer multiple of 32 bytes
size_t length = ((sizeof(payload_t)+31)/32)*32;