开源项目之NeoCrypt(加密解密)
2012-09-11 08:38
288 查看
一个文件加密解密软件的源代码,可以设置很多著名的加密算法进行加密解密。它集强力、安全的算法和简单、易用的界面于一身。它的文件管理界面可以轻易地观看和更改整个系 统的加密设置,也可以加/解密选定的文件,安全地删除文件。它支持多用户使用。如图:
项目需要用到第三方库cryptlib,该库已经放到源码包中。
本程序有一个动态链接库NeoCryptSX与一个主程序Neocrypt组成,NeoCryptSX动态库操作简单,只负责发送调用消息,Neocrypt主程序中封装了Decryptor类负责解码、Encryptor类负责编码,File类负责文件操作、CProgUpdater类负责进度等。
解码部分:
编码部分:
学习的目的是成熟!~
源码下载,喜欢的自己研究了,
项目需要用到第三方库cryptlib,该库已经放到源码包中。
本程序有一个动态链接库NeoCryptSX与一个主程序Neocrypt组成,NeoCryptSX动态库操作简单,只负责发送调用消息,Neocrypt主程序中封装了Decryptor类负责解码、Encryptor类负责编码,File类负责文件操作、CProgUpdater类负责进度等。
解码部分:
void Decryptor::decrypt(const std::string& name,std::list<std::string>& pwds) { char* buf = new char[BUFFER_SIZE]; File file(name); // pass file size to listener if (mListener) mListener->setRange(file.size()); // check for password if (pwds.size() == 0) throw std::logic_error("No passwords specified!"); // create the envelope CRYPT_ENVELOPE env; if (cryptCreateEnvelope(&env,CRYPT_UNUSED,CRYPT_FORMAT_AUTO)) throw std::runtime_error("Failed to create envelope!"); cryptSetAttribute(env,CRYPT_ATTRIBUTE_BUFFERSIZE,BUFFER_SIZE); int n; while (!file.eof() && (n = file.read(buf,BUFFER_SIZE-4096)) != 0) { int cBytes; int bytesDone = 0; int bytesLeft = n; while (bytesLeft) { int status = cryptPushData(env,buf+bytesDone,bytesLeft,&cBytes); if ( status == CRYPT_ENVELOPE_RESOURCE) { // add passwords until one is accepted std::list<std::string>::iterator it; std::list<std::string>::iterator end = pwds.end(); for (it = pwds.begin(); it != end; ++it) { // loop through all required attributes and try the password with each if (cryptSetAttribute(env,CRYPT_ENVINFO_PASSWORD,CRYPT_CURSOR_FIRST) == CRYPT_OK) { bool accept = false; do { CRYPT_ATTRIBUTE_TYPE attrib; cryptGetAttribute(env,CRYPT_ENVINFO_PASSWORD,(int*)&attrib); if (attrib == CRYPT_ENVINFO_PASSWORD) { if (cryptSetAttributeString(env,CRYPT_ENVINFO_PASSWORD,it->c_str(),it->size()) == CRYPT_OK) { accept = true; break; } } } while (cryptSetAttribute(env,CRYPT_ENVINFO_PASSWORD,CRYPT_CURSOR_NEXT) == CRYPT_OK); if (accept) break; } } if (it == end) throw std::runtime_error("None of the passwords are valid for file " + name + " ! Please try another password."); } else if (status != 0) { throw std::runtime_error("The given file " + name + " is not a valid encrypted file."); } bytesLeft -= cBytes; bytesDone += cBytes; cryptPopData(env,buf,BUFFER_SIZE,&cBytes); file.write(buf,cBytes); } if (mListener) mListener->increment(n); } // flush remaining data cryptFlushData(env); cryptPopData(env,buf,BUFFER_SIZE,&n); file.write(buf,n); // destroy envelope cryptDestroyEnvelope(env); // close file file.close(); mListener->complete(); delete [] buf; }
编码部分:
void Encryptor::encrypt(const std::string& name,std::list<std::string>& password,Algorithm::Type algo) { char* buf = new char[BUFFER_SIZE]; File file(name); // pass file size to listener if (mListener) mListener->setRange(file.size()); // check for at-least one password if (password.size() == 0) throw std::logic_error("No password in password list!"); // set algorithm type if (cryptSetAttribute(CRYPT_UNUSED,CRYPT_OPTION_ENCR_ALGO,algo)) throw std::runtime_error("Could not set algorithm type!"); // create the envelope CRYPT_ENVELOPE env; if (cryptCreateEnvelope(&env,CRYPT_UNUSED,CRYPT_FORMAT_CRYPTLIB)) throw std::runtime_error("Failed to create envelope!"); std::list<std::string>::iterator i; std::list<std::string>::iterator e = password.end(); // add password(s) for (i = password.begin(); i != e; ++i) { if (cryptSetAttributeString(env,CRYPT_ENVINFO_PASSWORD,(*i).c_str(),(*i).size())) throw std::runtime_error("Failed to set password " + (*i) + "!"); } // allocate encryption buffer if (cryptSetAttribute(env,CRYPT_ATTRIBUTE_BUFFERSIZE,BUFFER_SIZE)) throw std::runtime_error("Could not allocate encryption buffer!"); int n; int cBytes; if (!file.eof() && (n = file.read(buf,100)) != 0) { cryptPushData(env,buf,n,&cBytes); cryptPopData(env,buf,BUFFER_SIZE,&cBytes); file.write(buf,cBytes); if (mListener) mListener->increment(n); } while (!file.eof() && (n = file.read(buf,BUFFER_SIZE-4096)) != 0) { cryptPushData(env,buf,n,&cBytes); cryptPopData(env,buf,BUFFER_SIZE,&cBytes); file.write(buf,cBytes); if (mListener) mListener->increment(n); } // flush remaining data cryptFlushData(env); cryptPopData(env,buf,BUFFER_SIZE,&cBytes); file.write(buf,cBytes); // destroy envelope cryptDestroyEnvelope(env); // close file file.close(); mListener->complete(); delete [] buf; }
学习的目的是成熟!~
源码下载,喜欢的自己研究了,
相关文章推荐
- 开源加密解密库比较
- 【强烈推荐】开源C#工具类MSCL系列(八)Encrypt加密解密帮助类
- 基于Qt5.x的QCA加解密开源项目的编译过程
- javaee加密部署项目通过tomcat使用自定义的classload解密
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理
- 大三软件工程小项目-小技术集合-3DES加密及解密
- 寻找开源项目----让ksoap支持wsse加密的soap报文
- 项目中 加密解密某些字段的问题汇总
- 项目中用到的加密解密工具类
- MVC项目实践,在三层架构下实现SportsStore-10,连接字符串的加密和解密
- 开源加密解密库比较
- ios项目小结---base64加密解密
- 项目升级-加密的参数传递到后台然后解密(相当于重新封装下request)
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理(续)
- 读取项目中properties文件中的账号密码进行加密解密
- Base64加密和解密解决方案(个人项目经验)
- 开源加密解密库比较
- sqlite3对数据库加密用c语言开源项目wxSQLite3实现
- 开源加密解密库比较
- 开源加密解密库比较