首页
社区
课程
招聘
[原创]高强度随机密码生成C++实现
发表于: 2015-10-29 20:35 7773

[原创]高强度随机密码生成C++实现

2015-10-29 20:35
7773
看到有人贴了一个随机密码生成的应用,我贴下以前自己写的高强度随机密码生成C++版本的源代码吧,这垃圾玩意写的戳,放在微云上好久了,没啥用,今天看到有人贴了(分享一个随机密码生成应用),也贴出来吧。

password.h
#ifndef PASSWORD_PASSWORD_GEN_H_
#define PASSWORD_PASSWORD_GEN_H_
//////////////////////////////////////////////////////////////////////////
#include "password/basictypes.h"
//////////////////////////////////////////////////////////////////////////
namespace password{
	enum PasswordGenLength{
		kPassword32Bit = 32,
		kPassword16Bit = 16
	};
	class WIN_DLL_API PasswordGen
	{
	public:
		static PasswordGen* GetInterface(bool free_exit = false);
		virtual void DIAllocate();
		virtual void DIRelease();
		char* PasswordGenerator(const unsigned long length);
		bool IsValidPassword(const unsigned long length);
	private:
		PasswordGen(void);
		~PasswordGen(void);
		char* password_;
		DISALLOW_EVIL_CONSTRUCTORS(PasswordGen);
	};
}

#endif


password.cc
#include "password/password_gen.h"

namespace password{
	static const unsigned char password_table[] = {
		0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x3C, 0x3E, 0x40, 0x6A, 
		0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 
		0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x41, 0x42, 0x43, 0x44, 
		0x45, 0x46, 0x47, 0x48, 0x25, 0x26, 0x2F, 0x4B, 0x4C, 0x4D, 
		0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 
		0x59, 0x5A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 
		0x38, 0x39, 0x5E, 0x21, 0x24, 0x49, 0x4A, 0x28, 0x22, 0x4E,
		0x29, 0x3D, 0x3F, 0x3F, 0x5C, 0x2A, 0x2B, 0x27, 0x23, 0x3B, 
		0x2C, 0x3A, 0x2E, 0x5F, 0x2D, 0x67, 0x68, 0x69
	};
	PasswordGen* PasswordGen::GetInterface(bool free_exit){
		static PasswordGen* ref_instance;
		if(!ref_instance){
			PasswordGen* new_instance = new PasswordGen;
			if(InterlockedCompareExchangePointer(reinterpret_cast<PVOID*>(&ref_instance),new_instance,NULL)){
				delete new_instance;
			}
		}
		if(free_exit){
			delete ref_instance;
			ref_instance = NULL;
		}
		return ref_instance;
	}
	void PasswordGen::DIAllocate(){
		password_ = new char[kMaxTempLength];
		memset(password_,0,kMaxTempLength);
	}
	void PasswordGen::DIRelease(){
		if(password_){
			delete[] password_;
			password_ = NULL;
		}
	}
	char* PasswordGen::PasswordGenerator(const unsigned long length){
		srand(static_cast<unsigned int>(time(NULL)));
		for(unsigned int i=0;i<length;i++){
			int rand_position = (static_cast<int>(rand()%sizeof(password_table)-1));
			password_[i] = password_table[static_cast<int>(floor(static_cast<long double>(rand_position)))];
			while(i>0&&i<length&&isupper(password_[i-1])&&isupper(password_[i])){
				rand_position = (static_cast<int>(rand()%sizeof(password_table)-1));
				password_[i] = password_table[static_cast<int>(floor(static_cast<long double>(rand_position)))];
			}
			while(i>0&&i<length&&islower(password_[i-1])&&islower(password_[i])){
				rand_position = (static_cast<int>(rand()%sizeof(password_table)-2));
				password_[i] = password_table[static_cast<int>(floor(static_cast<long double>(rand_position)))];
			}
			while(i>0&&i<length&&isdigit(password_[i-1])&&isdigit(password_[i])){
				rand_position = (static_cast<int>(rand()%sizeof(password_table)-3));
				password_[i] = password_table[static_cast<int>(floor(static_cast<long double>(rand_position)))];
			}
			while(i>0&&i<length&&ispunct(password_[i-1])&&ispunct(password_[i])){
				rand_position = (static_cast<int>(rand()%sizeof(password_table)-4));
				password_[i] = password_table[static_cast<int>(floor(static_cast<long double>(rand_position)))];
			}
		}
		return password_;
	}
	bool PasswordGen::IsValidPassword(const unsigned long length){
		return (strlen(password_)==length);
	}
	PasswordGen::PasswordGen(void):password_(NULL){
		DIRelease();
	}
	PasswordGen::~PasswordGen(void){
		DIRelease();
	}
}


BTW:这垃圾玩意真的没啥用,掖着藏着干啥啊。。。。。。没搞明白。有点直接,抱歉。

[课程]Linux pwn 探索篇!

收藏
免费 3
支持
分享
最新回复 (2)
雪    币: 275
活跃值: (320)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
(⊙v⊙)嗯,其实加解密啥的随便写一个就可以。。。只要你不把程序给别人,估计他们也逆不出加密方法。真的没啥需要保密的哈哈。(前提是不给源程序)
2015-10-29 21:54
0
雪    币: 36
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
感觉好简单的样子
2016-9-9 18:08
0
游客
登录 | 注册 方可回帖
返回
//