Blowfish解密输出问题

时间:2015-08-04 11:05:37

标签: c encryption base64 blowfish

我有以下用C编写的源代码。它使用blowfish和base64(对输入/输出进行编码和编码)。问题取决于输入字符串的数量(当使用短明文时,它工作正常),大明文解密的纯文本输出已损坏(即几个字/字符不显示/切断)。加密文本的base64输出工作正常(sladex.org/blowfish.js/)。请帮帮我,谢谢

int main (void)  {
   BLOWFISH_CTX ctx;
   int n, i;

   // must be less than 56 bytes
   char *key = "1234567890";

   unsigned char *plaintext_string = "secretmessage";
   int plaintext_len = strlen(plaintext_string);

       unsigned char decrypt[10000];
       unsigned char crypt64[10000];
       unsigned char decrypt64[10000];

   unsigned char ciphertext_buffer[10000];
   unsigned char *ciphertext_string = &ciphertext_buffer[0];
   int ciphertext_len = 0;
   int ciphertext_len64 = 0;


   printf("Plaintext message string is: %s\n", plaintext_string);

   encryption (key, plaintext_string, ciphertext_string, crypt64, &ciphertext_len);

   printf("Encrypted 64 string is: %s\n", crypt64);

       decryption (key, decrypt, crypt64);

       printf("message Plain Text decode blowfish is: %s \n", decrypt);
  return 0;
  } 
int decryption (char *key, unsigned char *decrypt_string, unsigned char   *crypt64) 
{ 
BLOWFISH_CTX ctx;

unsigned long message_left;
unsigned long message_right;
int block_len;
int n,i;
    int keylen = strlen(key);
unsigned char ciphertext_buffer[10000];
unsigned char *ciphertext_string = &ciphertext_buffer[0];
int ciphertext_len;

decode_base64( crypt64, ciphertext_string);
    ciphertext_len = strlen (ciphertext_string);

Blowfish_Init(&ctx, key, keylen);

while(ciphertext_len)
{
    message_left = message_right = 0UL;

    for (block_len = 0; block_len < 4; block_len++)
    {
      message_left = message_left << 8;
      message_left += *ciphertext_string++;
      if (ciphertext_len)
       ciphertext_len--;
    }
    for (block_len = 0; block_len < 4; block_len++)
    {
       message_right = message_right << 8;
       message_right += *ciphertext_string++;
       if (ciphertext_len)
       ciphertext_len--;
    }

    Blowfish_Decrypt(&ctx, &message_left, &message_right);


    /* save the results of decryption */
    *decrypt_string++ = (unsigned char)(message_left >> 24);
    *decrypt_string++ = (unsigned char)(message_left >> 16);
    *decrypt_string++ = (unsigned char)(message_left >> 8);
    *decrypt_string++ = (unsigned char)message_left;
    *decrypt_string++ = (unsigned char)(message_right >> 24);
    *decrypt_string++ = (unsigned char)(message_right >> 16);
    *decrypt_string++ = (unsigned char)(message_right >> 8);
    *decrypt_string++ = (unsigned char)message_right;

}

}

0 个答案:

没有答案