Stuck in while loop when change to c++












1














i'm trying to rewrite this code in c++ which create a bitcoin mini private key



using System;
using System.Text;
using Org.BouncyCastle.Security;


public string CreateRandomMinikey(){
string keytotry = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
char chars = keytotry.ToCharArray();
char charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {// hash sha256 the key & check if the first character was '0'
// As long as key doesn't pass typo check, increment it.
for (int i = chars.Length - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
} else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
} else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
} else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
} else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
} else if (c == 'z') {
charstest[i] = chars[i] = '2';
// No break - let loop increment prior character.
} else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = new string(chars);//expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
return result;
}


Here my code when i transform into c++



#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <cstring>
#include "sha256.h"

using namespace std;

int main()
{
string input = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
string inputcharstest = input+"?";
char * chars = new char[input.size() + 1];
strcpy(chars, input.c_str());//convert input to char array
char * charstest = new char[inputcharstest.size() + 1];
strcpy(charstest, inputcharstest.c_str());// convert inputcharstest to char array
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
for (int i = strlen(chars) - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
}
else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
}
else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
}
else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
}
else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
}
else if (c == 'z') {
charstest[i] = chars[i] = '2';
}
else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = string(chars); //expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
cout << "input('" << input << "'):" << result << endl;

return 0;
}


enter image description here



This is the result i expected
enter image description here



The code was stuck & return blank when i run it on console application, i debug & find out it was stuck in while loop forever. May i ask if there're any problem with this & how do i fix it?










share|improve this question






















  • You never modify output[0] in your loop. What do you expect?
    – Nelfeal
    Nov 23 '18 at 7:32










  • @Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
    – Fei Xiang
    Nov 23 '18 at 7:32












  • output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
    – Huang Lee
    Nov 23 '18 at 7:33










  • If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
    – Nelfeal
    Nov 23 '18 at 7:36










  • @Nelfeal how can i modify it in loop, i'm new to c++
    – Huang Lee
    Nov 23 '18 at 7:36
















1














i'm trying to rewrite this code in c++ which create a bitcoin mini private key



using System;
using System.Text;
using Org.BouncyCastle.Security;


public string CreateRandomMinikey(){
string keytotry = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
char chars = keytotry.ToCharArray();
char charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {// hash sha256 the key & check if the first character was '0'
// As long as key doesn't pass typo check, increment it.
for (int i = chars.Length - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
} else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
} else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
} else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
} else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
} else if (c == 'z') {
charstest[i] = chars[i] = '2';
// No break - let loop increment prior character.
} else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = new string(chars);//expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
return result;
}


Here my code when i transform into c++



#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <cstring>
#include "sha256.h"

using namespace std;

int main()
{
string input = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
string inputcharstest = input+"?";
char * chars = new char[input.size() + 1];
strcpy(chars, input.c_str());//convert input to char array
char * charstest = new char[inputcharstest.size() + 1];
strcpy(charstest, inputcharstest.c_str());// convert inputcharstest to char array
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
for (int i = strlen(chars) - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
}
else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
}
else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
}
else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
}
else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
}
else if (c == 'z') {
charstest[i] = chars[i] = '2';
}
else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = string(chars); //expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
cout << "input('" << input << "'):" << result << endl;

return 0;
}


enter image description here



This is the result i expected
enter image description here



The code was stuck & return blank when i run it on console application, i debug & find out it was stuck in while loop forever. May i ask if there're any problem with this & how do i fix it?










share|improve this question






















  • You never modify output[0] in your loop. What do you expect?
    – Nelfeal
    Nov 23 '18 at 7:32










  • @Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
    – Fei Xiang
    Nov 23 '18 at 7:32












  • output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
    – Huang Lee
    Nov 23 '18 at 7:33










  • If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
    – Nelfeal
    Nov 23 '18 at 7:36










  • @Nelfeal how can i modify it in loop, i'm new to c++
    – Huang Lee
    Nov 23 '18 at 7:36














1












1








1







i'm trying to rewrite this code in c++ which create a bitcoin mini private key



using System;
using System.Text;
using Org.BouncyCastle.Security;


public string CreateRandomMinikey(){
string keytotry = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
char chars = keytotry.ToCharArray();
char charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {// hash sha256 the key & check if the first character was '0'
// As long as key doesn't pass typo check, increment it.
for (int i = chars.Length - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
} else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
} else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
} else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
} else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
} else if (c == 'z') {
charstest[i] = chars[i] = '2';
// No break - let loop increment prior character.
} else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = new string(chars);//expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
return result;
}


Here my code when i transform into c++



#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <cstring>
#include "sha256.h"

using namespace std;

int main()
{
string input = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
string inputcharstest = input+"?";
char * chars = new char[input.size() + 1];
strcpy(chars, input.c_str());//convert input to char array
char * charstest = new char[inputcharstest.size() + 1];
strcpy(charstest, inputcharstest.c_str());// convert inputcharstest to char array
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
for (int i = strlen(chars) - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
}
else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
}
else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
}
else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
}
else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
}
else if (c == 'z') {
charstest[i] = chars[i] = '2';
}
else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = string(chars); //expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
cout << "input('" << input << "'):" << result << endl;

return 0;
}


enter image description here



This is the result i expected
enter image description here



The code was stuck & return blank when i run it on console application, i debug & find out it was stuck in while loop forever. May i ask if there're any problem with this & how do i fix it?










share|improve this question













i'm trying to rewrite this code in c++ which create a bitcoin mini private key



using System;
using System.Text;
using Org.BouncyCastle.Security;


public string CreateRandomMinikey(){
string keytotry = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
char chars = keytotry.ToCharArray();
char charstest = (keytotry + "?").ToCharArray();
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {// hash sha256 the key & check if the first character was '0'
// As long as key doesn't pass typo check, increment it.
for (int i = chars.Length - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
} else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
} else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
} else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
} else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
} else if (c == 'z') {
charstest[i] = chars[i] = '2';
// No break - let loop increment prior character.
} else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = new string(chars);//expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
return result;
}


Here my code when i transform into c++



#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <cstring>
#include "sha256.h"

using namespace std;

int main()
{
string input = "S6c56bnXQiBjk9mqSYE7ykVQ7NzrQA";
string inputcharstest = input+"?";
char * chars = new char[input.size() + 1];
strcpy(chars, input.c_str());//convert input to char array
char * charstest = new char[inputcharstest.size() + 1];
strcpy(charstest, inputcharstest.c_str());// convert inputcharstest to char array
string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {
for (int i = strlen(chars) - 1; i >= 0; i--) {
char c = chars[i];
if (c == '9') {
charstest[i] = chars[i] = 'A';
break;
}
else if (c == 'H') {
charstest[i] = chars[i] = 'J';
break;
}
else if (c == 'N') {
charstest[i] = chars[i] = 'P';
break;
}
else if (c == 'Z') {
charstest[i] = chars[i] = 'a';
break;
}
else if (c == 'k') {
charstest[i] = chars[i] = 'm';
break;
}
else if (c == 'z') {
charstest[i] = chars[i] = '2';
}
else {
charstest[i] = chars[i] = ++c;
break;
}
}
}
string result = string(chars); //expect S6c56bnXQiBjk9mqSYE7ykVQ7NzrRy
cout << "input('" << input << "'):" << result << endl;

return 0;
}


enter image description here



This is the result i expected
enter image description here



The code was stuck & return blank when i run it on console application, i debug & find out it was stuck in while loop forever. May i ask if there're any problem with this & how do i fix it?







c++ performance cryptography






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:25









Huang Lee

278




278












  • You never modify output[0] in your loop. What do you expect?
    – Nelfeal
    Nov 23 '18 at 7:32










  • @Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
    – Fei Xiang
    Nov 23 '18 at 7:32












  • output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
    – Huang Lee
    Nov 23 '18 at 7:33










  • If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
    – Nelfeal
    Nov 23 '18 at 7:36










  • @Nelfeal how can i modify it in loop, i'm new to c++
    – Huang Lee
    Nov 23 '18 at 7:36


















  • You never modify output[0] in your loop. What do you expect?
    – Nelfeal
    Nov 23 '18 at 7:32










  • @Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
    – Fei Xiang
    Nov 23 '18 at 7:32












  • output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
    – Huang Lee
    Nov 23 '18 at 7:33










  • If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
    – Nelfeal
    Nov 23 '18 at 7:36










  • @Nelfeal how can i modify it in loop, i'm new to c++
    – Huang Lee
    Nov 23 '18 at 7:36
















You never modify output[0] in your loop. What do you expect?
– Nelfeal
Nov 23 '18 at 7:32




You never modify output[0] in your loop. What do you expect?
– Nelfeal
Nov 23 '18 at 7:32












@Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
– Fei Xiang
Nov 23 '18 at 7:32






@Ruks Since C++11, accessing one character past the end of an std::string is guaranteed to return the null character.
– Fei Xiang
Nov 23 '18 at 7:32














output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
– Huang Lee
Nov 23 '18 at 7:33




output wasn't empty, it contain the hash sha256 of the inputcharstest, i check the first character of it to see if it was '0'
– Huang Lee
Nov 23 '18 at 7:33












If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
– Nelfeal
Nov 23 '18 at 7:36




If it's 0 the first time, it's going to be 0 the next time. And the next. And the next. And the...
– Nelfeal
Nov 23 '18 at 7:36












@Nelfeal how can i modify it in loop, i'm new to c++
– Huang Lee
Nov 23 '18 at 7:36




@Nelfeal how can i modify it in loop, i'm new to c++
– Huang Lee
Nov 23 '18 at 7:36












1 Answer
1






active

oldest

votes


















1














Here you are changing the content of the string every time:



 char charstest = (keytotry + "?").ToCharArray();            
while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {


charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:



string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
while (output[0] != '0') {


Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442292%2fstuck-in-while-loop-when-change-to-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Here you are changing the content of the string every time:



     char charstest = (keytotry + "?").ToCharArray();            
    while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {


    charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:



    string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
    while (output[0] != '0') {


    Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.






    share|improve this answer




























      1














      Here you are changing the content of the string every time:



       char charstest = (keytotry + "?").ToCharArray();            
      while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {


      charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:



      string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
      while (output[0] != '0') {


      Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.






      share|improve this answer


























        1












        1








        1






        Here you are changing the content of the string every time:



         char charstest = (keytotry + "?").ToCharArray();            
        while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {


        charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:



        string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
        while (output[0] != '0') {


        Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.






        share|improve this answer














        Here you are changing the content of the string every time:



         char charstest = (keytotry + "?").ToCharArray();            
        while (Util.ComputeSha256(utf8.GetBytes(charstest))[0] != 0) {


        charstest changes in every run through the loop, so the test in the while can terminate at some point. In the C++ code, however, you're not doing that:



        string output = sha256(inputcharstest); //hash sha256 inputcharstest to check typo
        while (output[0] != '0') {


        Here, you're only doing the hash once before the loop, and then not again in the loop. The loop doesn't change output at all and neither does it break out of the while, so if output[0] != '0' is true the first time, it will always be true, thus the infinite loop.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 7:52

























        answered Nov 23 '18 at 7:38









        Blaze

        4,3821628




        4,3821628






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442292%2fstuck-in-while-loop-when-change-to-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo