📄 ctsecurityimei.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "math2.h"
#include "intconv.h"
#define MAX_PRIME_NUMBERS 150
const unsigned long PrimeNumbers[MAX_PRIME_NUMBERS] =
{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863
};
unsigned long GCD( unsigned long , unsigned long );
unsigned long tofindE( unsigned long , unsigned long , unsigned long );
unsigned long Round( double j1 );
unsigned long extend( unsigned long , unsigned long );
unsigned long Decrypt( unsigned long , unsigned long , unsigned long );
unsigned long Encrypt( unsigned long , unsigned long , unsigned long );
void Encrypt_Number( char *, char *);
int instr( char *, char , int );
void STRCPYEN( char *, char *, int, int, int );
void Get_Number(char *, char *);
int Check_Code( unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long );
void Get_Number( char *Enc_Num, char *Ak)
{
unsigned long My_P, My_Q;
unsigned long P1;
unsigned long Q1;
unsigned long PI;
unsigned long E1;
unsigned long N1;
unsigned long M1;
unsigned long D1;
char temp[60], i=0 ;
My_P = (unsigned long)rand() % ( MAX_PRIME_NUMBERS - 1 );
do
{
do
{
My_Q = (unsigned long)rand() % ( MAX_PRIME_NUMBERS - 1 );
} while( My_P == My_Q );
P1 = PrimeNumbers[My_P];
Q1 = PrimeNumbers[My_Q];
PI = ( P1 - 1 ) * ( Q1 - 1 );
E1 = tofindE( PI, P1, Q1 );
E1 = 1687;
D1 = extend( E1, PI );
N1 = P1 * Q1;
} while( N1 > 100000 );
do
{
M1 = (unsigned long)rand()%(N1 - 1);
} while( M1 >= N1 || M1 <= 0 );
i = sprintf( Enc_Num, "%lu%c%lu%c%lu", N1, '-', M1, '-', E1 );
Encrypt_Number( Enc_Num, temp);
strcpy( Ak, temp );
}
unsigned long tofindE( unsigned long PI, unsigned long P1, unsigned long Q1 )
{
double rm;
unsigned long great;
unsigned long se;
great = 0;
se = 0;
rm = 1682.0000000000;
se = Round( rm );
while( great != 1 )
{
se++;
great = GCD( se, PI );
}
return( se );
}
unsigned long GCD( unsigned long e1, unsigned long PI )
{
unsigned long a1, great;
a1 = 0;
great = 0;
if ( e1 > PI )
{
while( ( e1 % PI ) != 0 )
{
a1 = e1 % PI;
e1 = PI;
PI = a1;
}
great = PI;
}
else
{
while( ( PI % e1 ) != 0 )
{
a1 = PI % e1;
PI = e1;
e1 = a1;
}
great = e1;
}
return great;
}
unsigned long Round( double j1 )
{
unsigned long f1;
f1 = (unsigned long)j1;
if ( (j1 - f1) >= 0.5 ) f1++;
return f1;
}
unsigned long extend( unsigned long E1, unsigned long PI )
{
double t1T, t2T, t3T, u1T, u2T, u3T, v1T, v2T, v3T, qT, zT;
double uuT, vvT, inverse;
u1T = 1;
u2T = 0;
u3T = (double) PI;
v1T = 0;
v2T = 1;
v3T = (double) E1;
qT = 0;
zT = 0;
uuT = 0;
vvT = 0;
while( v3T != 0 )
{
qT = floor( u3T / v3T );
t1T = u1T - qT * v1T;
t2T = u2T - qT * v2T;
t3T = u3T - qT * v3T;
u1T = v1T;
u2T = v2T;
u3T = v3T;
v1T = t1T;
v2T = t2T;
v3T = t3T;
zT = 1;
}
uuT = u1T;
vvT = u2T;
if ( vvT < 0 )
{
inverse = vvT + PI;
}
else
{
inverse = vvT;
}
return( (unsigned long)inverse );
}
void Encrypt_Number( char *Ask_String, char *Enc_Str)
{
unsigned long pos, pos_1;
unsigned long log1, log2;
unsigned long N1;
unsigned long M1;
unsigned long E1;
unsigned long L1;
char temp[100];
pos = pos_1 = log1 = log2 = 0;
pos = instr( Ask_String, '-', 0 );
STRCPYEN( temp, Ask_String, 0, 0, pos );
temp[ pos ] = '\0';
N1 = (unsigned long)atol( temp );
pos_1 = pos + 1;
temp[ 0 ] = '\0';
pos = instr( Ask_String, '-', pos_1 );
STRCPYEN( temp, Ask_String, 0, pos_1, pos - pos_1 );
temp[ pos - pos_1 ] = '\0';
M1 = (unsigned long)atol( temp );
pos_1 = pos + 1;
temp[ 0 ] = '\0';
STRCPYEN( temp, Ask_String, 0, pos_1, strlen( Ask_String ) - pos);
temp[strlen( Ask_String ) - pos ] = '\0';
E1 = (unsigned long)atol( temp );
L1 = Encrypt( M1, E1, N1 );
log1 = Round(fmod(((double)N1 *(double) 98789 ), log( N1 )));
log2 = Round((((double)M1 * (double)N1 ) / (double)N1 ) / log( M1 ));
sprintf( temp, "%lu%c%lu%c%lu", L1+89, '-', log1+89, '-', log2+89 );
strcpy( Enc_Str, temp );
}
int instr( char *str, char find, int pos )
{
int ret = 0;
str = str + pos;
ret = pos;
while( *str )
{
if ( *str == find ) return ret;
ret++;
str++;
}
return 0;
}
void STRCPYEN( char *dest, char *source, int dest_start, int source_start, int count )
{
int i1;
for( i1 = 0; i1 < count; i1 ++ ) dest[ i1 + dest_start ] = source[ i1 + source_start ];
}
unsigned long Encrypt( unsigned long M1, unsigned long E1, unsigned long N1 )
{
unsigned long F1, C1, I1;
unsigned long max;
max = (unsigned long) ((double)E1 / 2.0);
F1 = ( M1 * M1 ) % N1;
if ( ( E1 % 2 ) == 0 )
{
C1 = 1;
for( I1 = 1; I1 <= max; I1++ )
{
C1 = (unsigned long)fmod(((double)F1 * (double)C1 ), N1);
}
}
else
{
C1 = M1;
for( I1 = 1; I1 <= max; I1++ )
{
C1 = (unsigned long)fmod(( (double)F1 * (double)C1 ), N1);
}
}
return( C1 );
}
/* ----------------------------------------------------------------------------
Buffer to store encrypted Data
--------------------------------------------------------------------------*/
static byte msl_encrypted_data_buffer[12];
/* ---------------------------------------------------------------------------
Buffer to store Encryption Key
-------------------------------------------------------------------------*/
static byte msl_encryption_key_buffer[6];
#ifdef _SAMSUNG_BREW_MSL
#define NV_MSL_CODE_SIZE 6 /* 6 digit msl code */
#define NV_MSL_ADDR_SIZE 10 /* 10 digit msl address */
/*==========================================================================
FUNCTION MSL_DECRYPT_DATA
DESCRIPTION
This function is called to decrypt the data that is currently encrypted.
DEPENDENCIES
None
RETURN VALUE
===========================================================================*/
byte * msl_decrypt_data(
byte * encrypted_data_p
)
{
int32 index = 0;
byte raw_data_p[(NV_MSL_CODE_SIZE)];
/* --------------------------------------------------------------------------
Copy the Cipher Key to the cipher_key_buffer
------------------------------------------------------------------------*/
memcpy(msl_encryption_key_buffer,
encrypted_data_p+(NV_MSL_CODE_SIZE),
int32touint32(NV_MSL_CODE_SIZE));
while ( index < NV_MSL_CODE_SIZE )
{
raw_data_p[index] = msl_encryption_key_buffer[index] ^ encrypted_data_p[index];
index++;
}
return raw_data_p;
}
/*===========================================================================
FUNCTION MSL_ENCRYPT_DATA
DESCRIPTION
This function is called to encrypt the data that is currently encrypted.
DEPENDENCIES
None
RETURN VALUE
===========================================================================*/
byte * msl_encrypt_data (
byte * raw_data_buffer_p
)
{
int32 index = 0x00;
byte encrypted_data_buffer_p[(NV_MSL_CODE_SIZE*2)];
while ( index < NV_MSL_CODE_SIZE )
{
msl_encryption_key_buffer[index] = (uint8)rand()%256;
encrypted_data_buffer_p[index] = msl_encryption_key_buffer[index] ^ raw_data_buffer_p[index];
index++;
}
/* Now Paste the Cipher Key to the end of the Data */
memcpy(&encrypted_data_buffer_p[index],msl_encryption_key_buffer,int32touint32(index));
return encrypted_data_buffer_p;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -