AES Manual

内容表

关于

An Advanced Encryption Standard (AES, also known as Rijndael) class to encrypt/decrypt data.

警告
For encryption using the MAXON API see Stream Conversions Manual .
// This example demonstrates the encryption and decryption of a string.

// Some plain text to encrypt. const Char plainText[] = "O brave new world / That has such people in't!" ; const Int32 length = sizeof (plainText);

ApplicationOutput ( "Plain Text: @" , maxon::String { plainText });

// Declare secret key and encryption block size. const Int32 blockSize = 256; // in bits (either 128, 192 or 256) const Char key[32] = { '7' , 'c' , '3' , '0' , 'e' , '0' , '0' , 'b' , 'b' , '6' , '2' , '7' , '1' , '4' , '9' , '5' }; // either 16, 24 or 32 bytes const Int32 keyLength = sizeof (key) * 8; // in bits (either 128, 192 or 256) DebugAssert ((keyLength == 128) || (keyLength == 192) || (keyLength == 256));

// Memory to store the encrypted data. void * buffer = nullptr ; Int encryptedSize = 0;

// Encrypt data. { // Get size of the memory needed to store the encrypted data. encryptedSize = AES::CalcEncryptedDataSize (blockSize, length); DebugAssert (encryptedSize >= length); // Note: encrytedSize is always equal or larger than unencrypted size.

// Allocate needed memory. buffer = NewMem ( char , encryptedSize) iferr_return ; // No need to clear memory here, will be filled with random data next.

// Fill buffer with random content for increased security (the buffer part behind plainText is also input to the encryption). maxon::Block<UChar> memBlock(( UChar *)buffer, encryptedSize); maxon::SecureRandomProvider srp = maxon::SecureRandom::GetDefaultProvider (); if (! maxon::SecureRandom::GetRandomNumber (srp, memBlock)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );

// Copy the given text into the memory. CopyMem (plainText, buffer, length);

AutoAlloc<AES> aes; if (aes && aes-> Init (blockSize, keyLength)) { aes-> Encrypt (buffer, encryptedSize, key); } }

// Decrypt data again. { AutoAlloc<AES> aes; if (buffer && aes && aes-> Init (blockSize, keyLength)) { if (aes-> Decrypt (buffer, encryptedSize, key)) { const maxon::String text { ( char *)buffer }; ApplicationOutput ( "Plain Text: @" , text); } } } DeleteMem (buffer);

创建

AES objects are created with the usual tools, see Entity Creation and Destruction Manual (Classic) .

使用

Encryption / Decryption

延伸阅读

maxon::SecureRandom::GetDefaultProvider
static MAXON_METHOD SecureRandomProvider GetDefaultProvider()
Int
maxon::Int Int
定义: ge_sys_math.h:62
AES::Init
Bool Init(Int32 lBlockLength, Int32 lKeyLength)
maxon::String
定义: string.h:1197
iferr_return
#define iferr_return
定义: resultbase.h:1434
AES::CalcEncryptedDataSize
static Int CalcEncryptedDataSize(Int32 lBlockLength, Int lDataLength)
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
UChar
maxon::UChar UChar
定义: ge_sys_math.h:55
maxon::SecureRandom::GetRandomNumber
static MAXON_METHOD Bool GetRandomNumber(SecureRandomProvider provider, const Block< Byte > &buffer)
maxon::DeleteMem
void DeleteMem(T *&p)
定义: defaultallocator.h:258
AES::Encrypt
Bool Encrypt(void *pData, Int lDataLength, const void *pKey)
Int32
maxon::Int32 Int32
定义: ge_sys_math.h:58
ApplicationOutput
#define ApplicationOutput(formatString,...)
定义: debugdiagnostics.h:207
AutoAlloc
定义: ge_autoptr.h:36
NewMem
#define NewMem(T, cnt)
定义: defaultallocator.h:196
AES::Decrypt
Bool Decrypt(void *pData, Int lDataLength, const void *pKey)
DebugAssert
#define DebugAssert(condition,...)
定义: debugdiagnostics.h:245
maxon::BaseRef
定义: apibase.h:1522
Char
maxon::Char Char
定义: ge_sys_math.h:54
CopyMem
void CopyMem(const void *s, void *d, Int size)
定义: c4d_memory.h:66
maxon::Block
定义: apibase.h:300

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1