void SPAuthCheckPrv::ReadCheckFile()
{
	// Read check prevention file
	mGameSecondsBeforeNextCheck = 1; // default -> check

	try
	{
		FileReader aReader;
		if(!aReader.Open(gSPAuthCheck_FileName))
			return;

		unsigned short aNumBytes = aReader.ReadShort();
		if(aNumBytes==0 || aReader.Available()<aNumBytes)
			return;

		char *aBuf = new char[aNumBytes];
		std::auto_ptr<char> aDelBuf(aBuf);

		aReader.ReadBytes(aBuf,aNumBytes);
		aReader.Close();

		ByteBufferPtr aDecrypt = mEncryptKey.Decrypt(aBuf,aNumBytes);
		if(aDecrypt.get()==NULL)
			return;

		WONFile aFile(gSPAuthCheck_FileName);

		ReadBuffer aReadBuf(aDecrypt->data(),aDecrypt->length());		
		std::string aSig;
		aReadBuf.ReadString(aSig);
		if(aSig!="magic")
			return;

		time_t aCreateTime = aFile.GetCreateTime();
		time_t aCompareCreateTime = aReadBuf.ReadLong();
		if(aCompareCreateTime != aCreateTime)
			return;

		mGameSecondsBeforeNextCheck = aReadBuf.ReadLong();
		if(mGameSecondsBeforeNextCheck==0) // don't remove file in this case
			return;
		
		aFile.Remove();

	}
	catch(FileReaderException&)
	{
	}
	catch(ReadBufferException&)
	{
	}
}
示例#2
0
int main( int argc, char* argv[] ){
    Blowfish b;

    if (argc != 2) {
        cout << "usage: Profile <num-iterations>" << endl;
        return 0;
    }

    string str(argv[1]);
    istringstream is(str);
    uint32_t numIter = 0;
    is >> numIter;

    uint8_t* key = new uint8_t[ b.keySize() ];
    
    for( unsigned int i = 0; i < b.keySize(); i++ ){
        key[ i ] = 0x00;
    }
    
    time_t start = time(NULL) * 1000;
    print_uint8_hex(key, b.keySize(), "Key: ");
    b.setKey( key );


    uint8_t* text;// = new uint8_t[ b.blockSize() ];
    for (unsigned int i = 0; i < numIter; i++) {
        text = new uint8_t[ b.blockSize() ];
    
        for( unsigned int i = 0; i < b.blockSize(); i++ ){
            text[ i ] = 0x00;
        }

        b.encrypt( text );
        delete[] text;
    }
    time_t end = time(NULL) * 1000;

    cout << "Running Time: " << (end - start) << " msec" << endl;
    //print_uint8_hex(text, b.blockSize(), "Plaintext");
    //print_uint8_hex(text, b.blockSize(), "Ciphertext");
    
}
void SPAuthCheckPrv::WriteCheckFile()
{
	
	if(mGameSecondsBeforeNextCheck==1) // Remove file to force check next time
	{
		WONFile aFile(gSPAuthCheck_FileName);
		aFile.Remove();
		return;
	}

	// Write new file
	try
	{
		FileWriter aWriter;
		if(!aWriter.Open(gSPAuthCheck_FileName))
			return;

		WONFile aFile(gSPAuthCheck_FileName);

		WriteBuffer anEncrypt;
		anEncrypt.AppendString("magic");
		anEncrypt.AppendLong(aFile.GetCreateTime()); // file creation time
		anEncrypt.AppendLong(mGameSecondsBeforeNextCheck);

		ByteBufferPtr aBuf = mEncryptKey.Encrypt(anEncrypt.data(),anEncrypt.length());
		if(aBuf.get()==NULL)
			return;

		aWriter.WriteShort(aBuf->length());
		aWriter.WriteBytes(aBuf->data(),aBuf->length());
	}
	catch(FileWriterException&)
	{
	}


}
示例#4
0
int main(int argc, char** argv) {
  if (argc != 3) {
    fprintf(stderr, "exef2paz v1.22 by asmodean\n\n");
    fprintf(stderr, "usage: %s <input.paz> <game index>\n\n", argv[0]);
    fprintf(stderr, "\t 0 = ef - the latter tale\n", argv[0]);
    fprintf(stderr, "\t 1 = eden*\n", argv[0]);
    return -1;
  }

  string        in_filename(argv[1]);
  unsigned long game_index = atol(argv[2]);

  if (game_index >= GAME_COUNT) {
    fprintf(stderr, "Unknown game index: %d\n", game_index);
    return -1;
  }

  string prefix(as::stringtol(as::get_file_prefix(in_filename, true)));

  game_info_t game_info = GAME_INFO[game_index];
  key_info_t  keys;

  for (unsigned long i = 0; !game_info.keys[i].prefix.empty(); i++)
  {
    if (prefix == game_info.keys[i].prefix)
    {
      keys = game_info.keys[i];
    }
  }

  if (keys.prefix.empty()) {
    fprintf(stderr, "%s: don't know encryption key.\n", in_filename.c_str());
    return -1;
  }

  // Hard to recognize individual sound files because they lack extension
  bool is_audio = prefix == "bgm" || prefix == "se" || prefix == "voice" || prefix == "PMvoice";
  bool is_mov   = prefix == "mov";

  int fd = as::open_or_die(in_filename, O_RDONLY | O_BINARY);  

  PAZHDR hdr;
  read(fd, &hdr, sizeof(hdr));  

  char simple_key = hdr.toc_len >> 24;

  lseek(fd, 0, SEEK_SET);
  read_unobfuscate(fd, simple_key, &hdr, sizeof(hdr));  

  Blowfish bf;  

  unsigned char* toc_buff = new unsigned char[hdr.toc_len];
  read_unobfuscate(fd, simple_key, toc_buff, hdr.toc_len);
  bf.Set_Key(keys.toc_key, sizeof(keys.toc_key));
  bf.Decrypt(toc_buff, hdr.toc_len);  

  PAZHDR2*       hdr2 = (PAZHDR2*) toc_buff;
  unsigned char* p    = (unsigned char*) (hdr2 + 1);

  unsigned char* mov_seed = NULL;

  if (is_mov)
  {
    mov_seed = p;
    p += 256;
  }

  for (unsigned long i = 0; i < hdr2->entry_count; i++)
  {
    string filename = (char*) p;
    p += filename.length() + 1;

    PAZENTRY* entry = (PAZENTRY*) p;
    p += sizeof(*entry);    

    unsigned long  len  = entry->padded_length;
    unsigned char* buff = new unsigned char[len];
    lseek(fd, entry->offset, SEEK_SET);
    read_unobfuscate(fd, simple_key, buff, len);

    string crud;

    if (filename.find(".png") != string::npos)
    {
      crud = game_info.crud.png;
    }
    else if (filename.find(".ogg") != string::npos || is_audio)
    {
      crud = game_info.crud.ogg;
    }
    else if (filename.find(".sc") != string::npos)
    {
      crud = game_info.crud.sc;
    }
    else if (filename.find(".avi") != string::npos)
    {
      crud = game_info.crud.avi;
    }

    string seed = as::stringtol(filename) + as::stringf(" %08X ", entry->length) + crud;

    if (is_mov)
    {
      decrypt_mov(mov_seed, seed, buff, len);
    }
    else
    {
      bf.Set_Key(keys.dat_key, sizeof(keys.dat_key));
      bf.Decrypt(buff, len);

      if (!crud.empty()) {
        unobfuscate3(seed, buff, len);
      }
    }

    as::make_path(filename);
    as::write_file(filename, buff, entry->length);

    delete [] buff;
  }

  delete [] toc_buff;

  close(fd);

  return 0;
}
bool Import_PwManager::importDatabase(QWidget* GuiParent, IDatabase* db){	
	database=db;
	QFile* file=openFile(GuiParent,identifier(),QStringList()<<tr("PwManager Files (*.pwm)")<<tr("All Files (*)"));
	if(!file)return false;
	QString password=getPassword(GuiParent);
	if(password.isEmpty()){delete file; return false;}
	char* buffer=NULL;
	int offset=0;
	int len=0;
	if((len=file->size())){
		buffer=new char[len];
	}
	else {
		QMessageBox::critical(GuiParent,tr("Import Failed"),tr("File is empty."));
		delete file;
		return false;
	}
	file->read(buffer,len);
	file->close();
	delete file;
	if(QString::fromAscii(buffer,17)!="PWM_PASSWORD_FILE")
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("File is no valid PwManager file.")); return false;}
	offset+=17;
	if(buffer[offset]!=0x05)
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Unsupported file version.")); return false;}
	offset++;
	if(buffer[offset]!=0x01)
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Unsupported hash algorithm.")); return false;}
	offset++;
	if(buffer[offset]!=0x01)
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Unsupported hash algorithm.")); return false;}
	offset++;
	if(buffer[offset]!=0x01)
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Unsupported encryption algorithm.")); return false;}
	offset++;
	if(buffer[offset]==0x00)Compression=0;
	if(buffer[offset]==0x01)Compression=1;
	if(buffer[offset]==0x02)Compression=2;
	///@TODO Compression
	if(buffer[offset])
	{QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Compressed files are not supported yet.")); return false;}
	offset++;
	if(buffer[offset]==0x00)KeyFlag=true;
	else KeyFlag=false;
	offset++;
	//Reserved Bytes (64)
	offset+=64;
	memcpy(KeyHash,buffer+offset,20);
	offset+=20;
	memcpy(DataHash,buffer+offset,20);
	offset+=20;
	
	Blowfish blowfish;
	int pwlen=password.length();
	byte* Key=new byte[pwlen];
	byte* xml=new byte[len-offset+1];
	xml[len-offset]=0;
	memcpy(Key,password.toAscii(),pwlen);
	QCryptographicHash sha(QCryptographicHash::Sha1);
	sha.addData((const char*)Key,pwlen);
	QByteArray key_hash = sha.result();
	if(memcmp(key_hash.constData(),KeyHash,20)){
		delete[] Key;
		delete [] buffer;
		QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Wrong password."));
		return false;
	}
	blowfish.bf_setkey(Key,password.length());
	blowfish.bf_decrypt(xml,(byte*)buffer+offset,len-offset);
	delete [] Key;
	delete [] buffer;
	sha.reset();
	sha.addData((const char*)xml,strlen((char*)xml)-1);
	QByteArray content_hash = sha.result();
	if(memcmp(content_hash.constData(),DataHash,20)){
		delete [] xml;
		QMessageBox::critical(GuiParent,tr("Import Failed"),tr("File is damaged (hash test failed)."));
		return false;
	}
	
	if(!parseXmlContent((char*)xml)){
		delete [] xml;
		QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Invalid XML data (see stdout for details).")); return false;
	}
	return true;
}
示例#6
-1
int main()
{
	string str_src = "abcdefghijklmnopq";
	string str_key = "key";
	vector<char> key;
	for (auto it = str_key.begin(); it != str_key.end(); it++)
	{
		key.push_back(*it);
	}

	vector<char> input;
	vector<char> output;

	Blowfish *tool = new Blowfish(key);	//设置密钥
	for (auto it : str_src)
	{
		input.push_back(it);
	}
	output = tool->Encrypt(input);	//加密后字串
	
	//解密没有直接处理string的方法,只有vector<char>的方法
	
	input.clear();
	for (auto it = output.begin(); it != output.end(); it++)
	//等效于
	//for (auto it : output)
	{
		input.push_back(*it);
	}

	output.clear();
	output = tool->Decrypt(input);
	return 0;
}