📄 arc4.cpp
字号:
/************************************************** ARC4 Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/arc4.h>namespace Botan {/************************************************** Generate ARC4 Cipher Stream **************************************************/void ARC4::cipher(const byte in[], byte out[], u32bit length) { while(length >= buffer.size() - position) { xor_buf(out, in, buffer + position, buffer.size() - position); length -= (buffer.size() - position); in += (buffer.size() - position); out += (buffer.size() - position); generate(); } xor_buf(out, in, buffer + position, length); position += length; }/************************************************** Generate the internal state **************************************************/void ARC4::generate() { for(u32bit j = 0; j != buffer.size(); j++) { X++; Y += state[X % 256]; std::swap(state[X % 256], state[Y % 256]); buffer[j] = (byte)state[(state[X % 256] + state[Y % 256]) % 256]; } position = 0; }/************************************************** ARC4 Key Schedule **************************************************/void ARC4::key(const byte key[], u32bit length) { clear(); for(u32bit j = 0; j != 256; j++) state[j] = j; for(u32bit j = 0, state_index = 0; j != 256; j++) { state_index = (state_index + key[j % length] + state[j]) % 256; std::swap(state[j], state[state_index]); } X = Y = 0; for(u32bit j = 0; j <= SKIP; j += buffer.size()) generate(); position += (SKIP % buffer.size()); }/************************************************** Return the name of this type **************************************************/std::string ARC4::name() const { if(SKIP == 256) return "MARK-4"; return "ARC4"; }/************************************************** Clear memory of sensitive data **************************************************/void ARC4::clear() throw() { state.clear(); buffer.clear(); position = X = Y = 0; }/************************************************** ARC4 Constructor **************************************************/ARC4::ARC4(u32bit s) : StreamCipher(1, 32), SKIP(s) { clear(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -