sP2P c++ STL *nix API  1.2
NAT traversal and peer-to-peer networking API providing all utilities needed to build peer-to-peer application straigth out of box
 All Classes Functions Variables Typedefs Enumerations Enumerator
sp2plib::AES Class Reference

Class for AES encryption/decription. Cryprion/Decryption is compatible with System.Security.Cryptography.Aes Some facts about AES for people meeting first time with it: More...

#include <AES.h>

Public Types

enum  KeySize { AESKS_Bits128 = 0, AESKS_Bits192 = 1, AESKS_Bits256 = 2 }
 AES key size enumerator More...
 
enum  Padding { AESPAD_None = 0, AESPAD_PCKS7 = 1 }
 AES padding type enumerator More...
 
enum  Mode { AESM_ECB = 0, AESM_CBC = 1 }
 AES chiper mode type enumerator More...
 

Public Member Functions

 AES (KeySize keySize, unsigned char *keyBytes, Padding padding=AESPAD_PCKS7, Mode mode=AESM_CBC)
 Creates new instance of AES class
 
int CryptedSize (int srcSize)
 Calculates size needed for crypted data array from given input data array size
 
int Crypt (unsigned char *src, int srcOffset, int srcLength, unsigned char *dst, int dstOffset)
 Crypts bytes from input array and stores them into destination array. Note taht input and output may have different sizes! Use CryptedSize function to calculate space needed for output array
 
int DecryptedSize (unsigned char *cryptedSrc, int cryptedOff, int cryptedLen)
 Calculates size needed for decryped data array
 
int Decrypt (unsigned char *src, int srcOffset, int srcLength, unsigned char *dst, int dstOffset)
 Decrypts data from input array and stores decrypted data to output array
 
int ChunkSize ()
 Gets basic chunk size of AES, alwaus 16
 

Public Attributes

unsigned char CBCInitialVector [16]
 Gets or sets the cbc initial vector used for bitwise exclusive OR operation with first block. By default if AES is in CBC mode it will be initaly set to value generated form key
 

Detailed Description

Class for AES encryption/decription. Cryprion/Decryption is compatible with System.Security.Cryptography.Aes Some facts about AES for people meeting first time with it:

  • AES chipers 16 byte chunks always and that is not dependent on key size If total number of message bytes is not divisible by 16 some sort of padding will be applied at end so total input array becomes divisible by 16 None - zeroes will be added and you will get this zeroes in decrypted array PCKS7 - X bytes having value X will be added at the end, if original input array is divisible by 16 then 16 bytes having value 16 will be added so 32 bytes to crypt this way will end as 48 bytes of crypted data
  • This implementation support CBC and ECB mode of chipering. -ECB means each chunk is chiped separately with KEY - you could extract partial data this way, but it's not recommended because of security weakness -CBC means XOR will be applied on chunk using previous chirped chunk value. For first chunk CBCInitialVector will be used for XOR operation. CBC makes total crypted data sensitive to even single bit change

Member Enumeration Documentation

AES key size enumerator

AES chiper mode type enumerator

Enumerator:
AESM_ECB 

The Electronic Codebook (ECB) mode encrypts each block individually. Any blocks of plain text that are identical and in the same message, or that are in a different message encrypted with the same key, will be transformed into identical cipher text blocks. Important: This mode is not recommended because it opens the door for multiple security exploits. If the plain text to be encrypted contains substantial repetition, it is feasible for the cipher text to be broken one block at a time. It is also possible to use block analysis to determine the encryption key. Also, an active adversary can substitute and exchange individual blocks without detection, which allows blocks to be saved and inserted into the stream at other points without detection.

AESM_CBC 

The Cipher Block Chaining (CBC) mode introduces feedback. Before each plain text block is encrypted, it is combined with the cipher text of the previous block by a bitwise exclusive OR operation. This ensures that even if the plain text contains many identical blocks, they will each encrypt to a different cipher text block. The initialization vector is combined with the first plain text block by a bitwise exclusive OR operation before the block is encrypted. If a single bit of the cipher text block is mangled, the corresponding plain text block will also be mangled. In addition, a bit in the subsequent block, in the same position as the original mangled bit, will be mangled.

AES padding type enumerator

Constructor & Destructor Documentation

sp2plib::AES::AES ( KeySize  keySize,
unsigned char *  keyBytes,
Padding  padding = AESPAD_PCKS7,
Mode  mode = AESM_CBC 
)

Creates new instance of AES class

Parameters
keySizeDefines key size for AES encryption
keyBytesKey bytes - must corespond to keySize variable
paddingThe padding algoritham to use - PCKS7 by default
modeCipher mode to use - CBC by default

Member Function Documentation

int sp2plib::AES::ChunkSize ( )

Gets basic chunk size of AES, alwaus 16

int sp2plib::AES::Crypt ( unsigned char *  src,
int  srcOffset,
int  srcLength,
unsigned char *  dst,
int  dstOffset 
)

Crypts bytes from input array and stores them into destination array. Note taht input and output may have different sizes! Use CryptedSize function to calculate space needed for output array

Parameters
srcSource array
srcOffsetIndex in source array form which to start chipering
srcLengthTotal length of data in source array for crypting
dstDestination array
dstOffsetStart index in destionation array for storing crypted data
Returns
Size of crypted data
int sp2plib::AES::CryptedSize ( int  srcSize)

Calculates size needed for crypted data array from given input data array size

Parameters
srcSizeSize of data array to crypt
Returns
Crypted array size
int sp2plib::AES::Decrypt ( unsigned char *  src,
int  srcOffset,
int  srcLength,
unsigned char *  dst,
int  dstOffset 
)

Decrypts data from input array and stores decrypted data to output array

Parameters
srcSource array containing crypted data
srcOffsetIndex in source array form which to start dechipering.
srcLengthTotal length of crypted data in source array for decrypting.
dstDestination array.
dstOffsetStart index in destionation array for storing decrypted data.
Returns
Size of decrypted data array
int sp2plib::AES::DecryptedSize ( unsigned char *  cryptedSrc,
int  cryptedOff,
int  cryptedLen 
)

Calculates size needed for decryped data array

Parameters
cryptedSrcCrypted data array
cryptedOffStart index of cryped data in array
cryptedLenTotal length of crypted data in array
Returns
.

Member Data Documentation

unsigned char sp2plib::AES::CBCInitialVector[16]

Gets or sets the cbc initial vector used for bitwise exclusive OR operation with first block. By default if AES is in CBC mode it will be initaly set to value generated form key


The documentation for this class was generated from the following file: