Algorithm for substitution encryption

Reply

Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Algorithm for substitution encryption

 
0
  #1
Jan 26th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Algorithm for substitution encryption

 
0
  #2
Jan 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 951
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Algorithm for substitution encryption

 
1
  #3
Jan 26th, 2009
Google, "rot13" That's as easy as it gets... well, actually using bit-operations are easier by a few lines less typed.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Algorithm for substitution encryption

 
0
  #4
Jan 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Algorithm for substitution encryption

 
0
  #5
Jan 26th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Algorithm for substitution encryption

 
0
  #6
Jan 27th, 2009
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Algorithm for substitution encryption

 
0
  #7
Jan 28th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Algorithm for substitution encryption

 
1
  #8
Jan 28th, 2009
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Algorithm for substitution encryption

 
0
  #9
Jan 28th, 2009
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC