我正在編程保險絲文件系統,我有一個問題。禁用AES密碼對象上的PKCS#7填充?
我使用CBC AES對磁盤中的數據進行加密。問題是填充。當密碼的大小例如是15個字節時是沒有問題的,因爲它增加了1個字節。問題是,當我試圖加密4096字節時,它也增加了16個字節的Padd,這對我來說是失敗的。我不知道爲什麼要添加填充,因爲4096是128的倍數(size aes block)。我需要修改我的C代碼說openssl,只有當它將是必要時添加填充,但不總是...
我知道,如果明文不是128的倍數,它將添加填充。但爲什麼不呢?我能做什麼?
這裏我密碼:
int encrypt_data(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext, int algorithm_pos)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_EncryptInit_ex(ctx, ciphers[algorithm_pos].algorithm(), NULL, key, iv))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
CBC模式只提供保密性,你通常要添加MAC安全使用CBC模式。您應該使用經過身份驗證的加密,因爲它提供了*機密性和真實性。請參閱OpenSSL wiki上的[EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)。 – jww