Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:58:59+00:00 2026-06-12T11:58:59+00:00

I’m trying to make a basic encryption program that used random numbers to encrypt

  • 0

I’m trying to make a basic encryption program that used random numbers to encrypt the whole alphabet, encrypt a user submitted phrase and then decrypt it back to the original phrase but I am finding it very hard. Can anyone help point out my mistakes please! It shouldn’t code two letters to the same letter, ie a and b shouldn’t ever be both matched to c.

public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
    { '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'
    };
public static final char[] Ualpha =
    {'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',
    };
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];



Random rgenerator = new Random();

public MainClass(){
    int randNum = rgenerator.nextInt(ALPHASIZE);


    for(int i=0; i<ALPHASIZE ; i++)
    {
        //makes sure that it won't assign a letter to itself or to one that has already been assigned

        do {
            randNum = rgenerator.nextInt(26);

         } while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));       

        encryptU[i] = Ualpha[randNum]; 
        encryptL[i] = Lalpha[randNum];
        decrypt[i] = randNum;

    }
}

public String encrypt(String secret)
{
    System.out.println(Arrays.toString(encryptU));
        int position = 0;
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Ualpha[j]) {
            position = j;
    }
    mess[i] = encryptU[position];

        }

        }

        if(Character.isLowerCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Lalpha[j]) {
            position = j;
    }
    mess[i] = encryptL[position];

        }

        }

    }
    return new String(mess);
}

public String decrypt(String secret)
{
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
                {
                for(int j = 0; j<ALPHASIZE; j++){
                    if(mess[i]==encryptU[j]){
                        mess[i] = Ualpha[j];

                        }
                    }
                }           

        if(Character.isLowerCase(mess[i]))
        {                   
            for(int j = 0; j<ALPHASIZE; j++){
                if(mess[i]==encryptL[j]){
                    mess[i] = Lalpha[j];

                    }
                }
            }
    }

    return new String(mess);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:59:00+00:00Added an answer on June 12, 2026 at 11:59 am

    You should really consider using a Map to store character/encoding pairs. Oh, and to create these random pairs you can add your characters to a List and make use of Collections.shuffle instead of reinventing the wheel yourself.


    Let me demonstrate using only Lalpha (only lowercase letters). You want something along these lines:

    List<Character> l = new ArrayList<Character>(Lalpha.length); 
    
    for (char c : Lalpha)
        l.add(c);
    
    Collections.shuffle(l);
    
    Map<Character, Character> encoding = new HashMap<Character, Character>(Lalpha.length);
    Map<Character, Character> decoding = new HashMap<Character, Character>(Lalpha.length);
    
    for (int i = 0 ; i < Lalpha.length ; i++) {
        encoding.put(Lalpha[i], l.get(i));
        decoding.put(l.get(i), Lalpha[i]);
    }
    

    Now lets say we wanted to encode / decode the string helloworld, we would do this:

    String s = "helloworld";    
    
    // Encode:
    String enc = "";
    for (char c : s.toCharArray())
        enc += encoding.get(c);
    
    System.out.println(enc);
    
    // Decode:
    String dec = "";
    for (char c : enc.toCharArray())
        dec += decoding.get(c);
    
    System.out.println(dec);
    

    Output (one of many possible):

    vjwwmtmcwz
    helloworld

    Of course, you can incorporate uppercase letters and what-not using the same idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.