473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sentence Encryption in C++

I have a problem in C++. I am writing a program to convert a sentence to a string of numbers. The encryption technique is:
A=1, B=2, C=3...Z=26
a=33, b=34, c=35...z=58

I am not suppose to convert spaces they should be left in the string. So, say for example, a user typed the sentence: The Cat

My final program should display:

Encrypted: 204037 33352
Unencrypted: The Cat

Please can someone help me with this task, as soon as possible. Thank you
Oct 29 '08 #1
7 5087
donbock
2,426 Expert 2GB
Let me try to restate your problem to see if I understand it:
Iterate through each character of a string: if the character is an upper or lowercase letter then emit two decimal digits (as per encoding rules); else emit the character itself.

What if there are pairs of decimal digits in the input string? Looks like they will pass through unchanged to the encoded output string. Your decoder will interpret them as a single alphabetic character.

Your example suggests that the first 9 uppercase letters expand into a single decimal digit. If so, then I don't see how you can expect to decode the output string.
Oct 29 '08 #2
If a user enters a sentence, the program should display both the encrypted and unencrypted output.

Therefore, the input is a string input, no numbers
And it can contain both upper and lower cases

A=1, B=2, C=3, D=4, E=5, F=6 and so on...
a=33, b=34, c=35, d=36, e=37 and so on...

So, if a user enters the sentence
The cat
the output would be:

Encrypted: 204037 33352
Unencrypted: The cat
Oct 29 '08 #3
Let me try to restate your problem to see if I understand it:
Iterate through each character of a string: if the character is an upper or lowercase letter then emit two decimal digits (as per encoding rules); else emit the character itself.

What if there are pairs of decimal digits in the input string? Looks like they will pass through unchanged to the encoded output string. Your decoder will interpret them as a single alphabetic character.

Your example suggests that the first 9 uppercase letters expand into a single decimal digit. If so, then I don't see how you can expect to decode the output string.
If a user enters a sentence, the program should display both the encrypted and unencrypted output.

Therefore, the input is a string input, no numbers
And it can contain both upper and lower cases

A=1, B=2, C=3, D=4, E=5, F=6 and so on...
a=33, b=34, c=35, d=36, e=37 and so on...

So, if a user enters the sentence
The cat
the output would be:

Encrypted: 204037 33352
Unencrypted: The cat
Oct 29 '08 #4
donbock
2,426 Expert 2GB
What is the encrypted string for:
The 12 cats

What is the encrypted string for:
The L cats

When you print the unencrypted string are you simply remembering the input string or are you pushing the encrypted string through a decrypter algorithm?

If you're running a decrypter algorithm then the encryption algorithm needs to be lossless.
Oct 29 '08 #5
AmeL
15
As you mentioned above, the designed constraint only allows the string input and never number type. so i better not speak something about this ( but wait you should consider this case too before shipping your .exe to your customer).
Yet I found another problem with this
How can u decrypt this data : 334 ?
if you split like this 3, 3 and 4 so it will be CCD
if that comes to 3, and 34 so it will be Cb
and if that 33, and 4 so it will be aD
see ? how can you manage that ?

Well, i would like to say that this is not called the encryption at all; that is a hashing. You do a hashing value by replacing the real value with the representing value; so do you think that it is more secured enough ?
Let says you are the one, on this earth, who knows this algorithm ( replace a letter with an appropriate number), so at least there is one person that can decrypt the data; no doubt that person is you.

Well the encryption means in another way. Even the one who designs the code for encryption can not reveal the decrypted data. why ? Actually there are so many mechanisms to achieve that goal but this is one of those :
You better implement a key associated with the decrypted data which looks like the following :
Expand|Select|Wrap|Line Numbers
  1. int Encrypt(char *key, char *data)
  2. {
  3.    // do something with the key and associate it with
  4.    // data to create the decrypted data
  5.    decrypted = key + data   // example
  6.    return ....
  7. }
  8.  
So imagine, if you were the code designer, you would not know what the data is.
why ? because it uses the key to encrypt the data. You can know what is behind unless you know the key.
Oct 30 '08 #6
This is a useful program that will make the encryption decodable after it is coded:
Expand|Select|Wrap|Line Numbers
  1. Deleted by moderator
This program will give you a file called zaza which has written in it the encrypted data, which in the case of "The Cat" which is used in this program is: {20,40,37,-32,3,33,52,0}. I just fixed the ascii code to the way you want(ascii value - 64). And note that I placed -32 for the space to avoid complications, and I placed a 0 for the null charachter.
Now that you have commas between all the numbers you will have no problem decoding it, just copy the code from the file zaza and paste it at the right of char string[]= in this program:
Expand|Select|Wrap|Line Numbers
  1. Spoonfeeding code removed
But to have an actual code that could not be decrypted easily apply some kind of formula to the numbers, like 2*number+1 in the encryption code which will make you use (number-1)/2 in the decryption code, but ofcourse there are much better ways to complicate the encrypted code, just use your imagination.
Oct 31 '08 #7
RedSon
5,000 Expert 4TB
Yes, I have to agree with the above poster, this is not encryption it is just replacing letters with numbers. Replacing letters with numbers is also known as a Caesar cipher and can be broken almost instantly with today's computers. All you need to do is take the char value and cast it to an int and then add or subtract the appropriate offset.

If you are using ASCII the characters A B C correspond to 65,66,67 so when you read them in to your program as integers you will have 65, 66 and 67. Then you simply subtract 64 from each value and then A will correspond to 1 and B will correspond to 2 and C will correspond to 3.

Good luck, and be careful using other people's code as it may cause errors that you are unfamiliar with.
Oct 31 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
6
by: mike | last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink just the words in it. I used Aaron's code from an earlier question as a start. So far, all the code does below is...
4
by: pintu | last post by:
Hello everybody.. I hav some confusion regarding asymmetric encryption.As asymmetric encryption it there is one private key and one public key.So any data is encrypted using private key and the...
1
by: =?Utf-8?B?bWljcm9ob2Y=?= | last post by:
Short version: Is there a way to configure (preferably programmatically) the max encryption strength that will be used by the framework when connecting to a particular SSL-protected web service? ...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
4
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by...
19
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.