2011-12-20 56 views
-3

所以我提出,從ISAAC輸出要求輸入密碼字符串,並用它來種子ISAAC我在做什麼導致這個程序的輸出不一致?

然後選出要加密的字符串和XOR的一項基本XOR加密程序。 由於某些原因,這對於相同的兩個字符串會產生不穩定的結果。 這是我的代碼問題,還是它被輸出到控制檯? 我在做些什麼讓它顯而易見? 這裏是我的代碼,我使用的ISAAC的網站提供的模板類:

#include <iostream> 
#include <cstdlib> 
using std::malloc; 
using std::free; 
#include "isaac.hpp" 
#include <time.h> 
#include <cmath> 
#include <string> 
#include <sstream> 

using namespace std; 

QTIsaac<8,int> derp; 



char trand(){ 
    int rando=0; 
    rando=derp.rand(); 
    rando=abs(rando); 
    rando=rando%256; 
    char re=rando; 
    return re; 
} 

int main() 
{ 
    cout << "What is your password string?\n"; 
    string get; 
    getline(cin,get); 
    int length=get.size(); 
    int seedWithMe[length]; 
    for(int i=0;i<length;i++){ 
     seedWithMe[i]=(int)get[i]; 
    } 
    derp.srand(1,1,1,seedWithMe); 
    cout << "What is your string to be encrypted? \n"; 
    getline(cin,get); 
    length=get.size(); 
    char table[length]; 
    char table3[length]; 
    for(int i=0;i<length;i++){ 
     table[i]=trand(); 
    } 
    for(int i=0;i<length;i++){ 
     table3[i]=(char)table[i]^(char)get[i]; 
    } 

    cout << table3 << "\n"; 



    return 0; 
} 

編輯:沒關係,這一切只是我太傻了

所以,我試圖修復大衛·施瓦茨給了我,但後來當我試圖 哈希密碼,它再次退出工作。我使用的 散列輸入字符串的功能是:

string shaOfPass(string digestThis){ 
    string digest; 
    CryptoPP::SHA256 hash; 
CryptoPP::StringSource foo(digestThis, true, 
    new CryptoPP::HashFilter(hash, 
     new CryptoPP::HexEncoder (
     new CryptoPP::StringSink(digest)))); 
    return digest; 
} And this code results in inconstant output: 
cout << "What is your password string?\n"; 
    string get; 
    getline(cin,get); 
    string hashOfPass=shaOfPass(get); 
    int seedWithMe[256]; 
    for(int i=0;i<256;i++){ 
     seedWithMe[i]=(int)get[i%256]; 
    } 
+5

這個程序是非法的C++。你不能聲明'char table [length]',其中'length'不是一個常量。 – 2011-12-20 21:34:36

+0

乍一看問題是'table'的值在每次運行時會有所不同,因此您的輸出將是不確定的。 – Matyas 2011-12-20 21:36:38

+1

@Alexandre C .:'gcc'允許它(但你是對的,它不是標準的一部分)。 – bitmask 2011-12-20 21:38:43

回答

1

srand功能,給定一個指針時,需要將指針包含至少2^N個字節的數據(您已將N設置爲8)。所以當你的字符串少於256個字符時,你會播種一些隨機的垃圾數據。

這裏有一個修復:

int length=get.size(); 
char seedWithMe[256]; 
for(int i=0;i<256;i++){ 
    seedWithMe[i]=get[i%length]; 
} 
derp.srand(1,1,1,seedWithMe); 
+0

所以如果我先用sha256對它進行散列,那麼它會好嗎? (crypto ++的實現,使用輸出保留原始字符串,沒有在任何基地格式化) 我假設你找到了模板類。 – natman3400 2011-12-20 22:04:56

+0

不完全。 SHA256產生256 *位​​。你需要256 *字節*。如果你願意,你可以SHA256對它進行8次散列,在散列間略微修改它。 (例如,你可以散列它,在末尾附加一個「。」,再次散列它,等等)。如果這是一個真正的加密應用程序,我建議你使用一個專門爲此目的設計的算法,例如作爲具有已知安全密鑰算法的RC4。 (或者至少有一個弱點被充分研究。) – 2011-12-20 22:06:49

+0

啊,誤解了。我假設它會將每個字母計爲一個字節,我可以將不同類型的哈希結果連接起來。 – natman3400 2011-12-20 22:09:19

相關問題