By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,155 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,155 IT Pros & Developers. It's quick & easy.

C Code for cryptographic algorithm

P: 1
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
Oct 30 '15 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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()?
Oct 30 '15 #2

P: 16
char rawdata[]="2_three_01_six"
int i = 0
while i<rawdata.length()
{
if string.isdigit(rawdata[i]) && rawdata[i-1]!='_'
print rawdata[i]
i=i+1
}
Will this work?? @weaknessforcats
Oct 31 '15 #3

Expert 100+
P: 2,418
@jaseel97 you need to print some separator (space, new line, etc) between numbers.
Oct 31 '15 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
@jaseel97:
It worked for me. Of course, I had to fix the code errors but the logic flow is correct.
Oct 31 '15 #5

P: 29
Try This code

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. // find gcd
  5. int gcd(int a, int b) {
  6.    int t;
  7.    while(1) {
  8.       t= a%b;
  9.       if(t==0)
  10.       return b;
  11.       a = b;
  12.       b= t;
  13.    }
  14. }
  15. int main() {
  16.    //2 random prime numbers
  17.    double p = 13;
  18.    double q = 11;
  19.    double n=p*q;//calculate n
  20.    double track;
  21.    double phi= (p-1)*(q-1);//calculate phi
  22.    //public key
  23.    //e stands for encrypt
  24.    double e=7;
  25.    //for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
  26.    while(e<phi) {
  27.       track = gcd(e,phi);
  28.       if(track==1)
  29.          break;
  30.       else
  31.          e++;
  32.    }
  33.    //private key
  34.    //d stands for decrypt
  35.    //choosing d such that it satisfies d*e = 1 mod phi
  36.    double d1=1/e;
  37.    double d=fmod(d1,phi);
  38.    double message = 9;
  39.    double c = pow(message,e); //encrypt the message
  40.    double m = pow(c,d);
  41.    c=fmod(c,n);
  42.    m=fmod(m,n);
  43.    cout<<"Original Message = "<<message;
  44.    cout<<"\n"<<"p = "<<p;
  45.    cout<<"\n"<<"q = "<<q;
  46.    cout<<"\n"<<"n = pq = "<<n;
  47.    cout<<"\n"<<"phi = "<<phi;
  48.    cout<<"\n"<<"e = "<<e;
  49.    cout<<"\n"<<"d = "<<d;
  50.    cout<<"\n"<<"Encrypted message = "<<c;
  51.    cout<<"\n"<<"Decrypted message = "<<m;
  52.    return 0;
  53. }
  54.  
Output

Expand|Select|Wrap|Line Numbers
  1. p = 13
  2. q = 11
  3. n = pq = 143
  4. phi = 120
  5. e = 7
  6. d = 0.142857
  7. Original Message = 9
  8.  
  9. Encrypted message = 48
  10. Decrypted message = 9
  11.  
Jun 3 '20 #6

P: 25
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.
Jun 5 '20 #7

Post your reply

Sign in to post your reply or Sign up for a free account.