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 8985601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:22:00+00:00 2026-06-15T21:22:00+00:00

I’m finished with the plugin finally, and started working on another project. That project

  • 0

I’m finished with the plugin finally, and started working on another project. That project is a simple bit of software to encrypt a string given an encryption key and the string itself. So I wrote that, and it seems to be working fine, but I can’t figure out how to decrypt it. I used to have an array with the encrypted alphabet, but I figured out a way to do without that in the encryption function and there should be a way to do without it in the decryption as well.

My encryption function:

public static String e(String toEncrypt, int encKey) {
    encKey %= ALPHABET.length;
    toEncrypt = toEncrypt.toLowerCase();
    char[] TEChar = toEncrypt.toCharArray();
    for (int i = 0; i < toEncrypt.length(); i++) {
        for (int j = 0; j < ALPHABET.length; j++) {
            if (TEChar[i] == '`') {
                TEChar[i] = '_';
            }
            else if (TEChar[i] == ALPHABET[j]) {
                TEChar[i] = ALPHABET[(j + encKey) % ALPHABET.length];
                break;
            }
        }
    }
    toEncrypt = String.valueOf(TEChar) + "`" + encKey;
    return toEncrypt;
}

ALPHABET (the array, and I realize that it contains more than just the alphabet.):

 char[] ALPHABET = {'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', ' ', '1',
'2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.', ';', ':', '[', ']', '{',
'}', '?'};

Rather than making a new array just to have the offset characters, I did that with a simple addition and use of the remainder operator. I tried using subtraction and the same thing, but it didn’t work as I would need it to wrap around to 26. Somehow I get the feeling that something glaringly obvious is the solution.

  • 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-15T21:22:02+00:00Added an answer on June 15, 2026 at 9:22 pm

    Try this:

    public static String e(String toEncrypt, int encKey, boolean addEncKey) {       
        encKey %= ALPHABET.length;
        toEncrypt = toEncrypt.toLowerCase();
        char[] TEChar = toEncrypt.toCharArray();
        for (int i = 0; i < toEncrypt.length(); i++) {
            for (int j = 0; j < ALPHABET.length; j++) {
                if (TEChar[i] == '`') {
                    TEChar[i] = '_';
                } else if (TEChar[i] == ALPHABET[j]) {
                    TEChar[i] = ALPHABET[(j + encKey + ALPHABET.length)
                            % ALPHABET.length];
                    break;
                }
            }
        }
        if (addEncKey) {
            toEncrypt = String.valueOf(TEChar) + "`" + encKey;
        } else {
            toEncrypt = String.valueOf(TEChar);
        }
        return toEncrypt;
    }
    

    This how I tested it:

    public static void main(String[] args) {
            String encoded = e("ABDSDfz}", 2, true);
            System.out.println(encoded);
            String decoded = e(
    encoded.substring(0, encoded.indexOf("`")),
                    -Integer.parseInt(encoded.substring(encoded.indexOf("`") + 1)),
                    false);
            System.out.println(decoded);
    
        }
    

    As you can see I moved some logic to method invocation (you can easily write a decryption function that wrapes around encryption like this:

    public static String decrypt(String encoded) {
    return e(encoded.substring(0, encoded.indexOf("`")),
                        -Integer.parseInt(encoded.substring(encoded.indexOf("`") + 1)),
                        false);
    }
    

    The decrypion and encryption works basically in the same way but decryption uses opposite encKey (for example instead of 22 it will use -22), this done in invocation. You add encKey after ‘`’ sign – I didn’t wanted that in decoded message so I cut it out when parsing encoded string to method and added booleam parameter addEncKey so I can disable adding addEncKey.
    I also changed in the code this line:
    TEChar[i] = ALPHABET[(j + encKey + ALPHABET.length)
    % ALPHABET.length];
    This is an easy way to deal with negative indexes that can happen when encKey is negative.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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 just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.