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

📄 aes.cpp

📁 网络传输方面的范例程序,其中有MD5加密和解密的子程序,可以参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/*
 * Rc6DecryptEcb() decrypts a specified number of blocks in ECB mode.
 */
void CAES::Rc6DecryptEcb(unsigned long* keySchedule, int numberOfBlocks,
                          unsigned long* ciphertext, unsigned long* plaintext)
{
  unsigned long thirtytwo = 32;
  unsigned long S[2+2*ROUNDS+2];


  if (numberOfBlocks == 0)
    return;


  /* Let's put our key schedule conveniently in this stack frame */
  memcpy((void*) S, (void*) keySchedule, 4*(2+2*ROUNDS+2));


  do {
    unsigned long A, B, C, D;
    unsigned long t,u;

    /* Undo pseudo-round #21 and round #20 */
    A = ciphertext[0]-S[42];
    C = ciphertext[2]-S[43];

    t = (A+A+1)*A;
    u = (C+C+1)*C;
    t = CONST_ROTL(t, 5);
    u = CONST_ROTL(u, 5);

    D = ciphertext[3]-S[40];
    B = ciphertext[1]-S[41];
    D = ROTR(D, u) ^ t;
    B = ROTR(B, t) ^ u;


    /* Undo rounds #19-2 */
    DECRYPT_MIDDLE(S, A, B, C, D);


    /* Undo round #1 and pseudo-round #0 */
    t = (B+B+1)*B;
    u = (D+D+1)*D;
    t = CONST_ROTL(t, 5);
    u = CONST_ROTL(u, 5);

    A -= S[2];
    C -= S[3];
    plaintext[0] = ROTR(A, u) ^ t;
    plaintext[2] = ROTR(C, t) ^ u;
    plaintext[1] = B-S[0];
    plaintext[3] = D-S[1];


    /* Update text pointers */
    ciphertext += 4;
    plaintext += 4;

  } while (--numberOfBlocks > 0);
}



/*
 * makeKey() initializes a keyInstance.
 */
int CAES::makeKey(keyInstance* key, BYTE direction, int keyLen, char* keyMaterialold)
{
  BYTE keyBytes[255];
  short keyByte;
  short hexProblem = 0;
  int keyLengthInBytes = keyLen >> 3;
  int i;
  BYTE keyMaterial[32]; 
  for(i=0;i<16;i++)
  {
	  keyMaterial[i]=*keyMaterialold++;
	  keyMaterial[i+16]='1';
  }

  if ((direction != DIR_ENCRYPT) && (direction != DIR_DECRYPT))
    return BAD_KEY_DIR;
  key -> direction = direction;


  /* Deal with the key length.  RC6 can use keys of length 0-255 bytes
   * (0-2040 bits, in multiples of 8). */
  if ((keyLen < 0) || (keyLen > 2040) || ((keyLen & 0x7) != 0))
    return BAD_KEY_MAT;


  /* Get key as a sequence of bytes */
  for (i = keyLengthInBytes; --i >= 0; ) {
    keyBytes[i] = (BYTE) (keyByte = (short) (((ConvertDigitToNumber((BYTE) keyMaterial[i+i]) << 4) | (ConvertDigitToNumber((BYTE) keyMaterial[i+i+1])))));
    hexProblem |= keyByte;
  }


  /* The ConvertDigitToNumber() macro has been designed so that the
   * following test will correctly determine if the supplied ASCII key
   * was not a valid hexadecimal string. */
  //if (hexProblem & 0xff00)
  //  return BAD_KEY_MAT;


  /* Copy ASCII key */
  memcpy(key -> keyMaterial, keyMaterial, keyLengthInBytes << 1);
  key -> keyLen = keyLen;


  Rc6ComputeKeySchedule(keyBytes, keyLengthInBytes, key -> S);


  return TRUE;
  return 1;
}



/*
 * cipherInit() initializes a cipherInstance.
 */
int CAES::cipherInit(cipherInstance* cipher, BYTE mode, char* IV)
{
  short ivByte;
  short hexProblem = 0;
  int i;


  if ((mode != MODE_ECB) && (mode != MODE_CBC) && (mode != MODE_CFB1))
    return BAD_CIPHER_MODE;
  cipher -> mode = mode;


  /* Get IV, if appropriate */
  if (IV != NULL) {
    for (i = 16; --i >= 0; ) {
      ((BYTE*) (cipher -> IV))[i] = (BYTE) (
        ivByte = (short) (
          ((ConvertDigitToNumber((BYTE) IV[i+i]) << 4)
         | (ConvertDigitToNumber((BYTE) IV[i+i+1])))));
      hexProblem |= ivByte;
    }


    /* The ConvertDigitToNumber() macro has been designed so that the
     * following test will correctly determine if the supplied ASCII IV
     * was not a valid hexadecimal string. */
    if (hexProblem & 0xff00)
      return BAD_IV_MAT;
  }


  return 1;
}



/*
 * blockEncrypt() encrypts some plaintext.
 */
int CAES::blockEncrypt(cipherInstance *cipher, keyInstance *key,
                 BYTE *input, int inputLen, BYTE *outBuffer)
{
	int numberOfBlocks = inputLen >> 7;
  if (key -> direction != DIR_ENCRYPT)
    return BAD_KEY_MAT;
  
  if(cipher -> mode == MODE_ECB) 
  {
      Rc6EncryptEcb(key -> S, numberOfBlocks,(unsigned long*) input, (unsigned long*) outBuffer);
      return (numberOfBlocks << 7);
  }
  return BAD_KEY_MAT;
}
/*
 * blockDecrypt() decrypts some ciphertext.
 */
int CAES::blockDecrypt(cipherInstance *cipher, keyInstance *key,
                 BYTE *input, int inputLen, BYTE *outBuffer)
{
	int numberOfBlocks = inputLen >> 7;
	if (key -> direction != DIR_DECRYPT)
		return BAD_KEY_MAT;
 	if(cipher -> mode == MODE_ECB) 
	{
		Rc6DecryptEcb(key -> S, numberOfBlocks,(unsigned long*) input, (unsigned long*) outBuffer);
		return (numberOfBlocks << 7);

	}
	return BAD_KEY_MAT;
}
void CAES::encrypt1(char *key,int keylen,unsigned char *input,int inputlen,
                        unsigned char *output)
{
        unsigned char *in,*out;
        char *kk;
        int i;
        in=input;
        out=output;
        kk=key;
        for(i=0;i<inputlen;i++)
                (*out++)=(*in++)^(*kk++);
}
/*keylen <=inputlen*/
void CAES::encrypt2(char *key,int keylen,unsigned char *input,int inputlen,
                        unsigned char *output)
{
        int i,j,t,tail;
        unsigned char *in,*out;
        char *p;
        t=inputlen/keylen;
        tail=inputlen%keylen;
        in=input;
        out=output;
        if(t){
                for(i=0;i<t;i++){
                        p=key;
                        for(j=0;j<keylen;j++){
                                (*out)=(*in)^(*p);
                                 out=out+1;
                                 in=in+1;
                                 p=p+1;
                                }
                        }
                }
        if(tail){encrypt1(key,keylen,in,tail,out);
           /*     printf("encrypt2 \n");*/}
}

void CAES::blockXOR_Encrypt(char *key,int keylen,unsigned char *input,int inputlen,unsigned char *output)
{
        if(keylen>inputlen)
              encrypt1(key,keylen,input,inputlen,output);
        if(keylen<=inputlen)
              encrypt2(key,keylen,input,inputlen,output);

}
int CAES::My_Decrypt(char* key,int keylen, BYTE *input,int inputlen,int type,BYTE *output)
{

	keyInstance S;
    cipherInstance ci;
	int  blocks,tail_bits;
	int  keylen_bit;
	BYTE *in,*out;

	
	in=input;
	out=output;
	
	if(type==1)//aes
	{
		keylen_bit=keylen*8;
		blocks=inputlen/keylen;
		tail_bits=inputlen%keylen;

		if(makeKey(&S,DIR_DECRYPT,keylen_bit,key)<0)
			return -1;
		if(cipherInit(&ci,MODE_ECB,NULL)<0)
			return -2;
		if(blockDecrypt(&ci,&S,input,inputlen*8,output)<0)
			return -3;
	}
	if(type==2)//xor
	{
		blockXOR_Encrypt(key,keylen,input,inputlen,output);
	}
	return 1;
}
int CAES::My_Encrypt(char* key,int keylen, BYTE *input,int inputlen,int type,BYTE *output)
{
	keyInstance S;
	cipherInstance ci;

	int blocks,tail_bits;
	int  keylen_bit;
	BYTE *in,*out;


	in=input;
	out=output;
	if(type==1)
	{
		keylen_bit=keylen*8;
		blocks=inputlen/keylen;
		tail_bits=inputlen%keylen;
		if(makeKey(&S,DIR_ENCRYPT,keylen_bit,key)<0)
			return -1;
		if(cipherInit(&ci,MODE_ECB,NULL)<0)
			return -2;
		if(blockEncrypt(&ci,&S,input,inputlen*8,output)<0)
			return -3;
	}
	if(type==2)//xor
	{
		blockXOR_Encrypt(key,keylen,input,inputlen,output);
	}
	return 1;
}

⌨️ 快捷键说明

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