1.des算法源代码
2.ACMå¨çº¿ç¼è¯ç³»ç» Memory[KB] ãTime[MS] ãLenght[Byte] åå«ä»£è¡¨ä»ä¹
des算法源代码
des.h文件:
#ifndef CRYPTOPP_DES_H
#define CRYPTOPP_DES_H
#include "cryptlib.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
class DES : public BlockTransformation
{
public:
DES(const byte *userKey, CipherDir);
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const
{ DES::ProcessBlock(inoutBlock, inoutBlock);}
enum { KEYLENGTH=8, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
protected:
static const word Spbox[8][];
SecBlock<word> k;
};
class DESEncryption : public DES
{
public:
DESEncryption(const byte * userKey)
: DES (userKey, ENCRYPTION) { }
};
class DESDecryption : public DES
{
public:
DESDecryption(const byte * userKey)
: DES (userKey, DECRYPTION) { }
};
class DES_EDE_Encryption : public BlockTransformation
{
public:
DES_EDE_Encryption(const byte * userKey)
: e(userKey, ENCRYPTION), d(userKey + DES::KEYLENGTH, DECRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES e, d;
};
class DES_EDE_Decryption : public BlockTransformation
{
public:
DES_EDE_Decryption(const byte * userKey)
: d(userKey, DECRYPTION), e(userKey + DES::KEYLENGTH, ENCRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES d, e;
};
class TripleDES_Encryption : public BlockTransformation
{
public:
TripleDES_Encryption(const byte * userKey)
: e1(userKey, ENCRYPTION), d(userKey + DES::KEYLENGTH, DECRYPTION),
e2(userKey + 2*DES::KEYLENGTH, ENCRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES e1, d, e2;
};
class TripleDES_Decryption : public BlockTransformation
{
public:
TripleDES_Decryption(const byte * userKey)
: d1(userKey + 2*DES::KEYLENGTH, DECRYPTION), e(userKey + DES::KEYLENGTH, ENCRYPTION),
d2(userKey, DECRYPTION) { }
void ProcessBlock(const byte *inBlock, byte * outBlock) const;
void ProcessBlock(byte * inoutBlock) const;
enum { KEYLENGTH=, BLOCKSIZE=8};
unsigned int BlockSize() const { return BLOCKSIZE;}
private:
DES d1, e, d2;
};
NAMESPACE_END
#endif
des.cpp文件:
// des.cpp - modified by Wei Dai from:
/*
* This is a major rewrite of my old public domain DES code written
* circa , which in turn borrowed heavily from Jim Gillogly's
* public domain code. I pretty much kept my key scheduling code, but
* the actual encrypt/decrypt routines are taken from from Richard
* Outerbridge's DES code as printed in Schneier's "Applied Cryptography."
*
* This code is in the public domain. I would appreciate bug reports and
* enhancements.
*
* Phil Karn KA9Q, karn@unix.ka9q.ampr.org, August .
*/
#include "pch.h"
#include "misc.h"
#include "des.h"
NAMESPACE_BEGIN(CryptoPP)
/* Tables defined in the Data Encryption Standard documents
* Three of these tables, the initial permutation, the final
* permutation and the expansion operator, are regular enough that
* for speed, we hard-code them. They're here for reference only.
* Also, the S and P boxes are used by a separate program, gensp.c,
* to build the combined SP box, Spbox[]. They're also here just
* for reference.
*/
#ifdef notdef
/* initial permutation IP */
static byte ip[] = {
, , , , , , , 2,
, , , , , , , 4,
, , , , , , , 6,
, , , , , , , 8,
, , , , , , 9, 1,
, , , , , , , 3,
, , , , , , , 5,
, , , , , , , 7
};
/* final permutation IP^-1 */
static byte fp[] = {
, 8, , , , , , ,
, 7, , , , , , ,
, 6, , , , , , ,
, 5, , , , , , ,
, 4, , , , , , ,
, 3, , , , , , ,
, 2, , , , , , ,
, 1, , 9, , , ,
};
/* expansion operation matrix */
static byte ei[] = {
, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , , 1
};
/* The (in)famous S-boxes */
static byte sbox[8][] = {
/* S1 */
, 4, , 1, 2, , , 8, 3, , 6, , 5, 9, 0, 7,
0, , 7, 4, , 2, , 1, , 6, , , 9, 5, 3, 8,
4, 1, , 8, , 6, 2, , , , 9, 7, 3, , 5, 0,
, , 8, 2, 4, 9, 1, 7, 5, , 3, , , 0, 6, ,
/* S2 */
, 1, 8, , 6, , 3, 4, 9, 7, 2, , , 0, 5, ,
3, , 4, 7, , 2, 8, , , 0, 1, , 6, 9, , 5,
0, , 7, , , 4, , 1, 5, 8, , 6, 9, 3, 2, ,
, 8, , 1, 3, , 4, 2, , 6, 7, , 0, 5, , 9,
/* S3 */
, 0, 9, , 6, 3, , 5, 1, , , 7, , 4, 2, 8,
, 7, 0, 9, 3, 4, 6, , 2, 8, 5, , , , , 1,
, 6, 4, 9, 8, , 3, 0, , 1, 2, , 5, , , 7,
1, , , 0, 6, 9, 8, 7, 4, , , 3, , 5, 2, ,
/* S4 */
7, , , 3, 0, 6, 9, , 1, 2, 8, 5, , , 4, ,
, 8, , 5, 6, , 0, 3, 4, 7, 2, , 1, , , 9,
, 6, 9, 0, , , 7, , , 1, 3, , 5, 2, 8, 4,
3, , 0, 6, , 1, , 8, 9, 4, 5, , , 7, 2, ,
/* S5 */
2, , 4, 1, 7, , , 6, 8, 5, 3, , , 0, , 9,
, , 2, , 4, 7, , 1, 5, 0, , , 3, 9, 8, 6,
4, 2, 1, , , , 7, 8, , 9, , 5, 6, 3, 0, ,
, 8, , 7, 1, , 2, , 6, , 0, 9, , 4, 5, 3,
/* S6 */
, 1, , , 9, 2, 6, 8, 0, , 3, 4, , 7, 5, ,
, , 4, 2, 7, , 9, 5, 6, 1, , , 0, , 3, 8,
9, , , 5, 2, 8, , 3, 7, 0, 4, , 1, , , 6,
4, 3, 2, , 9, 5, , , , , 1, 7, 6, 0, 8, ,
/* S7 */
4, , 2, , , 0, 8, , 3, , 9, 7, 5, , 6, 1,
, 0, , 7, 4, 9, 1, , , 3, 5, , 2, , 8, 6,
1, 4, , , , 3, 7, , , , 6, 8, 0, 5, 9, 2,
6, , , 8, 1, 4, , 7, 9, 5, 0, , , 2, 3, ,
/* S8 */
, 2, 8, 4, 6, , , 1, , 9, 3, , 5, 0, , 7,
1, , , 8, , 3, 7, 4, , 5, 6, , 0, , 9, 2,
7, , 4, 1, 9, , , 2, 0, 6, , , , 3, 5, 8,
2, 1, , 7, 4, , 8, , , , 9, 0, 3, 5, 6,
};
/* -bit permutation function P used on the output of the S-boxes */
static byte pi[] = {
, 7, , ,
, , , ,
1, , , ,
5, , , ,
2, 8, , ,
, , 3, 9,
, , , 6,
, , 4,
};
#endif
/* permuted choice table (key) */
static const byte pc1[] = {
, , , , , , 9,
1, , , , , , ,
, 2, , , , , ,
, , 3, , , , ,
, , , , , , ,
7, , , , , , ,
, 6, , , , , ,
, , 5, , , , 4
};
/* number left rotations of pc1 */
static const byte totrot[] = {
1,2,4,6,8,,,,,,,,,,,
};
/* permuted choice key (table) */
static const byte pc2[] = {
, , , , 1, 5,
3, , , 6, , ,
, , , 4, , 8,
, 7, , , , 2,
, , , , , ,
, , , , , ,
, , , , , ,
, , , , ,
};
/* End of DES-defined tables */
/* bit 0 is left-most in byte */
static const int bytebit[] = {
,,,,,,,
};
/* Set key (initialize key schedule array) */
DES::DES(const byte *key, CipherDir dir)
: k()
{
SecByteBlock buffer(++8);
byte *const pc1m=buffer; /* place to modify pc1 into */
byte *const pcr=pc1m+; /* place to rotate pc1 into */
byte *const ks=pcr+;
register int i,j,l;
int m;
for (j=0; j<; j++) { /* convert pc1 to bits of key */
l=pc1[j]-1; /* integer bit location */
m = l & ; /* find bit */
pc1m[j]=(key[l>>3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
1 : 0; /* and store 1-bit result */}
for (i=0; i<; i++) { /* key chunk for each iteration */
memset(ks,0,8); /* Clear key schedule */
for (j=0; j<; j++) /* rotate pc1 the right amount */
pcr[j] = pc1m[(l=j+totrot[i])<(j<? : ) ? l: l-];
/* rotate left and right halves independently */
for (j=0; j<; j++){ /* select bits individually */
/* check bit that goes to ks[j] */
if (pcr[pc2[j]-1]){
/* mask it in if it's there */
l= j % 6;
ks[j/6] |= bytebit[l] >> 2;
}
}
/* Now convert to odd/even interleaved form for use in F */
k[2*i] = ((word)ks[0] << )
| ((word)ks[2] << )
| ((word)ks[4] << 8)
| ((word)ks[6]);
k[2*i+1] = ((word)ks[1] << )
| ((word)ks[3] << )
| ((word)ks[5] << 8)
| ((word)ks[7]);
}
if (dir==DECRYPTION) // reverse key schedule order
for (i=0; i<; i+=2)
{
std::swap(k[i], k[-2-i]);
std::swap(k[i+1], k[-1-i]);
}
}
/* End of C code common to both versions */
/* C code only in portable version */
// Richard Outerbridge's initial permutation algorithm
/*
inline void IPERM(word &left, word &right)
{
word work;
work = ((left >> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
left ^= work << 4;
work = ((left >> ) ^ right) & 0xffff;
right ^= work;
left ^= work << ;
work = ((right >> 2) ^ left) & 0x;
left ^= work;
right ^= (work << 2);
work = ((right >> 8) ^ left) & 0xffff;
left ^= work;
right ^= (work << 8);
right = rotl(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotl(left, 1);
}
inline void FPERM(word &left, word &right)
{
word work;
right = rotr(right, 1);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
left = rotr(left, 1);
work = ((left >> 8) ^ right) & 0xffff;
right ^= work;
left ^= work << 8;
work = ((left >> 2) ^ right) & 0x;
right ^= work;
left ^= work << 2;
work = ((right >> ) ^ left) & 0xffff;
left ^= work;
right ^= work << ;
work = ((right >> 4) ^ left) & 0x0f0f0f0f;
left ^= work;
right ^= work << 4;
}
*/
// Wei Dai's modification to Richard Outerbridge's initial permutation
// algorithm, this one is faster if you have access to rotate instructions
// (like in MSVC)
inline void IPERM(word &left, word &right)
{
word work;
right = rotl(right, 4U);
work = (left ^ right) & 0xf0f0f0f0;
left ^= work;
right = rotr(right^work, U);
work = (left ^ right) & 0xffff;
left ^= work;
right = rotr(right^work, U);
work = (left ^ right) & 0x;
left ^= work;
right = rotr(right^work, 6U);
work = (left ^ right) & 0xffff;
left ^= work;
right = rotl(right^work, 9U);
work = (left ^ right) & 0xaaaaaaaa;
left = rotl(left^work, 1U);
right ^= work;
}
inline void FPERM(word &left, word &right)
{
word work;
right = rotr(right, 1U);
work = (left ^ right) & 0xaaaaaaaa;
right ^= work;
left = rotr(left^work, 9U);
work = (left ^ right) & 0xffff;
right ^= work;
left = rotl(left^work, 6U);
work = (left ^ right) & 0x;
right ^= work;
left = rotl(left^work, U);
work = (left ^ right) & 0xffff;
right ^= work;
left = rotl(left^work, U);
work = (left ^ right) & 0xf0f0f0f0;
right ^= work;
left = rotr(left^work, 4U);
}
// Encrypt or decrypt a block of data in ECB mode
void DES::ProcessBlock(const byte *inBlock, byte * outBlock) const
{
word l,r,work;
#ifdef IS_LITTLE_ENDIAN
l = byteReverse(*(word *)inBlock);
r = byteReverse(*(word *)(inBlock+4));
#else
l = *(word *)inBlock;
r = *(word *)(inBlock+4);
#endif
IPERM(l,r);
const word *kptr=k;
for (unsigned i=0; i<8; i++)
{
work = rotr(r, 4U) ^ kptr[4*i+0];
l ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> ) & 0x3f]
^ Spbox[0][(work >> ) & 0x3f];
work = r ^ kptr[4*i+1];
l ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> ) & 0x3f]
^ Spbox[1][(work >> ) & 0x3f];
work = rotr(l, 4U) ^ kptr[4*i+2];
r ^= Spbox[6][(work) & 0x3f]
^ Spbox[4][(work >> 8) & 0x3f]
^ Spbox[2][(work >> ) & 0x3f]
^ Spbox[0][(work >> ) & 0x3f];
work = l ^ kptr[4*i+3];
r ^= Spbox[7][(work) & 0x3f]
^ Spbox[5][(work >> 8) & 0x3f]
^ Spbox[3][(work >> ) & 0x3f]
^ Spbox[1][(work >> ) & 0x3f];
}
FPERM(l,r);
#ifdef IS_LITTLE_ENDIAN
*(word *)outBlock = byteReverse(r);
*(word *)(outBlock+4) = byteReverse(l);
#else
*(word *)outBlock = r;
*(word *)(outBlock+4) = l;
#endif
}
void DES_EDE_Encryption::ProcessBlock(byte *inoutBlock) const
{
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
}
void DES_EDE_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e.ProcessBlock(outBlock);
}
void DES_EDE_Decryption::ProcessBlock(byte *inoutBlock) const
{
d.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
}
void DES_EDE_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d.ProcessBlock(outBlock);
}
void TripleDES_Encryption::ProcessBlock(byte *inoutBlock) const
{
e1.ProcessBlock(inoutBlock);
d.ProcessBlock(inoutBlock);
e2.ProcessBlock(inoutBlock);
}
void TripleDES_Encryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
e1.ProcessBlock(inBlock, outBlock);
d.ProcessBlock(outBlock);
e2.ProcessBlock(outBlock);
}
void TripleDES_Decryption::ProcessBlock(byte *inoutBlock) const
{
d1.ProcessBlock(inoutBlock);
e.ProcessBlock(inoutBlock);
d2.ProcessBlock(inoutBlock);
}
void TripleDES_Decryption::ProcessBlock(const byte *inBlock, byte *outBlock) const
{
d1.ProcessBlock(inBlock, outBlock);
e.ProcessBlock(outBlock);
d2.ProcessBlock(outBlock);
}
NAMESPACE_END
ACMå¨çº¿ç¼è¯ç³»ç» Memory[KB] ãTime[MS] ãLenght[Byte] åå«ä»£è¡¨ä»ä¹
ä½ è¯´çé½æ²¡éMemoryæ¯ç¨åºå¨è¿è¡è¿ç¨ä¸ä½¿ç¨çå åæ åµï¼è¿ä¸ªåªè¦æ¥çå å使ç¨æ åµå°±è¡äºï¼windowséä»»å¡ç®¡çå¨å°±ææ¥çå å使ç¨ççåè½ï¼æ以è·åå åæ åµä¸æ¯é¾äºå§ãTimeæ¯ç¨åºè¿è¡çæ¶é´ï¼0çæ åµæå ç§å¯è½æ§ï¼ä¸ç§å°±æ¯æä¸ç§è¾åºé误ç¨åºå°±ä¸åè¿è¡ï¼æäºé¢ç®ä¸å¸¦æéå½å¾ªç¯çç®æ³çé¢ç®æç¨çæ¶é´ä¸ä¼å¾ä¹ ï¼èä¸æ¯ç²¾ç¡®å°æ¯«ç§ï¼æå¡å¨çè¿è¡é度æ¯ä¸ªäººçµèå¿«æ以èæ¶å¨æ¯«ç§çº§ä»¥ä¸æ¯æ£å¸¸çãTimeä¹å为å¢ä¸æ¶é´åcpuæ¶é´ï¼å¨ç¨åºå¼å§åè®°å½æ¶é´ï¼ç»ææ¶åè®°ä¸æ¶é´å°±è½è®¡ç®æç¨æ¶é´äºãLenghtæ¯ä½ 代ç 转æ¢ä¸ºææ¬åç大å°ãä¸è¬ä½ é½æ¯å°æºç¨åºæ件ä¸ä¼ ï¼ä»£ç æ件ç大å°å°±æ¯LenghtãGCCæwindowsçæ¬çï¼æè ä½ ä¹å¯ä»¥ä½¿ç¨GCCæ åçç¼è¯å¨MinGWï¼è¿äºé½æ¯ä½ç¼è¯å¨ã
2024-11-27 03:37
2024-11-27 03:34
2024-11-27 03:34
2024-11-27 02:46
2024-11-27 02:31
2024-11-27 02:23