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

  • Home
  • SEARCH
  • 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 6818407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:10:27+00:00 2026-05-26T21:10:27+00:00

I have the following test code to encrypt and decrypt a string. It works

  • 0

I have the following test code to encrypt and decrypt a string. It works fine if I leave out the wrapping and unwrapping code for my key in test(), but when I try to wrap my key, and then unwrap it again and use it for the decryption, it fails and I don’t get “Test” back as the resulting decrypted string but “�J��” instead.

Does anybody see the error I’m doing with the wrapping and unwrapping? Thanks.

private static void test() throws Exception {

    // create wrap key
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AESWrap");
    keyGenerator.init(256);
    Key wrapKey = keyGenerator.generateKey();

    SecretKey key = generateKey(PASSPHRASE);
    Cipher cipher;

    // wrap key
    cipher = Cipher.getInstance("AESWrap");
    cipher.init(Cipher.WRAP_MODE, wrapKey);
    byte[] wrappedKeyBytes = cipher.wrap(key);

    // unwrap key again
    cipher.init(Cipher.UNWRAP_MODE, wrapKey);
    key = (SecretKey)cipher.unwrap( wrappedKeyBytes, "AES/CTR/NOPADDING", Cipher.SECRET_KEY);

    // encrypt
    cipher = Cipher.getInstance("AES/CTR/NOPADDING");
    cipher.init(Cipher.ENCRYPT_MODE, key, generateIV(cipher), random);
    byte[] b = cipher.doFinal("Test".toString().getBytes());

    // decrypt
    cipher = Cipher.getInstance("AES/CTR/NOPADDING");
    cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher), random);
    b = cipher.doFinal(b);

    System.out.println(new String(b));  
    // should output "Test", but outputs �J�� if wrapping/unwrapping

}

And the two helper methods that are called in the code above:

private static IvParameterSpec generateIV(Cipher cipher) throws Exception {
    byte [] ivBytes = new byte[cipher.getBlockSize()];
    random.nextBytes(ivBytes);    // random = new SecureRandom();
    return new IvParameterSpec(ivBytes);
}

private static SecretKey generateKey(String passphrase) throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes(), iterations, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBE_ALGORITHM); //"PBEWITHSHA256AND256BITAES-CBC-BC"
    return keyFactory.generateSecret(keySpec);
}
  • 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-05-26T21:10:28+00:00Added an answer on May 26, 2026 at 9:10 pm

    It looks like you’re giving a different IV to cipher.init(Cipher.ENCRYPT_MODE, …) and cipher.init(Cipher.DECRYPT_MODE, …) by calling generateIV() twice.

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

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: test :: String -> Bool test g = True
I have downloaded and installed linq-to-sqlite from http://sqlite.phxsoftware.com/ But the following test code: [
Hi I have following test code: class Program { static void Main(string[] args) {
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I have the following test code to search a generic list: public void DoSearch(string
I have the following simple test code at my server http handler: String cookieString
So I have the following test code: #include <string.h> #include <stdio.h> int main(int argc,
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
I have the following code: [test.cpp] #include <mcrypt.h> #include <string> #include <iostream> #include <vector>

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.