473,395 Members | 1,458 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,395 software developers and data experts.

encoding and decoding messages

2
please help me with the java codes for this problem i tried to do this program a week ago but even now i dont have an idea of how to go about with it. please help me it is urgent

Cryptography

Encryption (encoding) Given a message to send, we use the following table of letter to integer conversion to perform the encoding:
a, A=0 b, B=1 c, C=2 d, D=3 e, E=4 f, F=5 g, G=6 h, H=7 i, I=8 j, J=9 k, K=10 l, L=11 m, M=12

n, N=13 o, O=14 p, P=15 q, Q=16 r, R=17 s, S=18 t, T=19 u, U=20 v, V=21 w, W=22 x, X=23 y, Y=24 z, Z=25

And a key k(0<=k<=25).

Example suppose the key for shift cipher is k=11, and the message is we will meet at midnight.
We encode each letter of the message (non-letter characters are not encoded) by first converting it to an integer using the above table, adding the integer and obtain the remainder of the sum divided by 26. the letter to replace w is given by 22 +11=33%26=7. hence w is replaced by h or H

Decoding(decryption) to decode an encoded message. For each letter we use the table to get its corresponding integer, subtract 11 from the integer and obtain the remainder of the difference divided by 26. if the result is negative we add 26. the letter to replace h is given by7-k=7-11=-4%26. since it is negative, we add 26 to get 22 so h is replaced by w or W


The task is to write two programs:
1. the fist program will read the shift Cipher key from the key board and filename from the keyboard. The program will then encode the message in the file and write it to a file called “ encrypted.txt”.
2. the second program will also read the shift cipher from the keyboard. The program will then decode the message in the file and write it to a file called “decrypted.txt”. the program should the be used to decode the following encoded message:.
Zebzh irq qeb obxi pfqrxqflk
Kxqflk txo xdxfkpq kxqflk
Tebob afa fq xii ybddfk?
Tebk tfii fq bka?
Tbii, fq pbbjp: qlqxi abpqorzqflk qeb lkiv plirqflk
Xka qebob xfk q kl rpb: kl lkb zxk pqlm qebj klt.
Xfk’ q kl rpb: klylav zxk pqlm qebj klt
Oct 3 '06 #1
1 5205
D_C
293 100+
I had this assignment too, but only in C. I'll post the encrypt/decrypt functions for you, and if you decide to understand it, go ahead and translate it.
Expand|Select|Wrap|Line Numbers
  1. const int AtoZ = 'Z'-'A'+1;
  2.  
  3. void decrypt(int offset, char *str) // offset = cipher
  4. {
  5.     int i=0,l;
  6.     l=strlen(str);
  7.     printf("encrypted str = \n%s\n", str);
  8.         while(i<l)
  9.         {    
  10.             if((*(str+i) >= 'A') && (*(str+i) < ('A' + AtoZ)))
  11.                 *(str+i)= 'A' + (*(str+i) - 'A' - offset + AtoZ)%AtoZ;
  12.             if((*(str+i) >= 'a') && (*(str+i) < ('a' + AtoZ)))
  13.                 *(str+i)= 'a' + (*(str+i) - 'a' - offset + AtoZ)%AtoZ;    
  14.             i++;
  15.         }
  16.     printf("unencrypted str = \n%s \nlength = %d\n", str, l);
  17. }
  18.  
  19. void encrypt(int offset, char *str) // offset = cipher
  20. {
  21.     int i=0,l;
  22.     l=strlen(str);
  23.     printf("unencrypted str = \n%s\n", str);
  24.         while(i<l)
  25.         {    
  26.             if((*(str+i) >= 'A') && (*(str+i) < ('A' + AtoZ)))
  27.                 *(str+i)='A' + (*(str+i) - 'A' + offset)%AtoZ;
  28.             if((*(str+i) >= 'a') && (*(str+i) < ('a' + AtoZ)))
  29.                 *(str+i)='a' + (*(str+i) - 'a' + offset)%AtoZ;    
  30.             i++;
  31.         }
  32.     printf("encrypted str = \n%s \nlength = %d\n", str, l);
  33. }
Oct 3 '06 #2

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

Similar topics

3
by: dot | last post by:
Hi all, I have written a Python huffman Encoding Module, for my own amusement. I thought it might be educational/entertaining for other people, so I've put it on my website and wrote about it a...
0
by: mubx2000 | last post by:
Hi , I'm looking for code (Symbian C++) that can do the following things: 1-Analyzing the (MIME) types (Content-typemContent-transfere-encoding,Mime Version,Conent Description). 2-Download...
9
by: KWSW | last post by:
Having settled the huffman encoding/decoding and channel modeling(thanks to the previous part on bitwise operation), the last part would be hamming encoding/decoding. Did some research as usual on...
0
by: Michele | last post by:
Hi there, I'm using a python script in conjunction with a JPype, to run java classes. So, here's the code: from jpype import * import os import random import math import sys
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.