954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Algorithm for substitution encryption

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

drjay1627
Junior Poster in Training
91 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

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

To decrypt:
The opposite.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Google, "rot13" That's as easy as it gets... well, actually using bit-operations are easier by a few lines less typed.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

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.

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

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).

devnar
Junior Poster
149 posts since Sep 2008
Reputation Points: 124
Solved Threads: 18
 

Thanks!

drjay1627
Junior Poster in Training
91 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

Can someone explain to me what I'm doing wrong

#include <stdio.h>

main(){
	char ch = 'd';
	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'}; 
	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'};
	for(int i = 0; i <= 26; i++){
		if(ch = alp[i]){
			printf("%c", enc[i]);
		}
	}
return 0;
}

OUTPUT:
fghijklmnopqrstuvwxyzabcde

Prints the whole enc array in order.

How do you print an individual character, not the entire array in C?

drjay1627
Junior Poster in Training
91 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

>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? :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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...

drjay1627
Junior Poster in Training
91 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You