📄 seril.c
字号:
#include <c8051F020.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define BYTEKEY 8 // the bytes of keysize
#define MELEGTH 512 // the bytes of keystream
/* rc4.c *//* * RC4 * * Author: Christophe De Canni\`ere, K.U.Leuven. *//* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */typedef unsigned char u8;
typedef unsigned long u32;
void SYSCLK_Init();
void PORT_Init();
void Uart0_Initial();
void send_char(u8 ch);
typedef struct{ u32 keylen; u32 i; u32 j; u32 s[256]; u8 key[32];} ECRYPT_ctx;
/* ------------------------------------------------------------------------- */void ECRYPT_keysetup( ECRYPT_ctx* ctx, const u8* key, u32 keysize, u32 ivsize){ u32 i; ctx->keylen = (keysize + 7) / 8; for (i = 0; i < ctx->keylen; ++i) ctx->key[i] = key[i];}/* ------------------------------------------------------------------------- */void ECRYPT_ivsetup( ECRYPT_ctx* ctx, const u8* iv){ u32 i, j;#if (ECRYPT_VARIANT == 1) u8* s = (u8*)ctx->s;#else u32* s = (u32*)ctx->s;#endif for (i = 0; i < 256; ++i) s[i] = i; for (i = j = 0; i < 256; ++i) { u32 a; j = (j + (a = s[i]) + ctx->key[i % ctx->keylen]) & 0xFF; s[i] = s[j]; s[j] = a; } ctx->i = 0; ctx->j = 0;}/* ------------------------------------------------------------------------- */#define ITERATE(n) \ do { \ u32 a, b; \ \ i = (i + 1) & 0xFF; \ j = (j + (a = s[i])) & 0xFF; \ \ s[i] = b = s[j]; \ output[n] = s[(b + (s[j] = a)) & 0xFF]; \ } while (0)
void ECRYPT_process_bytes( ECRYPT_ctx* ctx, u8* output, u32 msglen){ u32 i = ctx->i; u32 j = ctx->j;
u32 count;
#if (ECRYPT_VARIANT == 1) u8* s = (u8*)ctx->s;#else u32* s = (u32*)ctx->s;#endif for(count=0;count<msglen;count++)
{
ITERATE(count);
send_char(output[count]);
} ctx->i = i; ctx->j = j;}
void SYSCLK_Init (void)
{
int i;
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88;
}
void PORT_Init (void)
{
XBR0=0x04;
XBR1=0x00;
XBR2=0x40; // enable cross switch
P0MDOUT |= 0x01;
P2MDOUT |= 0x10; // P2.4 output;
}
void Uart0_Initial()
{
TMOD=0x20;
TH1=0x70;
TL1=0x70;
CKCON=0x10;
SCON0=0x50;
T2CON=0x00;
PCON=0x80;
TI0 = 0;
}
void send_char(u8 ch)
{
SBUF0=ch;
while(TI0==0) ;
TI0=0;
}
void main()
{
u8 key[BYTEKEY];
int i;
u32 j;
u8 output[MELEGTH];
u8 tmp;
ECRYPT_ctx a;
u8 *iv=NULL;
SYSCLK_Init();
PORT_Init();
Uart0_Initial();
WDTCN=0xDE;
WDTCN=0xAD;
P2=0x00; // P2.4=0
TR1=1; // The uart0 begins
for(i=0;i<BYTEKEY;i++)
key[i]=_getkey();
ECRYPT_keysetup(&a, key, BYTEKEY*8, 0);
ECRYPT_ivsetup(&a, iv);
P2=0x10; // P2.4=1
ECRYPT_process_bytes(&a, output, MELEGTH);
for(j=0;j<500000;j++)
;
TR1=0; // The uart0 stops
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -