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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:53:19+00:00 2026-06-17T08:53:19+00:00

I’m new to security, and I tried a lot to remove the exception (below

  • 0

I’m new to security, and I tried a lot to remove the exception (below the code). Both the RC2 and RC6 ciphers are giving this exception. The input should be a 128 bit String and a key of 128 bits, the output should be 128 bits cipher text.

import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;

public class RC2Encrypt
{
    public static void main(String args []) throws Exception
    {
        Scanner s=new Scanner(System.in);
        System.out.println("Enter PlainTextString:");
        String input=s.nextLine();

        System.out.println();
        System.out.println("Enter 16 digit key:");
        String strPassword=s.nextLine();

        SecretKeySpec key = new SecretKeySpec(strPassword.getBytes(), "RC2");
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());
        Cipher cipher =  Cipher.getInstance("RC2");
        cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

        byte[] encrypted = cipher.doFinal(input.getBytes());

        String b1 = new String(encrypted);
        System.out.println("Original string: " + input);
        System.out.println("Encrypted string: " + b1);
    }
}

This will generate the following exception:

Exception in thread "main" java.security.InvalidAlgorithmParameterException: Wrong IV    length: must be 8 bytes long
    at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)
    at com.sun.crypto.provider.RC2Cipher.engineInit(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at RC2Encrypt.main(RC2Encrypt.java:40) 
  • 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-17T08:53:20+00:00Added an answer on June 17, 2026 at 8:53 am

    There are multiple issues with your code.

    The most important one is that you confuse characters and bytes. You will have to know about character-encoding – which is about encoding text in binary and back – and encoding – which is about encoding binary as readable text.

    If you expect 8 bytes encoded by 16 characters then this implies that you need to perform hexadecimal decoding (which uses two characters for each byte). The best way of doing that is to use the Apache common codec library or the Bouncy Castle libraries. I would suggest the latter because it contains many more encryption algorithms – including RC6. The best way is to use the Bouncy Castle provider for the cryptographic functionality.

    OK, so that solved the part with the key and IV, now for the plain text. The plain text must be binary encoded. Unfortunately you are now using the platform default character set. This means you will run into compatibility issues if you want to exchange it with another system (or other application, even on your own PC, if it uses another character set). To remove this issue you should specify an encoding yourself, e.g. use String.getBytes(Charset.forName("UTF-8")) or on Java 7 onward String.getBytes(StandardCharsets.UTF_8). Do the same for the constructor.

    Furthermore, you really should not be using the same bytes for the key and IV. The IV is used to encrypt several ciphertext with the same key, without introducing security vulnerabilities. E.g. an attacker could know that the first, not so secret ciphertext contains the word “yes”. Then the next cipherblock comes along and contains the exact same bytes; obviously the sender has send the word “yes” again. If the IV was changed the attacker would see an entirely different cipher text – although it would still be 3 bytes long for stream ciphers.

    Finally, the key and IV should be randomly generated – possibly at some time in the past. If a password is required instead of a key, you should use a PBKDF (password based key derivation function) as a sub-optimal replacement. But I think that might be a more advanced topic.

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

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
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.