DES/des3 加密程序
1 #ifndef POLARSSL_DES_H
#define POLARSSL_DES_H #define DES_ENCRYPT 1
#define DES_DECRYPT 0 #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0C00 /**
* \brief DES context structure
*/
typedef struct
{
int mode; /*!< encrypt/decrypt */
unsigned long sk[]; /*!< DES subkeys */
}
des_context; /**
* \brief Triple-DES context structure
*/
typedef struct
{
int mode; /*!< encrypt/decrypt */
unsigned long sk[]; /*!< 3DES subkeys */
}
des3_context; #ifdef __cplusplus
extern "C" {
#endif /**
* \brief DES key schedule (56-bit, encryption)
*
* \param ctx DES context to be initialized
* \param key 8-byte secret key
*/
void des_setkey_enc( des_context *ctx, const unsigned char key[] ); /**
* \brief DES key schedule (56-bit, decryption)
*
* \param ctx DES context to be initialized
* \param key 8-byte secret key
*/
void des_setkey_dec( des_context *ctx, const unsigned char key[] ); /**
* \brief Triple-DES key schedule (112-bit, encryption)
*
* \param ctx 3DES context to be initialized
* \param key 16-byte secret key
*/
void des3_set2key_enc( des3_context *ctx, const unsigned char key[] ); /**
* \brief Triple-DES key schedule (112-bit, decryption)
*
* \param ctx 3DES context to be initialized
* \param key 16-byte secret key
*/
void des3_set2key_dec( des3_context *ctx, const unsigned char key[] ); /**
* \brief Triple-DES key schedule (168-bit, encryption)
*
* \param ctx 3DES context to be initialized
* \param key 24-byte secret key
*/
void des3_set3key_enc( des3_context *ctx, const unsigned char key[] ); /**
* \brief Triple-DES key schedule (168-bit, decryption)
*
* \param ctx 3DES context to be initialized
* \param key 24-byte secret key
*/
void des3_set3key_dec( des3_context *ctx, const unsigned char key[] ); /**
* \brief DES-ECB block encryption/decryption
*
* \param ctx DES context
* \param input 64-bit input block
* \param output 64-bit output block
*
* \return 0 if successful
*/
int des_crypt_ecb( des_context *ctx,
const unsigned char input[],
unsigned char output[] ); /**
* \brief DES-CBC buffer encryption/decryption
*
* \param ctx DES context
* \param mode DES_ENCRYPT or DES_DECRYPT
* \param length length of the input data
* \param iv initialization vector (updated after use)
* \param input buffer holding the input data
* \param output buffer holding the output data
*/
int des_crypt_cbc( des_context *ctx,
int mode,
int length,
unsigned char iv[],
const unsigned char *input,
unsigned char *output ); /**
* \brief 3DES-ECB block encryption/decryption
*
* \param ctx 3DES context
* \param input 64-bit input block
* \param output 64-bit output block
*
* \return 0 if successful
*/
int des3_crypt_ecb( des3_context *ctx,
const unsigned char input[],
unsigned char output[] ); /**
* \brief 3DES-CBC buffer encryption/decryption
*
* \param ctx 3DES context
* \param mode DES_ENCRYPT or DES_DECRYPT
* \param length length of the input data
* \param iv initialization vector (updated after use)
* \param input buffer holding the input data
* \param output buffer holding the output data
*
* \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
*/
int des3_crypt_cbc( des3_context *ctx,
int mode,
int length,
unsigned char iv[],
const unsigned char *input,
unsigned char *output ); /*
* \brief Checkup routine
*
* \return 0 if successful, or 1 if the test failed
*/
int des_self_test( int verbose ); #ifdef __cplusplus
}
#endif #endif /* des.h */
#ifndef POLARSSL_CONFIG_H
#define POLARSSL_CONFIG_H #ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif /*
* Uncomment if native integers are 8-bit wide.
*
#define POLARSSL_HAVE_INT8
*/ /*
* Uncomment if native integers are 16-bit wide.
*
#define POLARSSL_HAVE_INT16
*/ /*
* Uncomment if the compiler supports long long.
*
#define POLARSSL_HAVE_LONGLONG
*/ /*
* Uncomment to enable the use of assembly code.
*
* Requires support for asm() in compiler.
*
* Used in:
* library/timing.c
* library/padlock.c
* include/polarssl/bn_mul.h
*
*/
#define POLARSSL_HAVE_ASM /*
* Uncomment if the CPU supports SSE2 (IA-32 specific).
*
#define POLARSSL_HAVE_SSE2
*/ /*
* Enable all SSL/TLS debugging messages.
*/
#define POLARSSL_DEBUG_MSG /*
* Enable the checkup functions (*_self_test).
*/
#define POLARSSL_SELF_TEST /*
* Enable run-time version information functions
*/
#define POLARSSL_VERSION_C /*
* Enable the prime-number generation code.
*/
#define POLARSSL_GENPRIME /*
* Uncomment this macro to store the AES tables in ROM.
*
#define POLARSSL_AES_ROM_TABLES
*/ /*
* Module: library/aes.c
* Caller: library/ssl_tls.c
*
* This module enables the following ciphersuites:
* SSL_RSA_AES_128_SHA
* SSL_RSA_AES_256_SHA
* SSL_EDH_RSA_AES_256_SHA
*/
#define POLARSSL_AES_C /*
* Module: library/arc4.c
* Caller: library/ssl_tls.c
*
* This module enables the following ciphersuites:
* SSL_RSA_RC4_128_MD5
* SSL_RSA_RC4_128_SHA
*/
#define POLARSSL_ARC4_C /*
* Module: library/base64.c
* Caller: library/x509parse.c
*
* This module is required for X.509 support.
*/
#define POLARSSL_BASE64_C /*
* Module: library/bignum.c
* Caller: library/dhm.c
* library/rsa.c
* library/ssl_tls.c
* library/x509parse.c
*
* This module is required for RSA and DHM support.
*/
#define POLARSSL_BIGNUM_C /*
* Module: library/camellia.c
* Caller: library/ssl_tls.c
*
* This module enabled the following cipher suites:
* SSL_RSA_CAMELLIA_128_SHA
* SSL_RSA_CAMELLIA_256_SHA
* SSL_EDH_RSA_CAMELLIA_256_SHA
*/
#define POLARSSL_CAMELLIA_C /*
* Module: library/certs.c
* Caller:
*
* This module is used for testing (ssl_client/server).
*/
#define POLARSSL_CERTS_C /*
* Module: library/debug.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
* library/ssl_tls.c
*
* This module provides debugging functions.
*/
#define POLARSSL_DEBUG_C /*
* Module: library/des.c
* Caller: library/ssl_tls.c
*
* This module enables the following ciphersuites:
* SSL_RSA_DES_168_SHA
* SSL_EDH_RSA_DES_168_SHA
*/
#define POLARSSL_DES_C /*
* Module: library/dhm.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
*
* This module enables the following ciphersuites:
* SSL_EDH_RSA_DES_168_SHA
* SSL_EDH_RSA_AES_256_SHA
* SSL_EDH_RSA_CAMELLIA_256_SHA
*/
#define POLARSSL_DHM_C /*
* Module: library/havege.c
* Caller:
*
* This module enables the HAVEGE random number generator.
*/
#define POLARSSL_HAVEGE_C /*
* Module: library/md2.c
* Caller: library/x509parse.c
*
* Uncomment to enable support for (rare) MD2-signed X.509 certs.
*
#define POLARSSL_MD2_C
*/ /*
* Module: library/md4.c
* Caller: library/x509parse.c
*
* Uncomment to enable support for (rare) MD4-signed X.509 certs.
*
#define POLARSSL_MD4_C
*/ /*
* Module: library/md5.c
* Caller: library/ssl_tls.c
* library/x509parse.c
*
* This module is required for SSL/TLS and X.509.
*/
#define POLARSSL_MD5_C /*
* Module: library/net.c
* Caller:
*
* This module provides TCP/IP networking routines.
*/
#define POLARSSL_NET_C /*
* Module: library/padlock.c
* Caller: library/aes.c
*
* This modules adds support for the VIA PadLock on x86.
*/
#define POLARSSL_PADLOCK_C /*
* Module: library/rsa.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
* library/ssl_tls.c
* library/x509.c
*
* This module is required for SSL/TLS and MD5-signed certificates.
*/
#define POLARSSL_RSA_C /*
* Module: library/sha1.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
* library/ssl_tls.c
* library/x509parse.c
*
* This module is required for SSL/TLS and SHA1-signed certificates.
*/
#define POLARSSL_SHA1_C /*
* Module: library/sha2.c
* Caller:
*
* This module adds support for SHA-224 and SHA-256.
*/
#define POLARSSL_SHA2_C /*
* Module: library/sha4.c
* Caller:
*
* This module adds support for SHA-384 and SHA-512.
*/
#define POLARSSL_SHA4_C /*
* Module: library/ssl_cli.c
* Caller:
*
* This module is required for SSL/TLS client support.
*/
#define POLARSSL_SSL_CLI_C /*
* Module: library/ssl_srv.c
* Caller:
*
* This module is required for SSL/TLS server support.
*/
#define POLARSSL_SSL_SRV_C /*
* Module: library/ssl_tls.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
*
* This module is required for SSL/TLS.
*/
#define POLARSSL_SSL_TLS_C /*
* Module: library/timing.c
* Caller: library/havege.c
*
* This module is used by the HAVEGE random number generator.
*/
#define POLARSSL_TIMING_C /*
* Module: library/x509parse.c
* Caller: library/ssl_cli.c
* library/ssl_srv.c
* library/ssl_tls.c
*
* This module is required for X.509 certificate parsing.
*/
#define POLARSSL_X509_PARSE_C /*
* Module: library/x509_write.c
* Caller:
*
* This module is required for X.509 certificate writing.
*/
#define POLARSSL_X509_WRITE_C /*
* Module: library/xtea.c
* Caller:
*/
#define POLARSSL_XTEA_C #endif /* config.h */ #ifndef POLARSSL_TIMING_H
#define POLARSSL_TIMING_H struct hr_time
{
unsigned char opaque[];
}; #ifdef __cplusplus
extern "C" {
#endif extern int alarmed;
unsigned long hardclock( void ); unsigned long get_timer( struct hr_time *val, int reset ); void set_alarm( int seconds ); void m_sleep( int milliseconds ); #ifdef __cplusplus
}
#endif #endif #define BUFSIZE 32768 #if defined(POLARSSL_TIMING_C) #if defined(_WIN32) #include <windows.h>
#include <winbase.h> struct _hr_time
{
LARGE_INTEGER start;
}; #else #include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <time.h> struct _hr_time
{
struct timeval start;
}; #endif #if defined(POLARSSL_HAVE_ASM) && \
(defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) unsigned long hardclock( void )
{
unsigned long tsc;
__asm rdtsc
__asm mov [tsc], eax
return( tsc );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) unsigned long hardclock( void )
{
unsigned long tsc;
asm( "rdtsc" : "=a" (tsc) );
return( tsc );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
(defined(__amd64__) || defined(__x86_64__)) unsigned long hardclock( void )
{
unsigned long lo, hi;
asm( "rdtsc" : "=a" (lo), "=d" (hi) );
return( lo | (hi << ) );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
(defined(__powerpc__) || defined(__ppc__)) unsigned long hardclock( void )
{
unsigned long tbl, tbu0, tbu1; do
{
asm( "mftbu %0" : "=r" (tbu0) );
asm( "mftb %0" : "=r" (tbl ) );
asm( "mftbu %0" : "=r" (tbu1) );
}
while( tbu0 != tbu1 ); return( tbl );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__) unsigned long hardclock( void )
{
unsigned long tick;
asm( ".byte 0x83, 0x41, 0x00, 0x00" );
asm( "mov %%g1, %0" : "=r" (tick) );
return( tick );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__) unsigned long hardclock( void )
{
unsigned long cc;
asm( "rpcc %0" : "=r" (cc) );
return( cc & 0xFFFFFFFF );
} #else
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__) unsigned long hardclock( void )
{
unsigned long itc;
asm( "mov %0 = ar.itc" : "=r" (itc) );
return( itc );
} #else static int hardclock_init = ;
static struct timeval tv_init; unsigned long hardclock( void )
{
struct timeval tv_cur; if( hardclock_init == )
{
gettimeofday( &tv_init, NULL );
hardclock_init = ;
} gettimeofday( &tv_cur, NULL );
return( ( tv_cur.tv_sec - tv_init.tv_sec ) *
+ ( tv_cur.tv_usec - tv_init.tv_usec ) );
} #endif /* generic */
#endif /* IA-64 */
#endif /* Alpha */
#endif /* SPARC8 */
#endif /* PowerPC */
#endif /* AMD64 */
#endif /* i586+ */ int alarmed = ; #if defined(_WIN32) unsigned long get_timer( struct hr_time *val, int reset )
{
unsigned long delta;
LARGE_INTEGER offset, hfreq;
struct _hr_time *t = (struct _hr_time *) val; QueryPerformanceCounter( &offset );
QueryPerformanceFrequency( &hfreq ); delta = (unsigned long)( ( *
( offset.QuadPart - t->start.QuadPart ) ) /
hfreq.QuadPart ); if( reset )
QueryPerformanceCounter( &t->start ); return( delta );
} DWORD WINAPI TimerProc( LPVOID uElapse )
{
Sleep( (DWORD) uElapse );
alarmed = ;
return( TRUE );
} void set_alarm( int seconds )
{
DWORD ThreadId; alarmed = ;
CloseHandle( CreateThread( NULL, , TimerProc,
(LPVOID) ( seconds * ), , &ThreadId ) );
} void m_sleep( int milliseconds )
{
Sleep( milliseconds );
} #else unsigned long get_timer( struct hr_time *val, int reset )
{
unsigned long delta;
struct timeval offset;
struct _hr_time *t = (struct _hr_time *) val; gettimeofday( &offset, NULL ); delta = ( offset.tv_sec - t->start.tv_sec ) *
+ ( offset.tv_usec - t->start.tv_usec ) / ; if( reset )
{
t->start.tv_sec = offset.tv_sec;
t->start.tv_usec = offset.tv_usec;
} return( delta );
} static void sighandler( int signum )
{
alarmed = ;
signal( signum, sighandler );
} void set_alarm( int seconds )
{
alarmed = ;
signal( SIGALRM, sighandler );
alarm( seconds );
} void m_sleep( int milliseconds )
{
struct timeval tv; tv.tv_sec = milliseconds / ;
tv.tv_usec = milliseconds * ; select( , NULL, NULL, NULL, &tv );
} #endif #endif #if defined(POLARSSL_DES_C) #include <string.h> /*
* 32-bit integer manipulation macros (big endian)
*/
#ifndef GET_ULONG_BE
#define GET_ULONG_BE(n,b,i) \
{ \
(n) = ( (unsigned long) (b)[(i) ] << ) \
| ( (unsigned long) (b)[(i) + ] << ) \
| ( (unsigned long) (b)[(i) + ] << ) \
| ( (unsigned long) (b)[(i) + ] ); \
}
#endif #ifndef PUT_ULONG_BE
#define PUT_ULONG_BE(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> ); \
(b)[(i) + ] = (unsigned char) ( (n) >> ); \
(b)[(i) + ] = (unsigned char) ( (n) >> ); \
(b)[(i) + ] = (unsigned char) ( (n) ); \
}
#endif /*
* Expanded DES S-boxes
*/
static const unsigned long SB1[] =
{
0x01010400, 0x00000000, 0x00010000, 0x01010404,
0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
0x00000404, 0x01000400, 0x01000400, 0x00010400,
0x00010400, 0x01010000, 0x01010000, 0x01000404,
0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000,
0x00010000, 0x01010404, 0x00000004, 0x01010000,
0x01010400, 0x01000000, 0x01000000, 0x00000400,
0x01010004, 0x00010000, 0x00010400, 0x01000004,
0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
0x01000004, 0x00000404, 0x00010404, 0x01010400,
0x00000404, 0x01000400, 0x01000400, 0x00000000,
0x00010004, 0x00010400, 0x00000000, 0x01010004
}; static const unsigned long SB2[] =
{
0x80108020, 0x80008000, 0x00008000, 0x00108020,
0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
0x00108000, 0x00100020, 0x80008020, 0x00000000,
0x80000000, 0x00008000, 0x00108020, 0x80100000,
0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020,
0x00000000, 0x00108020, 0x80100020, 0x00100000,
0x80008020, 0x80100000, 0x80108000, 0x00008000,
0x80100000, 0x80008000, 0x00000020, 0x80108020,
0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
0x00100020, 0x80008020, 0x80000020, 0x00100020,
0x00108000, 0x00000000, 0x80008000, 0x00008020,
0x80000000, 0x80100020, 0x80108020, 0x00108000
}; static const unsigned long SB3[] =
{
0x00000208, 0x08020200, 0x00000000, 0x08020008,
0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
0x08000000, 0x00000008, 0x08020200, 0x00000200,
0x00020200, 0x08020000, 0x08020008, 0x00020208,
0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000,
0x08020200, 0x08000000, 0x00020008, 0x00000208,
0x00020000, 0x08020200, 0x08000200, 0x00000000,
0x00000200, 0x00020008, 0x08020208, 0x08000200,
0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
0x00000008, 0x00020208, 0x00020200, 0x08000008,
0x08020000, 0x08000208, 0x00000208, 0x08020000,
0x00020208, 0x00000008, 0x08020008, 0x00020200
}; static const unsigned long SB4[] =
{
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
0x00000001, 0x00002000, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002001, 0x00002080,
0x00800081, 0x00000001, 0x00002080, 0x00800080,
0x00002000, 0x00802080, 0x00802081, 0x00000081,
0x00800080, 0x00800001, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00000000, 0x00802000,
0x00002080, 0x00800080, 0x00800081, 0x00000001,
0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
0x00800001, 0x00002001, 0x00802080, 0x00800081,
0x00002001, 0x00002080, 0x00800000, 0x00802001,
0x00000080, 0x00800000, 0x00002000, 0x00802080
}; static const unsigned long SB5[] =
{
0x00000100, 0x02080100, 0x02080000, 0x42000100,
0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
0x02000000, 0x40080000, 0x40080000, 0x00000000,
0x40000100, 0x42080100, 0x42080100, 0x02000100,
0x42080000, 0x40000100, 0x00000000, 0x42000000,
0x02080100, 0x02000000, 0x42000000, 0x00080100,
0x00080000, 0x42000100, 0x00000100, 0x02000000,
0x40000000, 0x02080000, 0x42000100, 0x40080100,
0x02000100, 0x40000000, 0x42080000, 0x02080100,
0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
0x02080000, 0x00000000, 0x40080000, 0x42000000,
0x00080100, 0x02000100, 0x40000100, 0x00080000,
0x00000000, 0x40080000, 0x02080100, 0x40000100
}; static const unsigned long SB6[] =
{
0x20000010, 0x20400000, 0x00004000, 0x20404010,
0x20400000, 0x00000010, 0x20404010, 0x00400000,
0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
0x00000000, 0x00400010, 0x20004010, 0x00004000,
0x00404000, 0x20004010, 0x00000010, 0x20400010,
0x20400010, 0x00000000, 0x00404010, 0x20404000,
0x00004010, 0x00404000, 0x20404000, 0x20000000,
0x20004000, 0x00000010, 0x20400010, 0x00404000,
0x20404010, 0x00400000, 0x00004010, 0x20000010,
0x00400000, 0x20004000, 0x20000000, 0x00004010,
0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
0x00000010, 0x00004000, 0x20400000, 0x00404010,
0x00004000, 0x00400010, 0x20004010, 0x00000000,
0x20404000, 0x20000000, 0x00400010, 0x20004010
}; static const unsigned long SB7[] =
{
0x00200000, 0x04200002, 0x04000802, 0x00000000,
0x00000800, 0x04000802, 0x00200802, 0x04200800,
0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
0x04000800, 0x00200802, 0x00200002, 0x04000800,
0x04000002, 0x04200000, 0x04200800, 0x00200002,
0x04200000, 0x00000800, 0x00000802, 0x04200802,
0x00200800, 0x00000002, 0x04000000, 0x00200800,
0x04000000, 0x00200800, 0x00200000, 0x04000802,
0x04000802, 0x04200002, 0x04200002, 0x00000002,
0x00200002, 0x04000000, 0x04000800, 0x00200000,
0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
0x00200800, 0x00000000, 0x00000002, 0x04200802,
0x00000000, 0x00200802, 0x04200000, 0x00000800,
0x04000002, 0x04000800, 0x00000800, 0x00200002
}; static const unsigned long SB8[] =
{
0x10001040, 0x00001000, 0x00040000, 0x10041040,
0x10000000, 0x10001040, 0x00000040, 0x10000000,
0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
0x10040000, 0x10000040, 0x10001000, 0x00001040,
0x00041000, 0x00040040, 0x10040040, 0x10041000,
0x00001040, 0x00000000, 0x00000000, 0x10040040,
0x10000040, 0x10001000, 0x00041040, 0x00040000,
0x00041040, 0x00040000, 0x10041000, 0x00001000,
0x00000040, 0x10040040, 0x00001000, 0x00041040,
0x10001000, 0x00000040, 0x10000040, 0x10040000,
0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
0x10040000, 0x10001000, 0x10001040, 0x00000000,
0x10041040, 0x00041000, 0x00041000, 0x00001040,
0x00001040, 0x00040040, 0x10000000, 0x10041000
}; /*
* PC1: left and right halves bit-swap
*/
static const unsigned long LHs[] =
{
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
}; static const unsigned long RHs[] =
{
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100,
0x00000001, 0x01000001, 0x00010001, 0x01010001,
0x00000101, 0x01000101, 0x00010101, 0x01010101,
}; /*
* Initial Permutation macro
*/
#define DES_IP(X,Y) \
{ \
T = ((X >> ) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << ); \
T = ((X >> ) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << ); \
T = ((Y >> ) ^ X) & 0x33333333; X ^= T; Y ^= (T << ); \
T = ((Y >> ) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << ); \
Y = ((Y << ) | (Y >> )) & 0xFFFFFFFF; \
T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
X = ((X << ) | (X >> )) & 0xFFFFFFFF; \
} /*
* Final Permutation macro
*/
#define DES_FP(X,Y) \
{ \
X = ((X << ) | (X >> )) & 0xFFFFFFFF; \
T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
Y = ((Y << ) | (Y >> )) & 0xFFFFFFFF; \
T = ((Y >> ) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << ); \
T = ((Y >> ) ^ X) & 0x33333333; X ^= T; Y ^= (T << ); \
T = ((X >> ) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << ); \
T = ((X >> ) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << ); \
} /*
* DES round macro
*/
#define DES_ROUND(X,Y) \
{ \
T = *SK++ ^ X; \
Y ^= SB8[ (T ) & 0x3F ] ^ \
SB6[ (T >> ) & 0x3F ] ^ \
SB4[ (T >> ) & 0x3F ] ^ \
SB2[ (T >> ) & 0x3F ]; \
\
T = *SK++ ^ ((X << ) | (X >> )); \
Y ^= SB7[ (T ) & 0x3F ] ^ \
SB5[ (T >> ) & 0x3F ] ^ \
SB3[ (T >> ) & 0x3F ] ^ \
SB1[ (T >> ) & 0x3F ]; \
} #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; } static void des_setkey( unsigned long SK[], const unsigned char key[] )
{
int i;
unsigned long X, Y, T; GET_ULONG_BE( X, key, );
GET_ULONG_BE( Y, key, ); /*
* Permuted Choice 1
*/
T = ((Y >> ) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << );
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); X = (LHs[ (X ) & 0xF] << ) | (LHs[ (X >> ) & 0xF ] << )
| (LHs[ (X >> ) & 0xF] << ) | (LHs[ (X >> ) & 0xF ] )
| (LHs[ (X >> ) & 0xF] << ) | (LHs[ (X >> ) & 0xF ] << )
| (LHs[ (X >> ) & 0xF] << ) | (LHs[ (X >> ) & 0xF ] << ); Y = (RHs[ (Y >> ) & 0xF] << ) | (RHs[ (Y >> ) & 0xF ] << )
| (RHs[ (Y >> ) & 0xF] << ) | (RHs[ (Y >> ) & 0xF ] )
| (RHs[ (Y >> ) & 0xF] << ) | (RHs[ (Y >> ) & 0xF ] << )
| (RHs[ (Y >> ) & 0xF] << ) | (RHs[ (Y >> ) & 0xF ] << ); X &= 0x0FFFFFFF;
Y &= 0x0FFFFFFF; /*
* calculate subkeys
*/
for( i = ; i < ; i++ )
{
if( i < || i == || i == )
{
X = ((X << ) | (X >> )) & 0x0FFFFFFF;
Y = ((Y << ) | (Y >> )) & 0x0FFFFFFF;
}
else
{
X = ((X << ) | (X >> )) & 0x0FFFFFFF;
Y = ((Y << ) | (Y >> )) & 0x0FFFFFFF;
} *SK++ = ((X << ) & 0x24000000) | ((X << ) & 0x10000000)
| ((X << ) & 0x08000000) | ((X << ) & 0x02080000)
| ((X << ) & 0x01000000) | ((X << ) & 0x00200000)
| ((X >> ) & 0x00100000) | ((X << ) & 0x00040000)
| ((X << ) & 0x00020000) | ((X >> ) & 0x00010000)
| ((Y >> ) & 0x00002000) | ((Y >> ) & 0x00001000)
| ((Y << ) & 0x00000800) | ((Y >> ) & 0x00000400)
| ((Y >> ) & 0x00000200) | ((Y ) & 0x00000100)
| ((Y >> ) & 0x00000020) | ((Y >> ) & 0x00000010)
| ((Y >> ) & 0x00000008) | ((Y >> ) & 0x00000004)
| ((Y >> ) & 0x00000002) | ((Y >> ) & 0x00000001); *SK++ = ((X << ) & 0x20000000) | ((X << ) & 0x10000000)
| ((X << ) & 0x08000000) | ((X << ) & 0x04000000)
| ((X >> ) & 0x02000000) | ((X << ) & 0x01000000)
| ((X << ) & 0x00200000) | ((X << ) & 0x00100000)
| ((X << ) & 0x00080000) | ((X >> ) & 0x00040000)
| ((X << ) & 0x00020000) | ((X >> ) & 0x00010000)
| ((Y >> ) & 0x00002000) | ((Y << ) & 0x00001000)
| ((Y >> ) & 0x00000808) | ((Y >> ) & 0x00000400)
| ((Y ) & 0x00000200) | ((Y << ) & 0x00000100)
| ((Y >> ) & 0x00000020) | ((Y >> ) & 0x00000011)
| ((Y << ) & 0x00000004) | ((Y >> ) & 0x00000002);
}
} /*
* DES key schedule (56-bit, encryption)
*/
void des_setkey_enc( des_context *ctx, const unsigned char key[] )
{
des_setkey( ctx->sk, key );
} /*
* DES key schedule (56-bit, decryption)
*/
void des_setkey_dec( des_context *ctx, const unsigned char key[] )
{
int i; des_setkey( ctx->sk, key ); for( i = ; i < ; i += )
{
SWAP( ctx->sk[i ], ctx->sk[ - i] );
SWAP( ctx->sk[i + ], ctx->sk[ - i] );
}
} static void des3_set2key( unsigned long esk[],
unsigned long dsk[],
const unsigned char key[] )
{
int i; des_setkey( esk, key );
des_setkey( dsk + , key + ); for( i = ; i < ; i += )
{
dsk[i ] = esk[ - i];
dsk[i + ] = esk[ - i]; esk[i + ] = dsk[ - i];
esk[i + ] = dsk[ - i]; esk[i + ] = esk[i ];
esk[i + ] = esk[i + ]; dsk[i + ] = dsk[i ];
dsk[i + ] = dsk[i + ];
}
} /*
* Triple-DES key schedule (112-bit, encryption)
*/
void des3_set2key_enc( des3_context *ctx, const unsigned char key[] )
{
unsigned long sk[]; des3_set2key( ctx->sk, sk, key );
memset( sk, , sizeof( sk ) );
} /*
* Triple-DES key schedule (112-bit, decryption)
*/
void des3_set2key_dec( des3_context *ctx, const unsigned char key[] )
{
unsigned long sk[]; des3_set2key( sk, ctx->sk, key );
memset( sk, , sizeof( sk ) );
} static void des3_set3key( unsigned long esk[],
unsigned long dsk[],
const unsigned char key[] )
{
int i; des_setkey( esk, key );
des_setkey( dsk + , key + );
des_setkey( esk + , key + ); for( i = ; i < ; i += )
{
dsk[i ] = esk[ - i];
dsk[i + ] = esk[ - i]; esk[i + ] = dsk[ - i];
esk[i + ] = dsk[ - i]; dsk[i + ] = esk[ - i];
dsk[i + ] = esk[ - i];
}
} /*
* Triple-DES key schedule (168-bit, encryption)
*/
void des3_set3key_enc( des3_context *ctx, const unsigned char key[] )
{
unsigned long sk[]; des3_set3key( ctx->sk, sk, key );
memset( sk, , sizeof( sk ) );
} /*
* Triple-DES key schedule (168-bit, decryption)
*/
void des3_set3key_dec( des3_context *ctx, const unsigned char key[] )
{
unsigned long sk[]; des3_set3key( sk, ctx->sk, key );
memset( sk, , sizeof( sk ) );
} /*
* DES-ECB block encryption/decryption
*/
int des_crypt_ecb( des_context *ctx,
const unsigned char input[],
unsigned char output[] )
{
int i;
unsigned long X, Y, T, *SK; SK = ctx->sk; GET_ULONG_BE( X, input, );
GET_ULONG_BE( Y, input, ); DES_IP( X, Y ); for( i = ; i < ; i++ )
{
DES_ROUND( Y, X );
DES_ROUND( X, Y );
} DES_FP( Y, X ); PUT_ULONG_BE( Y, output, );
PUT_ULONG_BE( X, output, ); return( );
} /*
* DES-CBC buffer encryption/decryption
*/
int des_crypt_cbc( des_context *ctx,
int mode,
int length,
unsigned char iv[],
const unsigned char *input,
unsigned char *output )
{
int i;
unsigned char temp[]; if( length % )
return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH ); if( mode == DES_ENCRYPT )
{
while( length > )
{
for( i = ; i < ; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] ); des_crypt_ecb( ctx, output, output );
memcpy( iv, output, ); input += ;
output += ;
length -= ;
}
}
else /* DES_DECRYPT */
{
while( length > )
{
memcpy( temp, input, );
des_crypt_ecb( ctx, input, output ); for( i = ; i < ; i++ )
output[i] = (unsigned char)( output[i] ^ iv[i] ); memcpy( iv, temp, ); input += ;
output += ;
length -= ;
}
} return( );
} /*
* 3DES-ECB block encryption/decryption
*/
int des3_crypt_ecb( des3_context *ctx,
const unsigned char input[],
unsigned char output[] )
{
int i;
unsigned long X, Y, T, *SK; SK = ctx->sk; GET_ULONG_BE( X, input, );
GET_ULONG_BE( Y, input, ); DES_IP( X, Y ); for( i = ; i < ; i++ )
{
DES_ROUND( Y, X );
DES_ROUND( X, Y );
} for( i = ; i < ; i++ )
{
DES_ROUND( X, Y );
DES_ROUND( Y, X );
} for( i = ; i < ; i++ )
{
DES_ROUND( Y, X );
DES_ROUND( X, Y );
} DES_FP( Y, X ); PUT_ULONG_BE( Y, output, );
PUT_ULONG_BE( X, output, ); return( );
} /*
* 3DES-CBC buffer encryption/decryption
*/
int des3_crypt_cbc( des3_context *ctx,
int mode,
int length,
unsigned char iv[],
const unsigned char *input,
unsigned char *output )
{
int i;
unsigned char temp[]; if( length % )
return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH ); if( mode == DES_ENCRYPT )
{
while( length > )
{
for( i = ; i < ; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] ); des3_crypt_ecb( ctx, output, output );
memcpy( iv, output, ); input += ;
output += ;
length -= ;
}
}
else /* DES_DECRYPT */
{
while( length > )
{
memcpy( temp, input, );
des3_crypt_ecb( ctx, input, output ); for( i = ; i < ; i++ )
output[i] = (unsigned char)( output[i] ^ iv[i] ); memcpy( iv, temp, ); input += ;
output += ;
length -= ;
}
} return( );
} #if defined(POLARSSL_SELF_TEST) #include <stdio.h> /*
* DES and 3DES test vectors from: */
static const unsigned char des3_test_keys[] =
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
}; static const unsigned char des3_test_iv[] =
{
0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
}; static const unsigned char des3_test_buf[] =
{
0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
}; static const unsigned char des3_test_ecb_dec[][] =
{
{ 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
{ 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
{ 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
}; static const unsigned char des3_test_ecb_enc[][] =
{
{ 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
{ 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
{ 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
}; static const unsigned char des3_test_cbc_dec[][] =
{
{ 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 },
{ 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 },
{ 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C }
}; static const unsigned char des3_test_cbc_enc[][] =
{
{ 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 },
{ 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
{ 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
}; /*
* Checkup routine
*/
int des_self_test( int verbose )
{
int i, j, u, v;
des_context ctx;
des3_context ctx3;
unsigned char key[];
unsigned char buf[];
unsigned char prv[];
unsigned char iv[]; memset( key, , ); /*
* ECB mode
*/
for( i = ; i < ; i++ )
{
u = i >> ;
v = i & ; if( verbose != )
printf( " DES%c-ECB-%3d (%s): ",
( u == ) ? ' ' : '', + u * ,
( v == DES_DECRYPT ) ? "dec" : "enc" ); memcpy( buf, des3_test_buf, ); switch( i )
{
case :
des_setkey_dec( &ctx, (unsigned char *) des3_test_keys );
break; case :
des_setkey_enc( &ctx, (unsigned char *) des3_test_keys );
break; case :
des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys );
break; default:
return( );
} for( j = ; j < ; j++ )
{
if( u == )
des_crypt_ecb( &ctx, buf, buf );
else
des3_crypt_ecb( &ctx3, buf, buf );
} if( ( v == DES_DECRYPT &&
memcmp( buf, des3_test_ecb_dec[u], ) != ) ||
( v != DES_DECRYPT &&
memcmp( buf, des3_test_ecb_enc[u], ) != ) )
{
if( verbose != )
printf( "failed\n" ); return( );
} if( verbose != )
printf( "passed\n" );
} if( verbose != )
printf( "\n" ); /*
* CBC mode
*/
for( i = ; i < ; i++ )
{
u = i >> ;
v = i & ; if( verbose != )
printf( " DES%c-CBC-%3d (%s): ",
( u == ) ? ' ' : '', + u * ,
( v == DES_DECRYPT ) ? "dec" : "enc" ); memcpy( iv, des3_test_iv, );
memcpy( prv, des3_test_iv, );
memcpy( buf, des3_test_buf, ); switch( i )
{
case :
des_setkey_dec( &ctx, (unsigned char *) des3_test_keys );
break; case :
des_setkey_enc( &ctx, (unsigned char *) des3_test_keys );
break; case :
des3_set2key_dec( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set2key_enc( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set3key_dec( &ctx3, (unsigned char *) des3_test_keys );
break; case :
des3_set3key_enc( &ctx3, (unsigned char *) des3_test_keys );
break; default:
return( );
} if( v == DES_DECRYPT )
{
for( j = ; j < ; j++ )
{
if( u == )
des_crypt_cbc( &ctx, v, , iv, buf, buf );
else
des3_crypt_cbc( &ctx3, v, , iv, buf, buf );
}
}
else
{
for( j = ; j < ; j++ )
{
unsigned char tmp[]; if( u == )
des_crypt_cbc( &ctx, v, , iv, buf, buf );
else
des3_crypt_cbc( &ctx3, v, , iv, buf, buf ); memcpy( tmp, prv, );
memcpy( prv, buf, );
memcpy( buf, tmp, );
} memcpy( buf, prv, );
} if( ( v == DES_DECRYPT &&
memcmp( buf, des3_test_cbc_dec[u], ) != ) ||
( v != DES_DECRYPT &&
memcmp( buf, des3_test_cbc_enc[u], ) != ) )
{
if( verbose != )
printf( "failed\n" ); return( );
} if( verbose != )
printf( "passed\n" );
} if( verbose != )
printf( "\n" ); return( );
} #endif #endif
/*里面包含加密 解密 des DES3 */
/*
int for_encrypt_des3(char *data, unsigned int len)
{
int i, j;
unsigned char buf[BUFSIZE];
unsigned long tsc;
unsigned char tmp[64];
FILE *fp; des3_context des3; // des_context des; des_setkey_enc( &des, tmp);
set_alarm( 1 );
for(i=1; !alarmed; i++) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
tsc = hardclock();
for(j=0; j<1024; j++) des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
printf("DES speed: %9lu Kb/s, %9lu cycles/byte\n", i*BUFSIZE /1024, (hardclock()-tsc)/(j*BUFSIZE)); des3_set3key_enc( &des3, tmp );
set_alarm( 1 );
for(i=1; !alarmed; i++) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
tsc = hardclock();
for(j=0; j<1024; j++) des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); DO_LOG("DES3 speed: %9lu Kb/s, %9lu cycles/byte\n", i*BUFSIZE/1024, (hardclock()-tsc)/(j*BUFSIZE)); memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh");
memset(buf, 0, sizeof(buf)); strncpy(buf, data,len);
des3_set3key_enc( &des3, tmp);
DO_LOG("DES3 pwd=%s, txt=%s\n", tmp, buf);
des3_crypt_cbc(&des3, DES_ENCRYPT, 32768, tmp, buf, buf );
DO_LOG("encrypt txt=%s\n",buf);
fp = fopen("/D1", "wb");
if(fp!=NULL)
{
fprintf(fp,"%s",buf);
fclose(fp);
} memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh");
des3_set3key_dec(&des3, tmp);
des3_crypt_cbc(&des3, DES_DECRYPT, 32768, tmp, buf, buf );
//DO_LOG("decrypt txt= %s\n",buf); memset(tmp, 0, sizeof(tmp)); strcpy(tmp, "01234567abcdefgh");
des_setkey_dec(&des, tmp);
des_crypt_cbc(&des, DES_DECRYPT, 32768, tmp, buf, buf );
fp = fopen("/D2", "wb");
if(fp!=NULL)
{
fprintf(fp,"%s",buf);
fclose(fp);
}
return 0;
}
*/
int hexcharToInt(char c)
{
if (c >= '' && c <= '') return (c - '');
if (c >= 'A' && c <= 'F') return (c - 'A' + );
if (c >= 'a' && c <= 'f') return (c - 'a' + );
return ;
} void hexstringToBytes(char* hexstring,char* bytes,int hexlength)
{
int i= ;
//cout<<"length is :"<<sizeof(hexstring)/sizeof(char)<<endl;
for (i= ; i <hexlength ; i+=)
{
bytes[i/] = (char) ((hexcharToInt(hexstring[i]) << )| hexcharToInt(hexstring[i+]));
}
} void for_decode_des3(unsigned char *data, unsigned int len)//解密
{
char buf[];
unsigned char tmp[];
des3_context des3;
memset(buf, , );
hexstringToBytes((char *)data, buf, );
memset(data,,);
strncpy((char*)data,buf,strlen(buf)); memset(tmp, , sizeof(tmp)); strcpy((char*)tmp, "JDWA2003");
des3_set3key_dec(&des3, tmp);
des3_crypt_cbc(&des3, DES_DECRYPT, , tmp, data,data);
}
DES/des3 加密程序的更多相关文章
- PHP使用DES进行加密解密
DES是一种对称加密算法,也就是通过密文和合法的密钥能够将明文还原出来,在程序开发过程中有些 接口可能需要获取原始数据,而发送的数据又比较敏感(比如用户的密码等信息),这时可以选择DES加密算法,DE ...
- .NET和JAVA同等加密方法,MD5和DES对称加密记录
C#版: using System; using System.Security.Cryptography; using System.Text; namespace ConsoleApplicati ...
- Java和.NET使用DES对称加密的区别
Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...
- 基于DES算法加密的防撞库密码系统项目总结
项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 使用openssl库实现des,3des加密
原文地址: 使用openssl库实现des,3des加密 主要是调整了一下格式,以及一些变量的类型,以解决在VC2008下无法编译通过的问题. #include <stdio.h> #in ...
- linux c最简单的加密程序
最初的密码程序是在Hirst First c里面看到的,大概内容如下:对待加密的字符串的每一个字符和某个数值进行一次按位异或得到密文,再进行一次按位异或得到明文. 补充知识:按位异或的结果是“同位得1 ...
- Python基础教程3——教你用Python做个简单的加密程序(还基础什么呀,直接来练习吧,带源码)
因为发现基础教程我之前推荐的那个网站就已经很完善了,就不重复写了,所以本汪来一起做练习吧. 一.加密原理 记得当时我学c++的时候,学到输入输出流的时候,当时王老师就教我们写了一个小的加密程序,所以这 ...
- DES的加密与解密算法(Python实现)
DES的加密与解密算法(Python实现) 密码学实验:实现了DES的简单的加密和解密算法,DES算法的相关资料网上很多,这里不再赘述,仅仅贴出源代码给大家分享,源码中包含很多汉字注释,相信大家都是可 ...
随机推荐
- mvc4中的过滤器
过滤器(Filter)把附加逻辑注入到MVC框架的请求处理.实现了交叉关注. 交叉关注:用于整个应用程序,又不适合放在某个局部位置的功能. 过滤器是.NET的注解属性(Attribute),它们对请求 ...
- TypeScript设计模式之策略、模板方法
看看用TypeScript怎样实现常见的设计模式,顺便复习一下. 学模式最重要的不是记UML,而是知道什么模式可以解决什么样的问题,在做项目时碰到问题可以想到用哪个模式可以解决,UML忘了可以查,思想 ...
- React组件开发经典案例--todolist
点开查看代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- cocos2dx 中文路径编译错误记录
'/Q' 不是内部或外部命令,也不是可运行的程序1> 或批处理文件.1> 'y' 不是内部或外部命令,也不是可运行的程序1> 或批处理文件.1>C:\Program Files ...
- wemall doraemon中Android app商城系统向指定URL发送GET方法的请求代码
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...
- yaourt 之 Curl 错误
最近执行 yaourt 更新时总是出现以下错误: curl error: Couldn't connect to server 无法进行更新.把配置中的下载工具更换了成 axel 等其它下载工具,还是 ...
- 关于VS2013的编码的UI测试。
1. 打开VS2013,选择文件→新建→项目 2. 弹出的选项左侧选择visual C#中的测试,中间选择框选择编码的UI测试项目,确定后就产生的测试项目. 3. 弹出框选择默认的录制操作巴拉巴 ...
- 用ajax实现不刷新分页
今天我们要用ajax做一个分页: 实现Ajax分页: 如果可以的话加上查询条件 找一张表做分页 分页不使用page类 页面不用刷新 Ajax加载数据 <!doctype html> < ...
- Pydev--unresolved import:解决办法
1.右键点击项目,选择Properties-->Pydev-Interpreter/Grammar 2.点击"Click here to configure an interprete ...
- 分享小知识:善用Group By排序
以下列举了公用表/临时表/聚合函数三个因素为例子(覆盖索引因素除外,有利用此类索引都会以索引顺序) 环境: Microsoft SQL Server 2014 (SP1-GDR) (KB319472 ...