Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code below. The code is about RSA.
C++
#include "rsa.h"

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

rsa.cpp
C++
<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
C++
<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
C++
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
C++
#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 30-Jun-20 16:30pm
Comments
KarstenK 29-Jun-20 3:18am    
encryption is the holy gral of computing. For that you need really a lot of knowledege. So learn about it in depth.

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[^]
 
Share this answer
 
v2
Comments
CPallini 1-Jul-20 2:44am    
5.
Patrice T 1-Jul-20 3:04am    
Thank you
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);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900