#include<stdio.h>/* ==================== *//* FUNCTIONS *//* ==================== */void encrypt(FILE *file_in, FILE *file_out, int key){ int key_prev, key_new, value; char c; key_prev = key; while( (c = fgetc(file_in)) != EOF) { switch(c) { case ' ': fputc(' ', file_out); break; case '\n': fputc('\n', file_out); break; default: // Elimino el último dígito de key_prev key_new = key_prev/10; // Si no es el último dígito de la key corrida if(key_new != 0) { // La diferencia con la key_prev me da el último dígito value = key_prev - 10*key_new; // Para que en ciclo se haga lo anterior sobre la nueva key key_prev = key_new; fputc(c + value, file_out); } else { fputc(c + key_prev, file_out); key_prev = key; } break; } }}void decrypt(FILE *file_in, FILE *file_out, int key){ int key_prev, key_new, value; char c; key_prev = key; while( (c = fgetc(file_in)) != EOF) { switch(c) { case ' ': fputc(' ', file_out); break; case '\n': fputc('\n', file_out); break; default: // Elimino el último dígito de key_prev key_new = key_prev/10; // Si no es el último dígito de la key corrida if(key_new != 0) { // La diferencia con la key_prev me da el último dígito value = key_prev - 10*key_new; // Para que en ciclo se haga lo anterior sobre la nueva key key_prev = key_new; fputc(c - value, file_out); } else { fputc(c - key_prev, file_out); key_prev = key; } break; } }}/* ==================== *//* MAIN *//* ==================== */int main(int argc, char *argv[]){ int opt, key; FILE *file_in, *file_out; if(argc == 2) { printf("\n\n"); printf("=================================\n"); printf(" Cypher Caesar \n"); printf("=================================\n"); printf("\nChoice an option:\n"); printf("\tCrypter ---------- 1\n"); printf("\tDecrypter -------- 2\n"); printf("\nOption: "); scanf("%d", &opt); switch(opt) { // Eligio encriptar case 1: printf("\n----------------------------------\n"); printf("\n\t\tCrypter\n"); printf("\t\t-------\n\n"); /* Abro el fichero con el texto a encriptar*/ file_in = fopen(argv[1], "r"); printf("Opening '%s'... ", argv[1]); if(file_in) printf("\tOpen!\n"); else { printf("Error!\n"); printf("Quitting..."); return -1; } /* Creo el fichero con el texto encriptado*/ file_out = fopen("Encrypted.txt", "w"); printf("Creating 'Encrypted.txt'... "); if(file_in) printf("\tCreate!\n"); else { printf("Error!\n"); printf("Quitting..."); return -1; } /* Ingreso la clave */ printf("\nInsert key: "); scanf("%d", &key); encrypt(file_in, file_out, key); if(fclose(file_in)) { printf("Error closing '%s'\n", argv[1]); printf("Closing..."); return -1; } if(fclose(file_out)) { printf("Error closing 'Encrypted.txt'\n"); printf("Closing..."); return -1; } printf("\nEncrypt successfull!\n\n"); return 0; break; case 2: printf("\n----------------------------------\n"); printf("\n\t\tDecrypter\n"); printf("\t\t---------\n\n"); /* Abro el fichero con el texto a encriptar*/ file_in = fopen(argv[1], "r"); printf("Opening '%s'... ", argv[1]); if(file_in) printf("\tOpen!\n"); else { printf("\tError!\n"); printf("Quitting..."); return -1; } /* Creo el fichero con el texto encriptado*/ file_out = fopen("Decrypted.txt", "w"); printf("Creating 'Decrypted.txt'... "); if(file_in) printf("\tCreate!\n"); else { printf("\tError!\n"); printf("Quitting..."); return -1; } /* Ingreso la clave */ printf("\nInsert key: "); scanf("%d", &key); decrypt(file_in, file_out, key); if(fclose(file_in)) { printf("Error closing '%s'\n", argv[1]); printf("Closing..."); return -1; } if(fclose(file_out)) { printf("Error closing 'Decrypted.txt'\n"); printf("Closing..."); return -1; } printf("\nDecrypt successfull!\n\n"); return 0; break; default: printf("Invalid Option\n"); break; } } else { printf("Use: %s <file>\n", argv[0]); return(-1); }}
// ** cifrado cesar **// by user170294#include <iostream>using namespace std; int main (){ int caracteres=0; // caracteres del string ingresado int posiciones=0; // posiciones a mover string input=""; // string a cifrar (input=entrada) cout << "** cifrado cesar **" << endl; cout << "string a convertir >> "; cin >> input; cout << "posiciones a mover >> "; cin >> posiciones; caracteres = input.length(); // contamos los caracteres del input for(int i=0; i<caracteres; i++) // recorrer cada caracter del string for (int z=0; z<posiciones; z++) // aumentando las posiciones input[i]++; cout << input << endl; // imprimir el resultado system("pause"); return 0; //Salir}