⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csa.c

📁 CSA() 加扰解扰算法(DVB-C中用到)。内有实现源码
💻 C
📖 第 1 页 / 共 2 页
字号:







#define N 23 // assume TS packets, 184/8

void decrypt(unsigned char *ck, unsigned char *encrypted, unsigned char *h)
{
    FILE *fp4;
    unsigned char f_out_dname[20];
    unsigned char decrypted[0x100];
    int in_dex1;
    int i,j;
    int kk[57];
    unsigned char stream[8];
    unsigned char ib[8];
    unsigned char block[8];
    int fail;

    key_schedule(ck,kk);
  

    // 1st 4 bytes not encrypted
    for(i=0; i<4; i++)
        decrypted[i] = encrypted[i];

    // 1st 8 bytes of initialisation
    stream_cypher(1, ck, &encrypted[4], ib);

    for(j=1; j<(N+1); j++)
    {
        block_decypher(kk, ib, block);
       

        if (j != N)
        {
            stream_cypher(0, ck, NULL, stream);
            

            // xor sb x stream
            for(i=0; i<8; i++)  ib[i] = encrypted[4+8*j+i] ^ stream[i];
        }
        else
        {
            // last block - sb[N+1] = IV(initialisation vetor)(=0)
            for(i=0; i<8; i++)  ib[i] = 0;
        }
        
        // xor ib x block
        for(i=0; i<8; i++)  decrypted[4+8*(j-1)+i] = ib[i] ^ block[i];
        
    }


    printf("\nDecrypted Plaintext");
    for(i=0; i<188; i++)
    {
        if (!(i%0x10)) printf("\n%04x - ",i);
        printf("%02x ",decrypted[i]);
    }

   



	printf("\nPlease enter filename for output:\n");
		scanf("%s",&f_out_dname);

	fp4 = fopen(f_out_dname,"w");
	
	for(in_dex1=0; in_dex1<0x100; in_dex1++)
	{
		if(h[0]=='0' && h[1]=='x')
		{
			fprintf(fp4, "0x%x ",decrypted[in_dex1]);
		}
		else
		{
			fprintf(fp4, "%c", decrypted[in_dex1]);
		}

	}

	fclose(fp4);
    

}

/*
    encryption
        run the block cypher backwards on the whole packet
        this produces the ib[1..N] and sb[1]
        the go forwards
        xor the ib[2..N] with the stream cypher to give the encrypted data, sb[2..N]
*/
void encrypt(unsigned char *ck, unsigned char *decrypted, unsigned char *h)
{

    FILE *fp3;
    unsigned char encrypted[0x100];
    
    int in_dex;
    unsigned char f_out_name[20];
	
    int i,j;
    int kk[57];
    unsigned char stream[8];
    unsigned char ib[N+2][8];   // since we'll use 1..N and N+1 for IV
    unsigned char block[8];
    int fail;

    key_schedule(ck,kk);
  

    // 1st 4 bytes not encrypted
    for(i=0; i<4; i++)
        encrypted[i] = decrypted[i];


    // ignore residue for now


    // last word
    // IV is really ib[n+1] = 0
    for(i=0; i<8; i++) ib[N+1][i] = 0;

    for (j=N; j>0; j--)
    {
        // xor db x ib[n][j]
        for(i=0; i<8; i++)  block[i] = decrypted[4+(j*8)-8+i] ^ ib[j+1][i];
        block_encypher(kk, block, ib[j]);
    }

    // ib is now ib[1] which is in fact sb[1]
    // and sb[1] is the input to the stream cypher
    // so now we can run the stream cypher to generate all the sb[]


    // 1st 8 bytes of initialisation - ib[1] has popped out of the last block cypher ...
    stream_cypher(1, ck, ib[1], stream);

    // sb[1] is just ib[1];
    for(i=0; i<8; i++)  encrypted[4+0+i] = ib[1][i];

    for(j=2; j<(N+1); j++)
    {
        stream_cypher(0, ck, ib[j], stream);

        // xor ib x stream
        for(i=0; i<8; i++)  encrypted[4+8*(j-1)+i] = ib[j][i] ^ stream[i];
       
    }


    printf("\nEncrypted Ciphertext");
    for(i=0; i<188; i++)
    {
        if (!(i%0x10)) printf("\n%04x - ",i);
        printf("%02x ",encrypted[i]);
    }

   



	printf("\nPlease enter filename for output:\n");
		scanf("%s",&f_out_name);

	fp3 = fopen(f_out_name,"w");
	
	for(in_dex=0; in_dex<0x100; in_dex++)
	{
		if(h[0]=='0' && h[1]=='x')
		{
			fprintf(fp3, "0x%x ",encrypted[in_dex]);
		}
		else
		{
			fprintf(fp3, "%c", encrypted[in_dex]);
		}

	}

	fclose(fp3);







}

main()
{


    while(1){


    FILE *fp1;	//fp for key
    FILE *fp2;	//fp for plaintext
    FILE *fp5;	//fp for file1
    FILE *fp6;	//fp for file2
    //FILE *fp3;

    //char testend;

    char eord;		//to choose encryotion or decryption
    int in_dex;		//array index

   
    unsigned int key_in[8];
    unsigned char key_char[0x08];		//key array
    unsigned char plaintext_char[0x100];	//plaintext arrray
    unsigned char encrypted_char[0x100];	//plaintext arrray
    unsigned char f_key_name[20];		//file name for key
    unsigned char f_name[20];			//file name for plaintext
    unsigned char f_out_name[20];
    unsigned char ff1_name[20];
    unsigned char ff2_name[20];
    unsigned char hexorchartext[2];
    unsigned char hexorcharkey[2];
    unsigned char file1[0x100];	//plaintext arrray
    unsigned char file2[0x100];	//plaintext arrray


    printf("\nWould you like to encrypt, decrypt or compare?   e/d/c\n");


    scanf("%c",&eord);		//takes in user input of e or d


    if (eord=='e'){

	printf("\nPlease enter filename for plaintext:\n");
		scanf("%s",&f_name);



	while( (fp2 = fopen(f_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&f_name);
	}

	fscanf(fp2, "%c%c", &hexorchartext[0], &hexorchartext[1]);

	rewind(fp2);

	if(hexorchartext[0]=='0' && hexorchartext[1]=='x')
	{

		for(in_dex=0;in_dex<0x100;in_dex++){
		
			fscanf(fp2, "%x%*c", &plaintext_char[in_dex]);
			
		}
	}

	else
	{
		for(in_dex=0;!feof(fp2);in_dex++){
		
			fscanf(fp2, "%c", &plaintext_char[in_dex]);
			printf("%c", plaintext_char[in_dex]);
		}
	}

	fclose(fp2);









	printf("\nPlease enter filename for key:\n");
		scanf("%s",&f_key_name);



	while( (fp1 = fopen(f_key_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&f_key_name);
	}

	fscanf(fp1, "%c%c", &hexorcharkey[0], &hexorcharkey[1]);

	rewind(fp1);

	if(hexorcharkey[0]=='0' && hexorcharkey[1]=='x')
	{

		for(in_dex=0;in_dex<0x08;in_dex++){
		
			fscanf(fp1, "%x%*c", &key_char[in_dex]);
		}
	}

	else
	{
		for(in_dex=0;in_dex<0x08;in_dex++){
		
			fscanf(fp1, "%c", &key_char[in_dex]);
		}
	}

	fclose(fp1);


	encrypt(key_char, plaintext_char, hexorchartext);




    	

    }


    else if (eord=='d'){




	printf("\nPlease enter filename for ciphertext:\n");
		scanf("%s",&f_name);



	while( (fp2 = fopen(f_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&f_name);
	}

	fscanf(fp2, "%c%c", &hexorchartext[0], &hexorchartext[1]);

	rewind(fp2);

	if(hexorchartext[0]=='0' && hexorchartext[1]=='x')
	{

		for(in_dex=0;in_dex<0x100;in_dex++){
		
			fscanf(fp2, "%x%*c", &plaintext_char[in_dex]);
		}
	}

	else
	{
		for(in_dex=0;!feof(fp2);in_dex++){
		
			fscanf(fp2, "%c", &plaintext_char[in_dex]);
		}
	}

	fclose(fp2);









	printf("\nPlease enter filename for key:\n");
		scanf("%s",&f_key_name);



	while( (fp1 = fopen(f_key_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&f_key_name);
	}

	fscanf(fp1, "%c%c", &hexorcharkey[0], &hexorcharkey[1]);

	rewind(fp1);

	if(hexorcharkey[0]=='0' && hexorcharkey[1]=='x')
	{

		for(in_dex=0;in_dex<0x08;in_dex++){
		
			fscanf(fp1, "%x%*c", &key_char[in_dex]);
		}
	}

	else
	{
		for(in_dex=0;in_dex<0x08;in_dex++){
		
			fscanf(fp1, "%c", &key_char[in_dex]);
		}
	}

	fclose(fp1);


	decrypt(key_char, plaintext_char, hexorchartext);


    }


    else if (eord=='c'){

	
	printf("\nPlease enter filename:\n");
		scanf("%s",&ff1_name);

	while( (fp5 = fopen(ff1_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&ff1_name);
	}

	fscanf(fp5, "%c%c", &hexorchartext[0], &hexorchartext[1]);

	rewind(fp5);




	printf("\nPlease enter filename:\n");
		scanf("%s",&ff2_name);

	while( (fp6 = fopen(ff2_name,"r")) == NULL){
		printf("Re-enter a valid filename:\n");
		scanf("%s",&ff2_name);
	}




	
	if(hexorchartext[0]=='0' && hexorchartext[1]=='x')
	{
		//printf("we are in HEX mode\n");

		for(in_dex=0;in_dex<0x100;in_dex++){
		
			fscanf(fp5, "%x%*c", &file1[in_dex]);
			fscanf(fp6, "%x%*c", &file2[in_dex]);

			//printf("%x   %x\n",file1[in_dex],file2[in_dex]);



			if(!feof(fp5) && !feof(fp6) && file1[in_dex] != file2[in_dex])
			{
				in_dex=0x105;
			}
		}
	}

	else
	{
		//printf("we are in CHAR mode\n");

		for(in_dex=0;in_dex<0x100;in_dex++){
		
			fscanf(fp5, "%c", &file1[in_dex]);
			fscanf(fp6, "%c", &file2[in_dex]);

			//printf("%c   %c\n",file1[in_dex],file2[in_dex]);


			if(!feof(fp5) && !feof(fp6) && file1[in_dex] != file2[in_dex])
			{
				in_dex=0x105;
			}
		}
	}


	//printf("%x",in_dex);
	if(in_dex>0x102)
		printf("\nFAILED!!");
	else
		printf("\nPASSED!!");
	
	
    }

    else
    	printf("\n");

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -