Encryption Program Problem
SPONSORED LINKS
The task is to: (All lowercase letters)
1. Design a function, encrypt, that when applied to an unencrypted string un_str, transforms un_str to an encrypted string en_str using a right shift of length s to the letters of the alphabet with wraparound.
2. Design a function decrypt that when applied to an encrypted string en_str, transforms en_str to a decrypted string. The string en_str will have been encrypted with a cipher of length s.
Input: Shift length s, unencrypted string un_str, encrypted en_str
Output: String un_str and its encryption using function encrypt, String en_str and its decryption using function decrypt
Finished Program should be able to:
- Prompt for and get shift length x
- Prompt for and get unencrypted string un_str
- Encrypt un_str into en_str
- Prompt for and get encrypted string en_str
- Decrypt en_str into un_str
This is what I have so far, it’s obviously amateur’s work and I know there are SEVERAL errors as it is (though I’m not sure where ~). I’m trying to have it so that the program asks for a shift length, followed by offering a decrypt or encrypt option, followed by prompting for an entered string, and have the ability to perform those processes. Unfortunately I’m stuck. The string part, I really need help on. Thanks in advance
Title: 9.cpp
#include "9f.h"
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(void)
{
char un_str[100];
int shift;
int crypt;
std::cout << "Enter a desired shift length -> ";
std::cin >> shift;
std::cout << "Press 1 to encrypt or 0 to decrypt: ";
std::cin >> crypt;
while (getchar() != 'n');
if (crypt == 1)
encrypt(shift);
else
{
decrypt(shift);
}
std::cout << "Enter a string -> ";
std::cin.getline(un_str, 100);
return 0;
}
Title: 9f.cpp
#include "9f.h"
#include <iostream>
void encrypt(int shift) // prototypes of functions used in the code
{
char en_str;
std::cout <<
en_str = getchar();
while(en_str != 'n')
{
if (ch == ' ')
putchar(en_str);
else
{
if(shift == 1)
putchar (en_str + shift);
else
putchar (en_str - shift);
}
en_str = getchar();
}
putchar(en_str);
return 0;
}
void decrypt(int shift)
{
shift = -1 * shift;
encrypt(shift);
return 0;
}
Title: 9f.h
void encrypt (int shift);
void decrypt (int shift);
See the original post:
Encryption Program Problem