本站提倡有节制游戏,合理安排游戏时间,注意劳逸结合。

【源码分析与应用】【卡券回收商城 源码】【简单悬赏任务平台源码】des源码 c

2024-12-29 16:46:19 来源:热点 分类:热点

1.des算法源代码
2.c# 编程 DES

des源码 c

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

c# 编程 DES

       CLASS1 写出来 c#不是实现过DES类吗

       DES 实现都必须从中派生的数据加密标准 (DES) 算法的基类。

       命名空间:System.Security.Cryptography

       程序集:mscorlib(在 mscorlib.dll 中)

       语法

       Visual Basic(声明)

       <ComVisibleAttribute(True)> _

       Public MustInherit Class DES

        Inherits SymmetricAlgorithm

       Visual Basic(用法)

       Dim instance As DES

       C#

       [ComVisibleAttribute(true)]

       public abstract class DES : SymmetricAlgorithm

       C++

       [ComVisibleAttribute(true)]

       public ref class DES abstract : public SymmetricAlgorithm

       J#

       /** @attribute ComVisibleAttribute(true) */

       public abstract class DES extends SymmetricAlgorithm

       JScript

       ComVisibleAttribute(true)

       public abstract class DES extends SymmetricAlgorithm

       备注

       此算法支持长度为 位的源码分析与应用密钥。

       示例

       下面的卡券回收商城 源码示例方法使用带有指定的 Key 和初始化向量 (IV) 的 DESCryptoServiceProvider(DES 的实现)来加密由 inName 指定的文件,并将加密的简单悬赏任务平台源码结果输出到由 outName 指定的文件。

       C# 复制代码

       private static void EncryptData(String inName,印尼溯源码即食燕窝 String outName, byte[] desKey, byte[] desIV)

        {

        //Create the file streams to handle the input and output files.

        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);

        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);

        fout.SetLength(0);

        //Create variables to help with read and write.

        byte[] bin = new byte[]; //This is intermediate storage for the encryption.

        long rdlen = 0; //This is the total number of bytes written.

        long totlen = fin.Length; //This is the total length of the input file.

        int len; //This is the number of bytes to be written at a time.

        DES des = new DESCryptoServiceProvider();

        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

        Console.WriteLine("Encrypting...");

        //Read from the input file, then encrypt and write to the output file.

        while(rdlen < totlen)

        {

        len = fin.Read(bin, 0, );

        encStream.Write(bin, 0, len);

        rdlen = rdlen + len;

        Console.WriteLine("{ 0} bytes processed", rdlen);

        }

        encStream.Close();

        fout.Close();

        fin.Close();

        }

       可以用同样的方式处理解密;使用 CreateDecryptor 而不是 CreateEncryptor。必须使用加密该文件所用的划线指标源码怎么用同一 Key 和 IV 进行解密。

       继承层次结构

       System.Object

        System.Security.Cryptography.SymmetricAlgorithm

        System.Security.Cryptography.DES

        System.Security.Cryptography.DESCryptoServiceProvider

       线程安全

       此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

       平台

相关推荐
一周热点