• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

in 3des algorithm in java iam getting error IllegalBlockSizeException: Input length must be multiple

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is my encryption file where at x****** iam getting this error

package com.masa.utils;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Properties;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;



public class Des_3_Algo {

 public static byte[] encrypt(String message) throws Exception {
       final MessageDigest md = MessageDigest.getInstance("md5");
       final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
               .getBytes("utf-8"));
       final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
       for (int j = 0, k = 16; j < 8;) {
           keyBytes[k++] = keyBytes[j++];
       }

       final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
       final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
       final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
       cipher.init(Cipher.ENCRYPT_MODE, key, iv);

       final byte[] plainTextBytes = message.getBytes("utf-8");
       final byte[] cipherText = cipher.doFinal(plainTextBytes);

       return cipherText;
   }

   public static String decrypt(byte[] message) throws Exception {
       final MessageDigest md = MessageDigest.getInstance("md5");
       final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
               .getBytes("utf-8"));
       final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
       for (int j = 0, k = 16; j < 8;) {
           keyBytes[k++] = keyBytes[j++];
       }

       final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
       final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
       final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
       decipher.init(Cipher.DECRYPT_MODE, key, iv);
     
       final byte[] plainText = decipher.doFinal(message);--------->x******

       return new String(plainText, "UTF-8");
   }
   
 
}
Staff note (Paul Clapham) :

This doesn't look like a "beginning" problem so I moved the thread to Java In General.

Staff note (Paul Clapham) :

Also it looks like a "security" issue so I added it to the Security forum.

 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code works for me.
Can you post a main method that calls the methods and shows the error?
 
Saloon Keeper
Posts: 13280
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using Triple DES? Why are you not using the IV properly?
 
reply
    Bookmark Topic Watch Topic
  • New Topic