📄 cfb.cpp
字号:
/************************************************** CFB Mode Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/cfb.h>namespace Botan {/************************************************** CFB Encryption Constructor **************************************************/CFB_Encryption::CFB_Encryption(const std::string& cipher_name, const SymmetricKey& key, const BlockCipherModeIV& iv, u32bit fback) : BlockCipherMode(cipher_name, "CFB_Encryption", key, iv), FEEDBACKSIZE(fback ? fback : BLOCK_SIZE) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); if(FEEDBACKSIZE == 0 || FEEDBACKSIZE > BLOCK_SIZE) throw Invalid_Argument(name() + ": Invalid feedback size"); cipher->encrypt(state, buffer); }/************************************************** Encrypt data in CFB mode **************************************************/void CFB_Encryption::write(const byte input[], u32bit length) { while(length) { u32bit xored = std::min(FEEDBACKSIZE - position, length); xor_buf(buffer + position, input, xored); send(buffer + position, xored); input += xored; length -= xored; position += xored; if(position == FEEDBACKSIZE) feedback(); } }/************************************************** Do the feedback **************************************************/void CFB_Encryption::feedback() { for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACKSIZE; j++) state[j] = state[j + FEEDBACKSIZE]; state.copy(BLOCK_SIZE - FEEDBACKSIZE, buffer, FEEDBACKSIZE); cipher->encrypt(state, buffer); position = 0; }/************************************************** CFB Decryption Constructor **************************************************/CFB_Decryption::CFB_Decryption(const std::string& cipher_name, const SymmetricKey& key, const BlockCipherModeIV& iv, u32bit fback) : BlockCipherMode(cipher_name, "CFB_Decryption", key, iv), FEEDBACKSIZE(fback ? fback : BLOCK_SIZE) { if(!valid_iv_length(iv.length())) throw Invalid_IV_Length(name(), iv.length()); if(FEEDBACKSIZE == 0 || FEEDBACKSIZE > BLOCK_SIZE) throw Invalid_Argument(name() + ": Invalid feedback size"); cipher->encrypt(state, buffer); }/************************************************** Decrypt data in CFB mode **************************************************/void CFB_Decryption::write(const byte input[], u32bit length) { while(length) { u32bit xored = std::min(FEEDBACKSIZE - position, length); xor_buf(buffer + position, input, xored); send(buffer + position, xored); buffer.copy(position, input, xored); input += xored; length -= xored; position += xored; if(position == FEEDBACKSIZE) feedback(); } }/************************************************** Do the feedback **************************************************/void CFB_Decryption::feedback() { for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACKSIZE; j++) state[j] = state[j + FEEDBACKSIZE]; state.copy(BLOCK_SIZE - FEEDBACKSIZE, buffer, FEEDBACKSIZE); cipher->encrypt(state, buffer); position = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -