How to encrypt _c language for random numbers

Source: Internet
Author: User
Copy Code code as follows:

Random number encryption Algorithm a^b = C, A^c =b, b^c = A xor or encryption
Etual 2011-3-14

#include <stdio.h>

7byte Data and 1byte key
unsigned char code_buf[8] = {0x12,0x13,0x14,0x15,0x21,0x22,0x23,0x00};

void Print_buf (void)
{
int i;
for (i=0;i<8;i++)
{
printf ("%x,", Code_buf[i]);
}
printf ("\ n");
}

int main (void)
{
unsigned char key,new_key;
unsigned char Rand_digi;
int i;

printf ("Original buf is!\n");
Print_buf ();

Key is a private key that is owned by each of the two communications (required for encryption and decryption)
key = 0x55;
Rand_digi is a random number
Rand_digi = 0xe3; Can be a time stamp
Using private key to encrypt random number to get ciphertext as new key
New_key = key ^ Rand_digi;

Encrypt and encrypt data with this new key
for (i=0;i<7;i++)
{
Code_buf[i] ^= New_key;
}
This key is sent along with the data.
CODE_BUF[7] = New_key;

printf ("Encrypted buf is!\n");
Print_buf ();


Decryption process
Assuming the receiving end receives the encrypted 8-byte data correctly
printf ("Now decode:\n");
Because the last byte is key, use this to decrypt the previous data
for (i=0;i<7;i++)
{
Code_buf[i] ^= code_buf[7];
}
The last one is also encrypted, using the private key to restore can get random number
CODE_BUF[7] ^= key;

Print_buf ();

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.