c - 加密实现 - 令人困惑的结果

标签 c encryption cryptography

我正在尝试对我的一个项目使用简单的加密/解密 (treyfer),但没有可用的解密引用实现。我得到的(基于维基百科实现)是:

uint8_t rotl(uint8_t x) {
  return (x << 1) | (x >> 7);
}

uint8_t rotr(uint8_t x) {
  return (x >> 1) | (x << 7);
}

void encrypt (uint8_t text[8], uint8_t key[8]) {
  int r, i;
  uint8_t t;

  for (r=0; r<NUMROUNDS; r++) {
      text[8] = text[0];
      for (i=0; i<8; i++) {
          text[i+1] = rotl((text[i+1] + sbox[(key[i] + text[i]) % 256]));
      }
      text[0] = text[8];
  }
}

void decrypt (uint8_t text[8], uint8_t key[8]) {
    int r, i;
    uint8_t t;

    for (r=0; r<NUMROUNDS; r++) {
        text[8] = text[0];
        for (i=8; i>=0; --i) {
            text[i+1] = rotr(text[i+1]) - sbox[(key[i] + text[i]) % 256];
        }
        text[0] = text[8];
    }
}


void printhex(uint8_t text[8]) {
    for(int i = 0; i < 8; i++) {
        printf("%02x", text[i]);
    }
}
int main() { 
    char text[8+1] = "AAAAAAAA";
    char key[8+1] = "AAAAAAAA";
    puts("plaintext:");
    printhex(text);
    puts("\nkey:");
    printhex(key);
    puts("\nencrypted:");
    encrypt(text, key);
         printhex(text);
         puts("\ndecrypted:");
        decrypt(text, key);
        printhex(text);
    }

其中 Sbox 是 AES S-box,NUMROUNDS = 128 但是,当我运行它时,明文和解密值的结果截然不同,这表明我的加密或解密不正确,但我不太明白是怎么回事。希望我没有错过任何太痛苦的事情。 我得到的输出是:

plaintext:
4141414141414141
key:
4141414141414141
encrypted:
658ed27d394127a0
decrypted:
a0460a65d755953f

最佳答案

我假设您的实现基于此:https://en.wikipedia.org/wiki/Treyfer

#include <stdio.h>
#include <string.h>
#include <stdint.h>

#define NUMROUNDS 100

static const uint8_t sbox[256] =   {
  //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
  0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x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xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };

void treyfer_encrypt(uint8_t text[8], uint8_t const key[8])
{
    unsigned i;
    uint8_t t = text[0];
    for (i = 0; i < 8*NUMROUNDS; i++) {
        t += key[i%8];
        t = sbox[t] + text[(i+1)%8];
        text[(i+1) % 8] = t = (t << 1) | (t >> 7);        /* Rotate left 1 bit */
    }
}

void encrypt(uint8_t text[8], uint8_t const key[8])
{
    unsigned int i = 0;
    unsigned int j = 0;
    uint8_t t = 0;

    t = text[0];

    for (j = 0; j < NUMROUNDS; j++)
    {
        for (i = 0; i < 8; i++)
        {
            t = t + key[i];
            t = sbox[t] + text[(i + 1) % 8];
            t = (t << 1) | (t >> 7);
            text[(i + 1) % 8] = t;
        }
    }
}

void decrypt(uint8_t text[8], uint8_t const key[8])
{
    int i = 0;
    int j = 0;
    uint8_t top = 0;
    uint8_t bottom = 0;

    for (j = 0; j < NUMROUNDS; j++)
    {
        for (i = 7; i >= 0; i--)
        {
            top = text[i] + key[i];
            top = sbox[top];

            bottom = text[(i + 1) % 8];
            bottom = (bottom >> 1) | (bottom << 7);

            text[(i + 1) % 8] = bottom - top;
        }
    }
}


void printhex(uint8_t text[8]) {
    int i;
    for(i = 0; i < 8; i++) {
        printf("%02x", text[i]);
    }
}
int main() {
    uint8_t text[8] = "AAAAAAAA";
    uint8_t text2[8] = "AAAAAAAA";
    uint8_t key[8] = "AAAAAAAA";

    puts("plaintext:");
    printhex(text);

    puts("\nkey:");
    printhex(key);

    puts("\n\nref encrypted:");
    treyfer_encrypt(text, key);
    printhex(text);

    puts("\n\nencrypted:");
    encrypt(text2, key);
    printhex(text2);

    puts("\n\ndecrypted:");
    decrypt(text2, key);
    printhex(text2);
    puts("\n");
}

结果

plaintext:
4141414141414141
key:
4141414141414141

ref encrypted:
cc3121ccab578d93

encrypted:
cc3121ccab578d93

decrypted:
4141414141414141

关于c - 加密实现 - 令人困惑的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303176/

相关文章:

java - Java Crypto JCA Provider 真的需要 windows dll 吗?

c - 如何从 C 中的只读 FIFO 读取一行?

java - ColdFusion 8 Diffie-Hellman 加密

php - 加密例程将 %3D 添加到输出末尾

java - 在 Node js 中通过 AES/GCM/NoPadding 算法使用 key 和 iv 加密有效负载并在 java 中解密

java - 为什么人们使用 bouncycaSTLe 而不是 Java 的内置 JCE 提供程序?有什么区别?

C - 按值传递结构+成员

C: undefined reference `dlopen`/`dlsym` 尽管添加了 `-ldl` 标志

C:链表困惑

python - 默认情况下 SecureSocial 为 SecureSocialPasswordHasher 使用什么加密?