📄 u8_test.cpp
字号:
// U8_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef unsigned long u32;
typedef unsigned char byte;
#define XW 5
#define KW 3
void Xor_4byte(byte* out, byte* a, byte* b)
{
out[0] = a[0]^b[0];
out[1] = a[1]^b[1];
out[2] = a[2]^b[2];
out[3] = a[3]^b[3];
}
void rotr_8bit (byte* out,byte* in)
{
out[0] = in[1];
out[1] = in[2];
out[2] = in[3];
out[3] = in[0];
}
void ltr_2bit(byte x[])
{
byte flag_new,flag_old;
flag_new = flag_old = 0;
for(int i = 0;i < 4;i++)
{
flag_new = x[i]&0x80;
x[i] <<= 1;
if(flag_old)
x[i] |= 0x01;
flag_old = flag_new;
}
}
void add_4byte(byte (*x)[4],byte (*y)[4])
{
int flag = 0;
byte a[1][4] ={0,0,0,0};
for(int i=0;i<4;i++)
{
if(( (*x)[i] + (*y)[i] + flag ) > 255)
{
(*a)[i] = ( (*x)[i] + (*y)[i] + flag )%256;
flag = ( (*x)[i] + (*y)[i] + flag )/255;
(*x)[i] = (*a)[i];
}
else
{
(*x)[i] = ( (*x)[i] + (*y)[i] + flag );
flag =0;
}
}
}
void muil_9byte(byte (*t)[4])
{
byte p[1][4] = {{0,0,0,0}};
for(int g = 0;g < 4; g++)
p[0][g] = (*t)[g];
ltr_2bit(*t);
ltr_2bit(*t);
ltr_2bit(*t);
add_4byte(t,p);
}
void enRUPT (byte (*x)[4], const u32 xw, byte (*key)[4], const u32 kw)
{
int r, s=4, n=s*(2*xw+kw);
printf("\nStart for encrpt!!!\n");
printf(">>>>>>>>>>>>>>>>\n");
for (r=1; r<=n; r++)
{
byte p[1][4] = {{0,0,0,0}};
for(int g = 0;g < 4; g++)
p[0][g] = x[r%xw][g];
byte q[1][4] = {{0,0,0,0}};
for(int g1 = 0;g1 < 4; g1++)
q[0][g1] = x[(r-1)%xw][g1];
ltr_2bit(*q);
Xor_4byte((x[r%xw]),(x[(r+1)%xw]),(*q));
Xor_4byte(x[r%xw],x[r%xw],key[r%xw]);
byte r_r[1][4] ={{r,0,0,0}};
Xor_4byte(x[r%xw],x[r%xw],*r_r);
byte t[1][4] = {{0,0,0,0}};
rotr_8bit (*t,x[r%xw]);
muil_9byte(t);
Xor_4byte(x[r%xw],*t,key[r%xw]);
Xor_4byte(x[r%xw],x[r%xw],*p);
}
printf("the encrpt is done!\n");
printf("After the encrypted express:%x \n",*(int*)x);
}
void unRUPT (byte (*x)[4], const u32 xw, byte (*key)[4], const u32 kw)
{
int r, s=4, n=s*(2*xw+kw);
printf("\n\nStart for Decrypt!\n",r);
printf(">>>>>>>>>>>>>>>>\n");
for (r=n; r ; r--)
{
byte p[1][4] = {{0,0,0,0}};
for(int g = 0;g < 4; g++)
p[0][g] = x[r%xw][g];
byte q[1][4] = {{0,0,0,0}};
for(int g1 = 0;g1 < 4; g1++)
q[0][g1] = x[(r-1)%xw][g1];
ltr_2bit(*q);
Xor_4byte((x[r%xw]),(x[(r+1)%xw]),(*q));
Xor_4byte(x[r%xw],x[r%xw],key[r%xw]);
byte r_r[1][4] ={{r,0,0,0}};
Xor_4byte(x[r%xw],x[r%xw],*r_r);
byte t[1][4] = {{0,0,0,0}};
rotr_8bit (*t,x[r%xw]);
muil_9byte(t);
Xor_4byte(x[r%xw],*t,key[r%xw]);
Xor_4byte(x[r%xw],x[r%xw],*p);
}
printf("After the uncrypted express: ");
for(int j= 0;j<XW;j++)
printf(" 0x%x",*(int*)x[j]);
}
int main(void)
{
/*byte num[4]={0x01,0x02,0x03,0x4};
data在内存中的表示为0x01,0x02,0x03,0x04; data = 0x02010403;
u32 data;
for(int i=0;i<4;i++){ // 把int->char[];
num[i]=(byte)(data>>8*i & 0(*x)FF);
*/
byte test[XW][4] = {{0x3,0x0,0x2,0x0},{0x4,0x0,0x0,0x1},{0x3,0x5,0x0,0x1},{0x23,0x43,0x34,0x34},{0xe3,0xf1,0xdd,0x0}};
byte key[KW][4] = {{2,0,0,0},{3,0,0,0},{5,2,1,3}};
printf("\n\n\n\n\n\n");
printf("The Plaintext :");
for(int i1 = 0;i1<XW;i1++)
printf(" 0x%x",*(int*)test[i1]);
printf("\nThe Key :");
for(int i2= 0;i2<KW;i2++)
printf(" 0x%x",*(int*)key[i2]);
enRUPT (test,XW,key,KW);
printf("The Ciphertext :");
for(int i3 = 0;i3<XW;i3++)
printf(" 0x%x",*(int*)test[i3]);
unRUPT (test,XW,key,KW);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -