C Code for cryptographic algorithm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dinal
    New Member
    • Oct 2015
    • 1

    C Code for cryptographic algorithm

    i have a problem... i have to seperate numbers from digits. For example i have a cryptographic code done which gives me the output as
    2_three_06_one_ 01_four_32_two_ 3
    I need to seperate the numbers and print them like it should be 2 6 13 23
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Can't you just read your input a byte at a time and check whether the byte value is between 49 and 57 on the ascii table.? or pass your byte to isdigit()?

    Comment

    • jaseel97
      New Member
      • Feb 2015
      • 16

      #3
      char rawdata[]="2_three_01_si x"
      int i = 0
      while i<rawdata.lengt h()
      {
      if string.isdigit( rawdata[i]) && rawdata[i-1]!='_'
      print rawdata[i]
      i=i+1
      }
      Will this work?? @weaknessforcat s

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        @jaseel97 you need to print some separator (space, new line, etc) between numbers.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          @jaseel97:
          It worked for me. Of course, I had to fix the code errors but the logic flow is correct.

          Comment

          • Sherin
            New Member
            • Jan 2020
            • 77

            #6
            Try This code

            Code:
            #include<iostream>
            #include<math.h>
            using namespace std;
            // find gcd
            int gcd(int a, int b) {
               int t;
               while(1) {
                  t= a%b;
                  if(t==0)
                  return b;
                  a = b;
                  b= t;
               }
            }
            int main() {
               //2 random prime numbers
               double p = 13;
               double q = 11;
               double n=p*q;//calculate n
               double track;
               double phi= (p-1)*(q-1);//calculate phi
               //public key
               //e stands for encrypt
               double e=7;
               //for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
               while(e<phi) {
                  track = gcd(e,phi);
                  if(track==1)
                     break;
                  else
                     e++;
               }
               //private key
               //d stands for decrypt
               //choosing d such that it satisfies d*e = 1 mod phi
               double d1=1/e;
               double d=fmod(d1,phi);
               double message = 9;
               double c = pow(message,e); //encrypt the message
               double m = pow(c,d);
               c=fmod(c,n);
               m=fmod(m,n);
               cout<<"Original Message = "<<message;
               cout<<"\n"<<"p = "<<p;
               cout<<"\n"<<"q = "<<q;
               cout<<"\n"<<"n = pq = "<<n;
               cout<<"\n"<<"phi = "<<phi;
               cout<<"\n"<<"e = "<<e;
               cout<<"\n"<<"d = "<<d;
               cout<<"\n"<<"Encrypted message = "<<c;
               cout<<"\n"<<"Decrypted message = "<<m;
               return 0;
            }
            Output

            Code:
            p = 13
            q = 11
            n = pq = 143
            phi = 120
            e = 7
            d = 0.142857
            Original Message = 9
            
            Encrypted message = 48
            Decrypted message = 9

            Comment

            • SwissProgrammer
              New Member
              • Jun 2020
              • 220

              #7
              In case the reader is new to programming the following is a process that has been used for many years.

              For C++, if your input is a std::string, or if you convert your input into a std::string, then parse it for what you want. Use the result of your parsing.

              Study std::string, and parsing, and go from there.

              Comment

              Working...