📄 aes.h
字号:
#if !defined AES_H
#define AES_H
#define ROTL(x,count) (((x) >> (thirtytwo-(count))) | ((x) << (count)))
#define ROTR(x,count) (((x) << (thirtytwo-(count))) | ((x) >> (count)))
#define CONST_ROTL(x,count) (((x) >> (32-(count))) | ((x) << (count)))
#define CONST_ROTR(x,count) (((x) << (32-(count))) | ((x) >> (count)))
#define DECRYPT_ROUND(SSS,offset,AAA,BBB,CCC,DDD,t,u) t = (BBB+BBB+1)*BBB;u = (DDD+DDD+1)*DDD;t = CONST_ROTL(t, 5);u = CONST_ROTL(u, 5);AAA -= SSS[offset];CCC -= SSS[offset+1];AAA = ROTR(AAA, u)^t;CCC = ROTR(CCC, t)^u
#define DECRYPT_MIDDLE(SS,AA,BB,CC,DD) DECRYPT_ROUND(SS, 38, CC, DD, AA, BB,t,u);DECRYPT_ROUND(SS, 36, BB, CC, DD, AA,t,u);DECRYPT_ROUND(SS, 34, AA, BB, CC, DD,t,u);DECRYPT_ROUND(SS, 32, DD, AA, BB, CC,t,u);DECRYPT_ROUND(SS, 30, CC, DD, AA, BB,t,u);DECRYPT_ROUND(SS, 28, BB, CC, DD, AA,t,u);DECRYPT_ROUND(SS, 26, AA, BB, CC, DD,t,u);DECRYPT_ROUND(SS, 24, DD, AA, BB, CC,t,u);DECRYPT_ROUND(SS, 22, CC, DD, AA, BB,t,u);DECRYPT_ROUND(SS, 20, BB, CC, DD, AA,t,u);DECRYPT_ROUND(SS, 18, AA, BB, CC, DD,t,u);DECRYPT_ROUND(SS, 16, DD, AA, BB, CC,t,u);DECRYPT_ROUND(SS, 14, CC, DD, AA, BB,t,u);DECRYPT_ROUND(SS, 12, BB, CC, DD, AA,t,u);DECRYPT_ROUND(SS, 10, AA, BB, CC, DD,t,u);DECRYPT_ROUND(SS, 8, DD, AA, BB, CC,t,u);DECRYPT_ROUND(SS, 6, CC, DD, AA, BB,t,u);DECRYPT_ROUND(SS, 4, BB, CC, DD, AA,t,u)
#define ENCRYPT_ROUND(SSS,offset,AAA,BBB,CCC,DDD,t,u) t = (BBB+BBB+1)*BBB;u = (DDD+DDD+1)*DDD;t = CONST_ROTL(t, 5);u = CONST_ROTL(u, 5);AAA ^= t;CCC ^= u;AAA = ROTL(AAA, u)+SSS[offset];CCC = ROTL(CCC, t)+SSS[offset+1]
#define ENCRYPT_MIDDLE(SS,AA,BB,CC,DD) ENCRYPT_ROUND(SS, 4, BB, CC, DD, AA,t,u);ENCRYPT_ROUND(SS, 6, CC, DD, AA, BB,t,u);ENCRYPT_ROUND(SS, 8, DD, AA, BB, CC,t,u);ENCRYPT_ROUND(SS, 10, AA, BB, CC, DD,t,u);ENCRYPT_ROUND(SS, 12, BB, CC, DD, AA,t,u);ENCRYPT_ROUND(SS, 14, CC, DD, AA, BB,t,u);ENCRYPT_ROUND(SS, 16, DD, AA, BB, CC,t,u);ENCRYPT_ROUND(SS, 18, AA, BB, CC, DD,t,u);ENCRYPT_ROUND(SS, 20, BB, CC, DD, AA,t,u);ENCRYPT_ROUND(SS, 22, CC, DD, AA, BB,t,u);ENCRYPT_ROUND(SS, 24, DD, AA, BB, CC,t,u);ENCRYPT_ROUND(SS, 26, AA, BB, CC, DD,t,u);ENCRYPT_ROUND(SS, 28, BB, CC, DD, AA,t,u);ENCRYPT_ROUND(SS, 30, CC, DD, AA, BB,t,u);ENCRYPT_ROUND(SS, 32, DD, AA, BB, CC,t,u);ENCRYPT_ROUND(SS, 34, AA, BB, CC, DD,t,u);ENCRYPT_ROUND(SS, 36, BB, CC, DD, AA,t,u)
#define ConvertDigitToNumber(c) (digitValue[(c)])
#define DIR_ENCRYPT 0 /* Are we encrpyting? */
#define DIR_DECRYPT 1 /* Are we decrpyting? */
#define MODE_ECB 1 /* Are we ciphering in ECB mode? */
#define MODE_CBC 2 /* Are we ciphering in CBC mode? */
#define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
#define TRUE 1
#define FALSE 0
/* This #define is specific to RC6: the number of rounds of encipherment */
#define ROUNDS 20
/* Error Codes - CHANGE POSSIBLE: inclusion of additional error codes */
#define BAD_KEY_DIR -1 /* Key direction is invalid, e.g.,
unknown value */
#define BAD_KEY_MAT -2 /* Key material not of correct
length */
#define BAD_KEY_INSTANCE -3 /* Key passed is not valid */
#define BAD_CIPHER_MODE -4 /* Params struct passed to
cipherInit invalid */
#define BAD_CIPHER_STATE -5 /* Cipher in wrong state (e.g., not
initialized) */
/* This #define was added to be returned when cipherInit() is
* provided with bad IV material. It's not really RC6-specific. */
#define BAD_IV_MAT -6 /* IV material is bad */
/* CHANGE POSSIBLE: inclusion of algorithm specific defines */
/* The value of MAX_KEY_SIZE is specific to RC6 */
#define MAX_KEY_SIZE 510 /* # of ASCII char's needed to
represent a key */
#define MAX_IV_SIZE 16 /* # bytes needed to
represent an IV */
/* The structure for key information */
typedef struct
{
BYTE direction; /* Key used for encrypting or decrypting? */
int keyLen; /* Length of the key */
char keyMaterial[MAX_KEY_SIZE+1]; /* Raw key data in ASCII,*/
unsigned long S[2+2*ROUNDS+2]; /* Key schedule */
}keyInstance;
typedef struct
{
BYTE mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
BYTE IV[MAX_IV_SIZE]; /* A possible Initialization Vector for*/
} cipherInstance;
/* Function protoypes */
class CAES
{
private:
int makeKey(keyInstance *key, BYTE direction, int keyLen,char *keyMaterial);
int cipherInit(cipherInstance *cipher, BYTE mode, char *IV);
int blockEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input,int inputLen, BYTE *outBuffer);
void blockXOR_Encrypt(char *key,int keylen,unsigned char *input,int inputlen,unsigned char *output);
void encrypt1(char *key,int keylen,unsigned char *input,int inputlen,unsigned char *output);
void encrypt2(char *key,int keylen,unsigned char *input,int inputlen,unsigned char *output);
int blockDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input,int inputLen, BYTE *outBuffer);
void Rc6ComputeKeySchedule(BYTE* key, int keyLengthInBytes,unsigned long* S);
void Rc6EncryptEcb(unsigned long* keySchedule, int numberOfBlocks,unsigned long* plaintext, unsigned long* ciphertext);
void Rc6DecryptEcb(unsigned long* keySchedule, int numberOfBlocks,unsigned long* ciphertext, unsigned long* plaintext);
void Rc6EncryptCbc(unsigned long* keySchedule, int numberOfBlocks,unsigned long* ivDwords,unsigned long* plaintext, unsigned long* ciphertext);
public:
CAES();
virtual ~CAES();
int My_Encrypt(char* key,int keylen, BYTE *input,int inputlen,int type,BYTE *output);
int My_Decrypt(char* key,int keylen, BYTE *input,int inputlen,int type,BYTE *output);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -