Click here to Skip to main content
14,574,685 members
Rate this:
Please Sign up or sign in to vote.
See more:
Here is the code below. The code is about RSA.
#include "rsa.h"

int main()
{
  RSA rsa(13, 77);
  rsa.crypt("plainFile", "cipherFile");
  return 0;
}

rsa.cpp
<pre>#include "rsa.h"
#include <fstream>
#include <cmath>

RSA :: RSA(int k, int m)
: key(k), modulus(m)
{
}
RSA :: ~RSA()
{
}
void RSA :: crypt(const char* inFile, const char* outFile)
{
  ifstream  istrm(inFile, ios :: in);   
  ofstream  ostrm(outFile, ios :: out);
  int base, result;
  while (istrm >> base)
  {
    result = modulo(base, key, modulus);
    ostrm << result;
    ostrm << ' ';
  }
  istrm.close();
  ostrm.close();
}
int RSA:: modulo(int base, int power, int modulus)
{
  int result = 1;
  for (int i = 0; i < power; i++)
  {
    result = (result * base) % modulus;
  }
  return result;
}

rsa.h
<pre>#ifndef RSA_H
#define RSA_H
#include <iostream>
using namespace std;

class RSA
{
  private:
    int key;
    int modulus;
    int modulo(int base, int power, int modulus);
  public: 
    RSA(int key, int modulus);
    ~RSA();
    void crypt(const char* inFile, const char* outFile);
};
#endif

The output should be
Contents of Plaintext File:
14 27 12 45 9 64 22 8
Contents of Ciphertext File:
49 48 12 45 58 36 22 50

Now I would like to make this output become input, and get this output
#include "rsa.h"
int main ()
{
RSA rsa (13, 77); // 13 is the value of e and 77 is value of n
rsa.crypt ("plainFile", "cipherFile"); return 0;
}

I try to exchange input and output but don't know where to start, can I just change a little bit to do that? or I need to rewrite the code? anyone can help? How should I do it? Thanks.

What I have tried:

I try to use a vector for current output but don't know how to do that.
Posted
Updated 2 days ago
Comments
KarstenK 4 days ago
   
encryption is the holy gral of computing. For that you need really a lot of knowledege. So learn about it in depth.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
I try to exchange input and output but don't know where to start, can I just change a little bit to do that? or I need to rewrite the code?

The operation you are looking for is decryption.Because of the way rsa works, reapplying rsa.crypt to cipherFile will not give you plainFile.
You need to create code to decrypt the data accordingly to rsa encryption/decryption scheme.
You also need to note that the public key used to encrypt is not the key used to decrypt.
RSA (cryptosystem) - Wikipedia[^]
   
v2
Comments
CPallini 2 days ago
   
5.
Patrice T 2 days ago
   
Thank you
Rate this:
Please Sign up or sign in to vote.

Solution 2

rsa(13, 77);

77=7*11

RSA param:
p=7
q=11
e=13
n=77
d=37   
d value condition: 
(d * e) % t == 1 
t = (p-1) * (q-1);


to decryption the value you can use rsa(37, 77);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100