0

edit: I am implementing an algorithm in c++.

#include<iostream>
#include<math.h>
#include<string>

using namespace std;

 int gcd(int n,int m)
   {
   if(m<=n && n%m ==0)
   return m;
   if(n<m)
   return gcd(m,n);
   else
   return gcd(m,n%m);
  }

int REncryptText(char m)
  {


      int p = 11, q = 3;
      int e = 3;
      int n = p * q;
      int phi = (p - 1) * (q - 1);

      int check1 = gcd(e, p - 1);
      int check2 = gcd(e, q - 1);

      int check3 = gcd(e, phi);

            //     // Compute d such that ed ≡ 1 (mod phi)
            //i.e. compute d = e-1 mod phi = 3-1 mod 20
            //i.e. find a value for d such that phi divides (ed-1)
            //i.e. find d such that 20 divides 3d-1.
            //Simple testing (d = 1, 2, ...) gives d = 7

   //   double d = Math.Pow(e, -1) % phi;

      int d = 7;

      // public key = (n,e) // (33,3)
      //private key = (n,d) //(33 ,7)

      double g = pow(m,e);
      int ciphertext = g %n;
      // Now say we want to encrypt the message m = 7, c = me mod n = 73 mod 33 = 343 mod 33 = 13. Hence the ciphertext c = 13. 

      //double decrypt = Math.Pow(ciphertext, d) % n;
      return ciphertext;
  }

int main()
    {
    char plaintext[80],str[80];

    cout<<" enter the text you want to encrpt";
    cin.get(plaintext,79);

    int l =strlen(plaintext);
    for ( int i =0 ; i<l ; i++)
        { 
        char s = plaintext[i];
        str[i]=REncryptText(s);
        }
for ( int i =0 ; i<l ; i++)
    { 
    cout<<"the encryption of string"<<endl;
        cout<<str[i];
    }


return 0;
}

error message error C2296: '%' : illegal, left operand has type 'double'

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
Raja
  • 105
  • 1
  • 3
  • 9
  • 2
    Why does `REncryptText` take a double, and output a double? Shouldn't it take in a string and output a string? – Matt Ellen Jan 08 '11 at 12:01
  • @matt ellen, my fuction does take first value of string and convert its cypher code instead of whole at once. i just want to pass each value one by one using for loop. and at allast want to add all the thing into final one cypher code. – Raja Jan 08 '11 at 12:14
  • 2
    @Raja: why does your function take a double and return a double? Doubles are not strings or characters. – Matt Ellen Jan 08 '11 at 12:25
  • 6
    @Raja: Writing your own encryption routine is a *really bad idea*. Unless you're a trained cryptographer, you're pretty much bound to do it wrong. Why don't you want to use the standard algorithms? – Jon Skeet Jan 08 '11 at 12:29
  • It's unfortunate that I can't edit Jon Skeet's comment above to appear in giant bold letters. Unless this is a purely academic exercise, I strongly recommend that you take a look at the various functionality already provided in the [`System.Security` namespace](http://msdn.microsoft.com/en-us/library/gg145025.aspx) that is free for use. There's absolutely no reason to do this yourself. It's too likely that your wheel will end up looking more like a dodecahedron. – Cody Gray - on strike Jan 08 '11 at 12:56
  • @Matt ellen and @ code Gray. i have imlemented the md5 alogorithm myself in REncrypttext and decrypted the same.but problem is it is only for the single character.so i want to add one at the end .how to do that. – Raja Jan 08 '11 at 13:06
  • 1
    Raja, the details from your code sample really make no sense: you seem to want to assign a `double` to an `int[]` . The `5` and `32` magic numbers make no sense either. You're not going to get a concrete answer this way. You already got some good advice though. – H H Jan 08 '11 at 13:23
  • The only part of this question that makes any sense has already been answered: How do I do encryption in C#? Use one of the standard classes. The rest is nonsense, incomprehensible, or both. Voting to close. – Cody Gray - on strike Jan 08 '11 at 13:25
  • @ for all who hav added comment. i have edit the question and done this program in visual c++.plz hav a look.and remove the error required – Raja Jan 08 '11 at 13:38
  • 2
    You edited the question to be about something *completely* different, including the language it's written in, yet accepted an answer to the *previous* question? I think you meant to post a new question instead... – Cody Gray - on strike Jan 08 '11 at 15:05

1 Answers1

2

Try this.

Simple 2 way encryption for C#

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251
  • i don't wanna any in built fuction for it. i want to use my own encrytion technique.plz answer my question – Raja Jan 08 '11 at 12:01
  • 1
    @Raja: "don't do it like that" is sometimes the best answer to a question. Ans plz be polite. – H H Jan 08 '11 at 13:05
  • @henk sorry for any misappropriate word – Raja Jan 08 '11 at 13:09
  • @ for all who hav added comment. i have edit the question and done this program in visual c++.plz hav a look.and remove the error required – Raja Jan 08 '11 at 13:38
  • 1
    @Raja: your error is that you are trying to use the modulus operator on a double and an int. it will only work between 2 ints – Matt Ellen Jan 08 '11 at 16:00