943,692 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2228
  • C RSS
Jan 26th, 2009
0

Algorithm for substitution encryption

Expand Post »
I'm working in a project and part of it is to do a small encryption program. W can use substitution encryption where each character is substituted by another pre-determined character. Can someone kindly help me with an algorithm. I just can't think any more!

Thanks

drjay
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Jan 26th, 2009
0

Re: Algorithm for substitution encryption

To encrypt:
while string
iteration of char = iteration of char + pre-determined character

To decrypt:
The opposite.
Last edited by Aia; Jan 26th, 2009 at 3:54 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 26th, 2009
1

Re: Algorithm for substitution encryption

Google, "rot13" That's as easy as it gets... well, actually using bit-operations are easier by a few lines less typed.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 26th, 2009
0

Re: Algorithm for substitution encryption

A simple solution (but will give more flexibility): define two arrays, one with source characters and the other with destination characters to replace those with. Arrays are matched by indices. Just make sure there are no repeats or decryption may not work correctly.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 26th, 2009
0

Re: Algorithm for substitution encryption

You can also look up RSA or DES if you are dealing with delicate data. But they are both quite complicated to implement (compared to Caesar cipher and rot13).
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Jan 27th, 2009
0

Re: Algorithm for substitution encryption

Thanks!
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Jan 28th, 2009
0

Re: Algorithm for substitution encryption

Can someone explain to me what I'm doing wrong
  1. #include <stdio.h>
  2.  
  3. main(){
  4. char ch = 'd';
  5. char alp[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  6. char enc[26] = {'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e'};
  7. for(int i = 0; i <= 26; i++){
  8. if(ch = alp[i]){
  9. printf("%c", enc[i]);
  10. }
  11. }
  12. return 0;
  13. }
OUTPUT:
fghijklmnopqrstuvwxyzabcde

Prints the whole enc array in order.

How do you print an individual character, not the entire array in C?
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Jan 28th, 2009
1

Re: Algorithm for substitution encryption

>for(int i = 0; i <= 26; i++){
I'd start with this. When you declare an array of 26, it means that the last valid index is 25. This loop runs off the end of the array.

>if(ch = alp[i]){
This is a common mistake too. The operator for equality is ==. The operator for assignment is =.

>How do you print an individual character, not the entire array in C?
Um, don't loop?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 28th, 2009
0

Re: Algorithm for substitution encryption

cheers mate!
it works!

the 26 thing i did it on purpose... the assignment operator i just didnt see it... i guess its all about experience!

thanks though i would have had to spend a good couple of hours trying this...
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: strings
Next Thread in C Forum Timeline: How to measure C execution time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC