[英]Emulate C# Random() in C++ (Same Numbers)
有沒有辦法在C ++中實現C#Random()類? 我特別需要根據給定的種子生成相同的數字序列。
場景:我正在努力通過利用他們在C#中使用Random()來“破解”幾個加密惡意軟件來生成密鑰。 顯然,只有2 ^ 32個可能的密鑰,~4.3B密鑰,這是可能的猜測范圍。 我在C#中編寫了強力執行器,但無論我優化多少,它們都相當慢。 我想用C ++實現一個bruteforcer以獲得最佳效率(“更接近硬件”),因為我可以通過解密部分獲得更好的速度優化(例如,AES-256通常甚至可以在未來利用GPU) ,並以指數方式獲得更好的產出。
顯然,隨機(種子)!= srand(種子),基於不同的生成器。 有沒有辦法在C ++中實現PRNG C#用途? 我顯然無法修改C#惡意軟件,因為已經對受害者的文件進行了加密,因此我不能只是“重寫兩者以使用相同的常見RNG”。
您可以在此處查看Random(在c#中)的源代碼。
感謝大家的回答和評論。 我在這里發布我的移植C ++代碼,如果其他人需要它來進行類似的項目。 這是相當復制/粘貼,只需要“翻譯”幾行,並將其分解為適當的原型。 確認並排生成與C#應用程序完全相同的數字序列。 :)
Random.h
#include <limits>
#pragma once
class Random
{
private:
const int MBIG = INT_MAX;
const int MSEED = 161803398;
const int MZ = 0;
int inext;
int inextp;
int *SeedArray = new int[56]();
double Sample();
double GetSampleForLargeRange();
int InternalSample();
public:
Random(int seed);
~Random();
int Next();
int Next(int minValue, int maxValue);
int Next(int maxValue);
double NextDouble();
};
Random.cpp
#include "stdafx.h"
#include "Random.h"
#include <limits.h>
#include <math.h>
#include <stdexcept>
double Random::Sample() {
//Including this division at the end gives us significantly improved
//random number distribution.
return (this->InternalSample()*(1.0 / MBIG));
}
int Random::InternalSample() {
int retVal;
int locINext = this->inext;
int locINextp = this->inextp;
if (++locINext >= 56) locINext = 1;
if (++locINextp >= 56) locINextp = 1;
retVal = SeedArray[locINext] - SeedArray[locINextp];
if (retVal == MBIG) retVal--;
if (retVal<0) retVal += MBIG;
SeedArray[locINext] = retVal;
inext = locINext;
inextp = locINextp;
return retVal;
}
Random::Random(int seed) {
int ii;
int mj, mk;
//Initialize our Seed array.
//This algorithm comes from Numerical Recipes in C (2nd Ed.)
int subtraction = (seed == INT_MAX) ? INT_MAX : abs(seed);
mj = MSEED - subtraction;
SeedArray[55] = mj;
mk = 1;
for (int i = 1; i<55; i++) { //Apparently the range [1..55] is special (Knuth) and so we're wasting the 0'th position.
ii = (21 * i) % 55;
SeedArray[ii] = mk;
mk = mj - mk;
if (mk<0) mk += MBIG;
mj = SeedArray[ii];
}
for (int k = 1; k<5; k++) {
for (int i = 1; i<56; i++) {
SeedArray[i] -= SeedArray[1 + (i + 30) % 55];
if (SeedArray[i]<0) SeedArray[i] += MBIG;
}
}
inext = 0;
inextp = 21;
seed = 1;
}
Random::~Random()
{
delete SeedArray;
}
int Random::Next() {
return this->InternalSample();
}
double Random::GetSampleForLargeRange() {
int result = this->InternalSample();
// Note we can't use addition here. The distribution will be bad if we do that.
bool negative = (InternalSample() % 2 == 0) ? true : false; // decide the sign based on second sample
if (negative) {
result = -result;
}
double d = result;
d += (INT_MAX - 1); // get a number in range [0 .. 2 * Int32MaxValue - 1)
d /= 2 * INT_MAX - 1;
return d;
}
int Random::Next(int minValue, int maxValue) {
if (minValue>maxValue) {
throw std::invalid_argument("minValue is larger than maxValue");
}
long range = (long)maxValue - minValue;
if (range <= (long)INT_MAX) {
return ((int)(this->Sample() * range) + minValue);
}
else {
return (int)((long)(this->GetSampleForLargeRange() * range) + minValue);
}
}
int Random::Next(int maxValue) {
if (maxValue<0) {
throw std::invalid_argument("maxValue must be positive");
}
return (int)(this->Sample()*maxValue);
}
double Random::NextDouble() {
return this->Sample();
}
Main.cpp的
#include "Random.h"
#include <iostream>
int main(int argc, char* argv[]){
// Example usage with a given seed
Random r = Random(7898);
std::cout << r.Next() << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.