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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:40:10+00:00 2026-06-18T03:40:10+00:00

My Code import java.security.*; import java.security.spec.InvalidKeySpecException; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import sun.misc.*; import org.apache.commons.codec.binary.Base64;

  • 0

My Code

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.*;

import org.apache.commons.codec.binary.Base64;

public class FrontierCipher
{
    private static final String ALGO = "AES";
    private static String keyString = "00112233445566778899AABBCCDDEEFF0123456789ABCDEF0123456789ABCDEF";

     private static Key generateKey() throws Exception 
     {
        Key key = new SecretKeySpec(convertToByteArray(keyString), ALGO);
        return key;
    }
    public static byte[] encryptBytes(byte[] data) throws Exception
    {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);

        byte[] encVal = c.doFinal(data);
        byte[] encryptedValue = Base64.encodeBase64(encVal);

        return encryptedValue;
    }

    public static byte[] decrpytBytes(byte[] encryptedData) throws Exception
    {   
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);

        byte[] decordedValue = Base64.decodeBase64(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);

        return decValue;
    }

    public static byte[] convertToByteArray(String key) throws KeySizeException
    {
        if(key.length()<64)
            throw new KeySizeException("Key must contain 64 characters");

        byte[] b = new byte[32];

        for(int i=0, bStepper=0; i<key.length()+2; i+=2)
            if(i !=0)
                b[bStepper++]=((byte) Integer.parseInt((key.charAt(i-2)+""+key.charAt(i-1)), 16));

        return b;
    }


    public static void main(String[] args) throws Exception
    {
        byte[] password  = {6,75,3};
        byte[] passwordEnc = encryptBytes(password);
        byte[] passwordDec = decrpytBytes(passwordEnc);

        System.out.println("Plain Text : " + password[0]+" "+ password[1]+" "+ password[2]);
        System.out.println("Encrypted Text : " + passwordEnc[0]+" "+ passwordEnc[1]+" "+ passwordEnc[2]);
        System.out.println("Decrypted Text : " + passwordDec[0]+" "+passwordDec[1]+" "+passwordDec[2]);
    }
}

When run locally

Plain Text : 6 75 3
Encrypted Text : 74 43 117
Decrypted Text : 6 75 3

When run, waiting for udp packet to come I get this

   javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)

TestServer Using

private static void udpServer()
    {
        try
        {      //This is whats coming in ==> byte[] password={6,75,3};

            DatagramSocket serverSocket = new DatagramSocket(18000);
            byte[] receiveData = new byte[64];

            while (true)
            {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);

                byte[] b = decrpytBytes(receivePacket.getData());
            }
        }
        catch(Exception e)
        {
            //blahh
        }
    }
  • 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-18T03:40:11+00:00Added an answer on June 18, 2026 at 3:40 am

    OK, this is a terrible example that I will update later, I will have to sleep.

    Note that using ECB encoding is always unsafe for normal data. CBC encoding is terribly unsafe when there is a possibility of a man-in-the-middle attack (padding Oracle attack). But it should work. If you have an updated Java 7, try and use "AES/GCM/NoPadding" and don’t forget to send the randomized IV to the other side as well.

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;
    import java.nio.charset.Charset;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    public class UDP_AES {
    
    
        /**
         * @param args
         * @throws SocketException 
         */
        public static void main(String[] args) throws SocketException {
    
    
            Thread t = new Thread(new Runnable() {
                DatagramSocket socket;
                byte[] receiveData = new byte[1024];
                Cipher aesCipher;
    
                {
                    try {
                        socket = new DatagramSocket(18000);
                        aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                        IvParameterSpec zeroIV = new IvParameterSpec(new byte[aesCipher.getBlockSize()]);
                        SecretKey key = new SecretKeySpec(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,}, "AES");
                        aesCipher.init(Cipher.DECRYPT_MODE, key, zeroIV);
                    } catch (Exception e) {
                        throw new IllegalStateException("Could not create server socket or cipher", e);
                    }
    
                }
    
                @Override
                public void run() {
                    while (true) {
                        DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
    
                        try {
                            socket.receive(packet);
                            final byte[] plaintextBinary = aesCipher.doFinal(packet.getData(), 0, packet.getLength());
                            String plaintext = new String(plaintextBinary, Charset.forName("UTF-8"));
                            System.out.println(plaintext);
                        } catch (Exception e) {
                            throw new IllegalStateException("Could not receive or decrypt packet");
                        }
                    }
                }
    
            });
            t.start();
    
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // yeah, whatever
            }
    
            DatagramSocket sendingSocket = new DatagramSocket();
            String plaintext = "1234";
            byte[] plaintextBinary = plaintext.getBytes(Charset.forName("UTF-8"));
    
            try {
                Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                SecretKey key = new SecretKeySpec(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,}, "AES");
                IvParameterSpec zeroIV = new IvParameterSpec(new byte[aesCipher.getBlockSize()]);
                aesCipher.init(Cipher.ENCRYPT_MODE, key, zeroIV);
                final byte[] ciphertextBinary = aesCipher.doFinal(plaintextBinary);
    
            DatagramPacket sendingPacket = new DatagramPacket(ciphertextBinary, ciphertextBinary.length);
            sendingPacket.setSocketAddress(new InetSocketAddress("localhost", 18000));
                sendingSocket.send(sendingPacket);
            } catch (Exception e) {
                throw new IllegalStateException("Could not send or encrypt", e);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code: import org.apache.commons.codec.binary.Base64; import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import
I tried the following code: import java.math.BigInteger; import org.apache.commons.codec.binary.Base32; import org.junit.Test; public class Sandbox
MCrypt: import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class MCrypt
I use the following code to test the BouncyCastle crypto library: import java.security.Security; public
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I am working on the following code: import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import
Really a buggy question , hee is the code: import java.io.IOException; import java.security.*; import
Java applet code package M257Applet import java.applet.*; import javax.swing.*; import java.awt.*; public class HellowApplet
Here is my code import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.sql.*;
I got the following code: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Sha1{ private static

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.