De momento en esta primera versión solo admite los caracteres ASCII del 32 al 127.
jcrypt.h
Código
#ifndef JCRYPT_H_
#define JCRYPT_H_
#include <string>
using namespace std;
class jcrypt{
private:
char *ordenado;
char *desordenado;
int salto;
void IntToString(int, string &);
void cambiasalto(int);
public:
jcrypt(int =-1);
jcrypt(const jcrypt &);
jcrypt operator=(const jcrypt &);
~jcrypt();
string cifra(string &);
string descifra(string &);
};
#endif
jcrypt.cpp
Código
#include <iostream>
#include <time.h>
#include <cmath>
#include "jcrypt.h"
using namespace std;
const int ini = 32;
const int fin = 127;
const int N = 95;
jcrypt::jcrypt(int s){
ordenado = new char[N];
desordenado = new char[N];
for(int i=ini; i<fin; i++){
ordenado[i-ini] = i;
}
salto = s;
if(s==-1){
time_t semilla = time(NULL) + getpid();
srand(semilla);
salto = rand() % N;
}
for(int i=0; i<N; i++){
desordenado[i]=ordenado[(i+salto)%N];
}
}
jcrypt::jcrypt(const jcrypt &j){
ordenado = new char[N];
desordenado = new char[N];
for(int i=0; i<N; i++){
ordenado[i] = j.ordenado[i];
}
for(int i=0; i<N; i++){
desordenado[i] = j.desordenado[i];
}
}
jcrypt jcrypt::operator=(const jcrypt &j){
if(this != &j){
delete ordenado;
delete desordenado;
for(int i=0; i<N; i++){
ordenado[i] = j.ordenado[i];
desordenado[i] = j.desordenado[i];
}
}
return *this;
}
jcrypt::~jcrypt(){
delete ordenado;
delete desordenado;
}
string jcrypt::cifra(string & cad){
string nuevo;
int len = cad.length();
if(salto < 10) nuevo = "0";
IntToString(salto, nuevo);
for(int i=0; i<len; i++){
nuevo += desordenado[cad[i] - ini];
}
return nuevo;
}
string jcrypt::descifra(string &cad){
char s[2] = {cad[0], cad[1]}; //Salto
int pos, ss, len = cad.length();
string nuevo="";
ss = atoi(s);//Salto
if(salto != ss){
cambiasalto(ss);
}
for(int i=2; i<len; i++){
for(int j=0; j<N; j++){
if(cad[i] == desordenado[j]){
nuevo += j+32;
}
}
}
return nuevo;
}
void jcrypt::cambiasalto(int s){
for(int i=0; i<N; i++){
desordenado[i] = ordenado[(i+s) % N];
}
}
void jcrypt::IntToString(int i, string & s)
{
// s = "";
if (i == 0)
{
s = "0";
return;
}
if (i < 0)
{
s += '-';
i = -i;
}
int count = log10(i);
while (count >= 0)
{
s += ('0' + i/pow(10.0, count));
i -= static_cast<int>(i/pow(10.0,count)) * static_cast<int>(pow(10.0,count));
count--;
}
}
También un main por si no les apetece programar.
Código
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include "jcrypt.h"
using namespace std;
int main(int argc, char *argv[]){
fstream in;
fstream out;
string texto = "";
char aux;
string ttexto;
if(argc != 4){
cerr<<"ERROR: Pase de paramentros."<<endl;
cerr<<"enjcrypt E|D fich_in fich_out"<<endl;
cerr<<"E ->Eencriptar"<<endl;
cerr<<"D -> descifrar"<<endl;
exit(-1);
}
in.open(argv[2], ios::in);
out.open(argv[3], ios::out | ios::trunc);
if(!out || !in){
cerr<<"ERROR: No se han podido abrir los ficheros de E/S"<<endl;
exit(-1);
}
in.get(aux);
while(!in.eof()){
texto += aux;
in.get(aux);
}
in.close();
if(strcmp(argv[1],"E") == 0){
jcrypt var;
ttexto = var.cifra(texto);
}
else{
jcrypt var;
ttexto = var.descifra(texto);
}
for(int i=0; i<ttexto.length();i++){
out.put(ttexto[i]);
}
out.close();
return 0;
}










Autor



En línea
