⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptlib.h

📁 一个DES,RSA,MD5,RC4等加密算法的源码
💻 H
📖 第 1 页 / 共 3 页
字号:
		//! input multiple bytes
		virtual void Put(const byte *inString, unsigned int length) =0;

		//! input a 16-bit word, big-endian or little-endian depending on highFirst
		void PutWord16(word16 value, bool highFirst=true);
		//! input a 32-bit word
		void PutWord32(word32 value, bool highFirst=true);
	//@}

	//!	\name SIGNALS
	//@{
		//! process everything in internal buffers and output them
		/*! throws exception if completeFlush == true and it's
			not possible to flush everything */
		virtual void Flush(bool completeFlush, int propagation=-1);
		//! mark end of an input segment, message, or packet
		/*! propagation != 0 means pass on the signal to attached
			BufferedTransformation objects, with propagation
			decremented at each step until it reaches 0.
			-1 means unlimited propagation. */
		virtual void MessageEnd(int propagation=-1);
		//! same as Put() followed by MessageEnd() but may be more efficient
		virtual void PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
		//! mark end of a series of messages
		/*! There should be a MessageEnd immediately before MessageSeriesEnd. */
		virtual void MessageSeriesEnd(int propagation=-1);

		//! set propagation of automatically generated and transfered signals
		/*! propagation == 0 means do not automaticly generate signals */
		virtual void SetAutoSignalPropagation(int propagation) {}

		//!
		virtual int GetAutoSignalPropagation() const {return 0;}

		// for backwards compatibility
		void Close() {MessageEnd();}
	//@}

	//!	\name ERRORS
	//@{
		//! error types
		enum ErrorType {
			//! received a Flush(true) signal but can't flush buffers
			CANNOT_FLUSH,
			//! data integerity check (such as CRC or MAC) failed
			DATA_INTEGRITY_CHECK_FAILED,
			//! received input data that doesn't conform to expected format
			INVALID_DATA_FORMAT,
			//! error reading from input device
			INPUT_ERROR,
			//! error writing to output device
			OUTPUT_ERROR,
			//! some error not belong to any of the above categories
			OTHER_ERROR
		};

		//! exception thrown by BufferedTransformation
		class Err : public Exception
		{
		public:
			Err(ErrorType errorType, const std::string &s="");
			ErrorType GetErrorType() const {return m_errorType;}
			void SetErrorType(ErrorType errorType) {m_errorType = errorType;}
		private:
			ErrorType m_errorType;
		};
	//@}

	//!	\name RETRIEVAL OF ONE MESSAGE
	//@{
		//! returns number of bytes that is currently ready for retrieval
		/*! All retrieval functions return the actual number of bytes
			retrieved, which is the lesser of the request number and
			MaxRetrievable(). */
		virtual unsigned long MaxRetrievable() const;

		// old mispelled name
		unsigned long MaxRetrieveable() const {return MaxRetrievable();}

		//! returns whether any bytes are currently ready for retrieval
		virtual bool AnyRetrievable() const;

		//! try to retrieve a single byte
		virtual unsigned int Get(byte &outByte);
		//! try to retrieve multiple bytes
		virtual unsigned int Get(byte *outString, unsigned int getMax);

		//! peek at the next byte without removing it from the output buffer
		virtual unsigned int Peek(byte &outByte) const;
		//! peek at multiple bytes without removing them from the output buffer
		virtual unsigned int Peek(byte *outString, unsigned int peekMax) const;

		//! try to retrieve a 16-bit word, big-endian or little-endian depending on highFirst
		unsigned int GetWord16(word16 &value, bool highFirst=true);
		//! try to retrieve a 32-bit word
		unsigned int GetWord32(word32 &value, bool highFirst=true);

		//! try to peek at a 16-bit word, big-endian or little-endian depending on highFirst
		unsigned int PeekWord16(word16 &value, bool highFirst=true);
		//! try to peek at a 32-bit word
		unsigned int PeekWord32(word32 &value, bool highFirst=true);

		//! move transferMax bytes of the buffered output to target as input
		virtual unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax=ULONG_MAX);

		//! discard skipMax bytes from the output buffer
		virtual unsigned long Skip(unsigned long skipMax=ULONG_MAX);

		//! copy copyMax bytes of the buffered output to target as input
		virtual unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax=ULONG_MAX) const;
	//@}

	//!	\name RETRIEVAL OF MULTIPLE MESSAGES
	//@{
		//!
		virtual unsigned long TotalBytesRetrievable() const;
		//! number of times MessageEnd() has been received minus messages retrieved or skipped
		virtual unsigned int NumberOfMessages() const;
		//! returns true if NumberOfMessages() > 0
		virtual bool AnyMessages() const;
		//! start retrieving the next message
		/*!
			Returns false if no more messages exist or this message 
			is not completely retrieved.
		*/
		virtual bool GetNextMessage();
		//! skip count number of messages
		virtual unsigned int SkipMessages(unsigned int count=UINT_MAX);
		//!
		virtual unsigned int TransferMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX);
		//!
		virtual unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX) const;

		//!
		virtual void SkipAll();
		//!
		virtual void TransferAllTo(BufferedTransformation &target);
		//!
		virtual void CopyAllTo(BufferedTransformation &target) const;
	//@}

	//!	\name CHANNELS
	//@{
		virtual void ChannelPut(const std::string &channel, byte inByte);
		virtual void ChannelPut(const std::string &channel, const byte *inString, unsigned int length);

		void ChannelPutWord16(const std::string &channel, word16 value, bool highFirst=true);
		void ChannelPutWord32(const std::string &channel, word32 value, bool highFirst=true);

		virtual void ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1);
		virtual void ChannelMessageEnd(const std::string &channel, int propagation=-1);
		virtual void ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation=-1);
		virtual void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1);

		virtual void SetRetrievalChannel(const std::string &channel);

		static const std::string NULL_CHANNEL;
	//@}

	/*!	\name ATTACHMENT
		Some BufferedTransformation objects (e.g. Filter objects)
		allow other BufferedTransformation objects to be attached. When
		this is done, the first object instead of buffering its output,
		sents that output to the attached object as input. The entire
		attachment chain is deleted when the anchor object is destructed.
	*/
	//@{
		//! returns whether this object allows attachment
		virtual bool Attachable() {return false;}
		//! returns the object immediately attached to this object or NULL for no attachment
		virtual BufferedTransformation *AttachedTransformation() {return 0;}
		//!
		virtual const BufferedTransformation *AttachedTransformation() const
			{return const_cast<BufferedTransformation *>(this)->AttachedTransformation();}
		//! delete the current attachment chain and replace it with newAttachment
		virtual void Detach(BufferedTransformation *newAttachment = 0) {}
		//! add newAttachment to the end of attachment chain
		virtual void Attach(BufferedTransformation *newAttachment);
	//@}
};

//! abstract base class for public-key encryptors and decryptors

/*! This class provides an interface common to encryptors and decryptors
	for querying their plaintext and ciphertext lengths.
*/
class PK_CryptoSystem
{
public:
	//!
	virtual ~PK_CryptoSystem() {}

	//! maximum length of plaintext for a given ciphertext length
	//* This function returns 0 if cipherTextLength is not valid (too long or too short).
	virtual unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const =0;

	//! calculate length of ciphertext given length of plaintext
	//* This function returns 0 if plainTextLength is not valid (too long).
	virtual unsigned int CipherTextLength(unsigned int plainTextLength) const =0;
};

//! abstract base class for public-key encryptors

/*! An encryptor is also a public encryption key.  It contains both the
	key and the algorithm to perform the encryption.
*/
class PK_Encryptor : public virtual PK_CryptoSystem
{
public:
	//! encrypt a byte string
	/*! Preconditions:
			\begin{itemize}
			\item CipherTextLength(plainTextLength) != 0 (i.e., plainText isn't too long)
			\item size of cipherText == CipherTextLength(plainTextLength)
			\end{itemize}
	*/
	virtual void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) =0;
};

//! abstract base class for public-key decryptors

/*! An decryptor is also a private decryption key.	It contains both the
	key and the algorithm to perform the decryption.
*/
class PK_Decryptor : public virtual PK_CryptoSystem
{
public:
	//! decrypt a byte string, and return the length of plaintext
	/*! Precondition: size of plainText == MaxPlainTextLength(cipherTextLength)
		bytes.

		The function returns the actual length of the plaintext, or 0
		if decryption fails.
	*/
	virtual unsigned int Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText) =0;
};

//! abstract base class for encryptors and decryptors with fixed length ciphertext

/*! A simplified interface (as embodied in this
	class and its subclasses) is provided for crypto systems (such
	as RSA) whose ciphertext length depend only on the key, not on the length
	of the plaintext.  The maximum plaintext length also depend only on
	the key.
*/
class PK_FixedLengthCryptoSystem : public virtual PK_CryptoSystem
{
public:
	//!
	virtual unsigned int MaxPlainTextLength() const =0;
	//!
	virtual unsigned int CipherTextLength() const =0;

	unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const;
	unsigned int CipherTextLength(unsigned int plainTextLength) const;
};

//! abstract base class for encryptors with fixed length ciphertext

class PK_FixedLengthEncryptor : public virtual PK_Encryptor, public virtual PK_FixedLengthCryptoSystem
{
};

//! abstract base class for decryptors with fixed length ciphertext

class PK_FixedLengthDecryptor : public virtual PK_Decryptor, public virtual PK_FixedLengthCryptoSystem
{
public:
	//! decrypt a byte string, and return the length of plaintext
	/*! Preconditions:
			\begin{itemize}
			\item length of cipherText == CipherTextLength()
			\item size of plainText == MaxPlainTextLength()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -