473,406 Members | 2,713 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,406 software developers and data experts.

Help with a simple encryption program?

Hello,

I'm trying to make a simple Caesar cipher however instead of A-Z i want to also include 0-9. where 9 wraps back to A.

The best i could can do shift letters and integers independently so a string like "TESTING SHIFT Z 9" with a key of 1
becomes
"UFTUJOH TIJGU A 0"

9 wraps to 0 and Z wraps to A

here is how I did that:

Expand|Select|Wrap|Line Numbers
  1.     private char encryptChar(char c, int k) {
  2.         if (Character.isDigit(c))
  3.             return (char) ('0' + (c - '0' + k) % 10); //handles the ints
  4.         else if (Character.isLetter(c))
  5.             return (char) ('A' + (c - 'A' + k) % 26); //handles the letters
  6.         else
  7.             return c;
  8.     }
  9.  
Is there a way I can make 9 wrap around to A such that instead of the traditional?

so that instead of:
ABCDEFGHIJKLMNOPQRSTUVXYZ (Z wraps to A)

what i'm trying to do is:
ABCDEFGHIJKLMNOPQRSTUVXYZ0123465789 (9 wraps to A)

Thanks in advance for any help!
Apr 4 '10 #1
1 2278
jkmyoung
2,057 Expert 2GB
Change the mod to 36 for both, and adjust the final result accordingly. Here's how to work the digits:
Expand|Select|Wrap|Line Numbers
  1.         char d;
  2.         if (Character.isDigit(c)) {
  3.             d = (char) ((c - '0' + k) % 36);
  4.             if (d <= 9)
  5.               return '0' + d;
  6.             else
  7.               return d - 10 + 'A';
  8.          }
You may need to cast where appropriate.
Apr 5 '10 #2

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

Similar topics

11
by: Steve Clay | last post by:
I have a small C program for a college course. It is meant to encrypt and decrypt lower case letters and leave spaces as spaces. I can't get it to run properly as I think I have a problem in the...
11
by: Beeeeeeeeeeeeves | last post by:
Hi I'm looking for some .net example code on encryption: I don't want anything fancy, no complex third party components with sophisticated algorithms, explanations of principle, or "public/private...
10
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets...
0
by: soss | last post by:
I want to make a program that read a text file and encrypt it by add 1 to each character value , for example: before encryption: HELLO Afater encryption: IFMMP Thank you
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...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
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...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
2
by: BISKOT188 | last post by:
hello my friends .my first problem...i ve develop an algorith of the casear encryption but i have an error and i cant make it work..the decryption works fine but i have problem with the encryption...
3
by: yottabyte | last post by:
Hey bytes, you may or may not remember but last time I was here a few months ago I got some help with making a hangman program which went well. Now I'm still doing okay in Java this year but I'm...
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: 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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.