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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:55:57+00:00 2026-06-02T14:55:57+00:00

I have this simple function for encrypting strings via Vigenère in Java. I omitted

  • 0

I have this simple function for encrypting strings via Vigenère in Java. I omitted the decryption as this is just a “-” instead of the “+” in the line where the new value is calculated.

But this function works only for the normal alphabet A-Z. How can I change the function so that it supports lowercase letters as well as uppercase letters and all other UTF-8 chars?

public static String vigenere_encrypt(String plaintext, String key) {
    String encryptedText = "";
    for (int i = 0, j = 0; i < plaintext.length(); i++, j++) {
        if (j == key.length()) { j = 0; } // use key again if end reached
        encryptedText += (char) ((plaintext.charAt(i)+key.charAt(j)-130)%26 + 65);
    }
    return encryptedText;
}

Thank you very much for your help!

  • 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-02T14:55:59+00:00Added an answer on June 2, 2026 at 2:55 pm

    Another answer, that does do the Vigenere cipher on upper & lower case characters, simply inserting the other characters. Use this technique to create multiple groups of characters to encode.

    public static String vigenere(String plaintext, String key, boolean encrypt) {
    
        final int textSize = plaintext.length();
        final int keySize = key.length();
    
        final int groupSize1 = 'Z' - 'A' + 1; 
        final int groupSize2 = 'z' - 'a' + 1;
        final int totalGroupSize = groupSize1 + groupSize2;
    
        final StringBuilder encryptedText = new StringBuilder(textSize);
        for (int i = 0; i < textSize; i++) {
            final char plainChar = plaintext.charAt(i);
    
            // this should be a method, called for both the plain text as well as the key
            final int plainGroupNumber; 
            if (plainChar >= 'A' && plainChar <= 'Z') {
                plainGroupNumber = plainChar - 'A';
            } else if (plainChar >= 'a' && plainChar <= 'z') {
                plainGroupNumber = groupSize1 + plainChar - 'a';
            } else {
                // simply leave spaces and other characters
                encryptedText.append(plainChar);
                continue;
            }
    
            final char keyChar = key.charAt(i % keySize);
            final int keyGroupNumber; 
            if (keyChar >= 'A' && keyChar <= 'Z') {
                keyGroupNumber = keyChar - 'A';
            } else if (keyChar >= 'a' && keyChar <= 'z') {
                keyGroupNumber = groupSize1 + keyChar - 'a';
            } else {
                throw new IllegalStateException("Invalid character in key");
            }
    
            // this should be a separate method
            final int cipherGroupNumber;
            if (encrypt) {
                cipherGroupNumber = (plainGroupNumber + keyGroupNumber) % totalGroupSize;
            } else {
                // some code to go around the awkward way of handling % in Java for negative numbers
                final int someCipherGroupNumber = plainGroupNumber - keyGroupNumber;
                if (someCipherGroupNumber < 0) {
                    cipherGroupNumber = (someCipherGroupNumber + totalGroupSize);
                } else {
                    cipherGroupNumber = someCipherGroupNumber;
                }
            }
    
            // this should be a separate method
            final char cipherChar;
            if (cipherGroupNumber < groupSize1) {
                cipherChar = (char) ('A' + cipherGroupNumber);
            } else {
                cipherChar = (char) ('a' + cipherGroupNumber - groupSize1);
            }
            encryptedText.append(cipherChar);
        }
    
        return encryptedText.toString();
    }
    

    Again, this is unsafe code as the cipher used has been broken for ages. Don’t use too many ‘A’ characters in your keys 🙂 But the character encoding should be sound.

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

Sidebar

Related Questions

I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I have this simple function which I pass in an array of strings: function
I have this simple java function that is listening to one IP, using sockets:
I have this simple jQuery code $(document).ready(function() { $('#test').attr('value','hello world'); $('#test1').text('hello world'); }); And
I have this simple function to check if a value is in list: template
I have this simple function: <script type=text/javascript> //<![CDATA[ jQuery(function($){ function here(b){alert(b);} ; here(6); });
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
So, I have this simple code $('#info_name'+index).show(slide, {direction: right}, 1000, function(){ $('#info_info'+index).show('slide',{direction: up}, 1000);
I have a simple function about neural network. This function gets a matrix, loads
I have a simple html multiline tooltip implementation: this.tooltip = function(tag) { xOffset =

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.