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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:42:32+00:00 2026-05-24T13:42:32+00:00

I’m trying to support PBE for AES, Serpent, and TwoFish. Currently I am able

  • 0

I’m trying to support PBE for AES, Serpent, and TwoFish. Currently I am able to generate an AES PBEKey in Java using BC like this:

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC", provider);
PBEKeySpec pbeKeySpec = new PBEKeySpec("Password12".toCharArray());
SecretKey key = factory.generateSecret(pbeKeySpec);

but I can’t figure out how to generate a PBEKey for Serpent, so I’m assuming its not possible out of the box. How would I go about implementing this? Is there a hook somewhere that I can just register my own SecretKeyFactory to handle Serpent keys?

Coincidentally, I have noticed that using an AES PBEKey (as generated above) for encrypting/decrypting with Serpent/TwoFish “works”, but I have no idea what the repercussions are. Could I just get away with using the AES PBEKey?

  • 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-24T13:42:34+00:00Added an answer on May 24, 2026 at 1:42 pm

    After discussions with PaŭloEbermann (above), I put together the following solution. It generates a PBE key for AES256 and then simply copies the required number of bytes from the generated key into a new SecretKeySpec(), which allows me to specify the desired algorithm and key length. Currently I am salting the password AND creating a random IV on each call to encrypt. My assumption is that the IV is unnecessary since a random salt is applied to each encrypted message, but I wasn’t 100% sure so I added the IV anyway. I’m hoping someone can confirm or deny this assumption, since if the IV isnt needed, then its bloating the size of the output from encrypt() for no valid reason. Ideally I would be able to generate a PBEKey of variable length with no algorithm ties (as per PKCS5), but it appears I am bound to the key sizes defined in the available PBE schemes provided by the selected Provider. This implementation is therefore bound to using BouncyCastle, since I was unable to find a PBE scheme that provided at least 256bit keys from the standard JCE provider.

    /**
     * parts of this code were copied from the StandardPBEByteEncryptor class from the Jasypt (www.jasypt.org) project
     */
    public class PBESample {
        private final String KEY_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
        private final String MODE_PADDING = "/CBC/PKCS5Padding";
        private final int DEFAULT_SALT_SIZE_BYTES = 16;
        
        private final SecureRandom rand;
        
        private final String passwd = "(Password){12}<.....>!";
    
        public PBESample() throws Exception {
            rand = SecureRandom.getInstance("SHA1PRNG");
        }
        
        private byte[] generateSalt(int size) {
            byte[] salt = new byte[size];
            rand.nextBytes(salt);
            
            return salt;
        }
        
        private SecretKey generateKey(String algorithm, int keySize, byte[] salt) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException{
            SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
            PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd.toCharArray(), salt, 100000);
            SecretKey tmpKey = factory.generateSecret(pbeKeySpec);
            byte[] keyBytes = new byte[keySize / 8];
            System.arraycopy(tmpKey.getEncoded(), 0, keyBytes, 0, keyBytes.length);
            
            return new SecretKeySpec(keyBytes, algorithm);
        }
        
        private byte[] generateIV(Cipher cipher) {
            byte[] iv = new byte[cipher.getBlockSize()];
            rand.nextBytes(iv);
            
            return iv;
        }
    
        private byte[] appendArrays(byte[] firstArray, byte[] secondArray) {
            final byte[] result = new byte[firstArray.length + secondArray.length];
            
            System.arraycopy(firstArray, 0, result, 0, firstArray.length);
            System.arraycopy(secondArray, 0, result, firstArray.length, secondArray.length);
            
                return result;
        }
    
        
        public byte[] encrypt(String algorithm, int keySize, final byte[] message) throws Exception {
            Cipher cipher = Cipher.getInstance(algorithm + MODE_PADDING);
    
            // The salt size for the chosen algorithm is set to be equal 
            // to the algorithm's block size (if it is a block algorithm).
            int saltSizeBytes = DEFAULT_SALT_SIZE_BYTES;
            int algorithmBlockSize = cipher.getBlockSize();
            if (algorithmBlockSize > 0) {
                saltSizeBytes = algorithmBlockSize;
            }
    
            // Create salt
            final byte[] salt = generateSalt(saltSizeBytes);
    
            SecretKey key = generateKey(algorithm, keySize, salt);
    
            // create a new IV for each encryption
            final IvParameterSpec ivParamSpec = new IvParameterSpec(generateIV(cipher));
    
            // Perform encryption using the Cipher
            cipher.init(Cipher.ENCRYPT_MODE, key, ivParamSpec);
            byte[] encryptedMessage = cipher.doFinal(message);
    
            // append the IV and salt
            encryptedMessage = appendArrays(ivParamSpec.getIV(), encryptedMessage);
            encryptedMessage = appendArrays(salt, encryptedMessage);
    
            return encryptedMessage;
        }
        
        public byte[] decrypt(String algorithm, int keySize, final byte[] encryptedMessage) throws Exception {
            Cipher cipher = Cipher.getInstance(algorithm + MODE_PADDING);
            
            // determine the salt size for the first layer of encryption
            int saltSizeBytes = DEFAULT_SALT_SIZE_BYTES;
            int algorithmBlockSize = cipher.getBlockSize();
            if (algorithmBlockSize > 0) {
                saltSizeBytes = algorithmBlockSize;
            }
            
            byte[] decryptedMessage = new byte[encryptedMessage.length];
            System.arraycopy(encryptedMessage, 0, decryptedMessage, 0, encryptedMessage.length);
    
            // extract the salt and IV from the incoming message
            byte[] salt = null;
            byte[] iv = null;
            byte[] encryptedMessageKernel = null;
            final int saltStart = 0;
            final int saltSize = (saltSizeBytes < decryptedMessage.length ? saltSizeBytes : decryptedMessage.length);
            final int ivStart = (saltSizeBytes < decryptedMessage.length ? saltSizeBytes : decryptedMessage.length);
            final int ivSize = cipher.getBlockSize();
            final int encMesKernelStart = (saltSizeBytes + ivSize < decryptedMessage.length ? saltSizeBytes + ivSize : decryptedMessage.length);
            final int encMesKernelSize = (saltSizeBytes + ivSize < decryptedMessage.length ? (decryptedMessage.length - saltSizeBytes - ivSize) : 0);
    
            salt = new byte[saltSize];
            iv = new byte[ivSize];
            encryptedMessageKernel = new byte[encMesKernelSize];
    
            System.arraycopy(decryptedMessage, saltStart, salt, 0, saltSize);
            System.arraycopy(decryptedMessage, ivStart, iv, 0, ivSize);
            System.arraycopy(decryptedMessage, encMesKernelStart, encryptedMessageKernel, 0, encMesKernelSize);
            
            SecretKey key = generateKey(algorithm, keySize, salt);
            
            IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
    
            // Perform decryption using the Cipher
            cipher.init(Cipher.DECRYPT_MODE, key, ivParamSpec);
            decryptedMessage = cipher.doFinal(encryptedMessageKernel);
    
            // Return the results
            return decryptedMessage;
        }
    
        public static void main(String[] args) throws Exception {
            // allow the use of the BC JCE
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            
            final String message = "Secret Message";
            PBESample engine = new PBESample();
            
            byte[] encryptedMessage = engine.encrypt("AES", 128, message.getBytes());
            byte[] decryptedMessage = engine.decrypt("AES", 128, encryptedMessage);
            if (message.equals(new String(decryptedMessage))) {
                System.out.println("AES OK");
            }
            
            encryptedMessage = engine.encrypt("Serpent", 256, message.getBytes());
            decryptedMessage = engine.decrypt("Serpent", 256, encryptedMessage);
            if (message.equals(new String(decryptedMessage))) {
                System.out.println("Serpent OK");
            }
            
            encryptedMessage = engine.encrypt("TwoFish", 256, message.getBytes());
            decryptedMessage = engine.decrypt("TwoFish", 256, encryptedMessage);
            if (message.equals(new String(decryptedMessage))) {
                System.out.println("TwoFish OK");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
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
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.