cryptix

Stanciu
Stanciu used Ask the Experts™
on
Hello!
Any idea why this use of Cryptix (www.cryptix.org) for DES does not works
for text length that is not divisible with 8?
Do you have any idea?

Here is the code:

import java.io.*;
import java.security.*;
import java.math.*;
import cryptix.util.core.BI;
import cryptix.util.core.ArrayUtil;
import cryptix.util.core.Hex;
import cryptix.provider.key.*;
import xjava.security.Cipher;

class runDES {

 private static int iPadNo = -1;
 public runDES(){
 }

public static void main (String[] args) {

 try {

  byte[] b = "e provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robt".getBytes();
          System.out.println("Initial values");
          int bLen = b.length;
          for (int i=0; i<bLen; i++)
               System.out.print(b[i]);
          System.out.println("\n");
               
          byte[] bEnc = desCrypt( b, "cucucucu", 319, true );
          byte[] bDec = desCrypt( bEnc, "cucucucu",319 , false );      } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
      }

 }

      public static byte[] desCrypt(byte[] bInBuff,
                                          String sKey,
                                          int len,
                                          boolean bCryptDirection)
     throws Exception{          
          byte [] bOutBuff;
         
          byte [] bKey;
          String sOutBuff;
     
         
          /* get byte[] values from Strings */          
                    bKey                    = sKey.getBytes();
                   
          for ( int i = 0; i < bKey.length; i++){
               bKey[ i ]           = (byte)( bKey[ i ] << 1);
          }
          /* generate byte[] keys */
          RawSecretKey key2      = new RawSecretKey( "DES/ECB/OneAndZeroes", bKey );
          RawKey rkey           = (RawKey) key2;          
         
         
          bKey                     = rkey.getEncoded();
         
                    Cipher des               = Cipher.getInstance( "DES/ECB/OneAndZeroes","Cryptix" );
          bOutBuff               = new byte[ bInBuff.length ];
          for( int i = 0; i< bInBuff.length; i++ ){
               bOutBuff[ i ]      = 0x0;
          }
         
          /* Encrypt or decrypt */
          if( bCryptDirection ){
          des.initEncrypt( key2 );    
               bOutBuff           = des.crypt( bInBuff );
               
                              int bLen =bOutBuff.length;
          for (int i=0; i<bLen; i++)
               System.out.print(bOutBuff[i]);
          System.out.println("\n");
          /// ----------end crypt
          }
          else{                              
               des.initDecrypt( key2 );    
               bOutBuff           = des.crypt( bInBuff );
         
                              int bLen = bOutBuff.length;
          for (int i=0; i<bLen; i++)
               System.out.print(bOutBuff[i]);
          System.out.println("\n");
          /// ----------end decrypt
          }
         
          return bOutBuff;          
     }
     
}



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
DES is a block encoder. It always process chunks of 8 Bytes. If a block has a length of less than 8 Bytes (the last one) padding is used. There are different padding schemes, read the appropiate literature to learn about it.

You gave a Padding-scheme "OnesAndZeros". Doesn't talk to me. The most common padding is "PKCS5Padding".

Author

Commented:
HI!

Using any of the three the padding schemes, the crypt and decrypt must works if the API is properly used. I have two problems:

  1: Some strings that I encrypt and then decrypt don't decript entirely correct (ony ~80% correct, with "shadow" 8 bytes regions)

  2: I have the algorith DES developed by Eric Young under C, and I must decrypt the message crypted with that algorithm with a Java program and it seems that the cryptet strings for a given input string are not the same.

However, I must solve first the 1st problem.
Thanx a lot for your answer, I thoght that I am the only one that heard about cryptix (kidding :)). I tryed on comp.java.lang.security and comp.java.lang.programmer but I received no answer.

Do you have any idea where can I search further?

Thanx again, H

Commented:
I used cryptix 2 years ago, maybe something has changed since then. I was not able to compile yout code to test it. Which Version of cryptix do use?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
I use Cryptix 3.2.0 with JDK 1.3.0.

You have to add in <JAVA_HOME>\jre\lib\security\java.security

security.provider.2=cryptix.provider.Cryptix

The Cryptix 3.2.0 library is 1.9 MB.

H

Author

Commented:
The right code is this


import java.io.*;
import java.security.*;
import java.math.*;
import cryptix.util.core.BI;
import cryptix.util.core.ArrayUtil;
import cryptix.util.core.Hex;
import cryptix.provider.key.*;
import xjava.security.Cipher;
import cryptix.provider.padding.*;

class dub {

      private static int iPadNo = -1;
      public dub(){
      }
      
public static void main (String[] args) {

      try {

      
            //String sEnc = desCrypt( "mam##~!@# mia vai ****** ******(((((())))))@@!!$$##%%^^%%**&&(( ce placere", "cucucucu", 8, true );
            //String sEnc = desCrypt( "desinitDecrypt(key2);ciphertext = des crypt(ciphertext); System out print( n Systemoutprintln(plaintextlength = ciphertextlength", "cucucucu", 8, true );
            //String sEnc = desCrypt( "The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.", "cucucucu", 8, true );
            //String sEnc = desCrypt( "l implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results.", "cucucucu", 115, true );
            //String sEnc = desCrypt( "Ana are pere mic", "cucucucu", 16, true );
            /*byte[] b = "e provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robt".getBytes();
            System.out.println("Initial values");
            int bLen = b.length;
            for (int i=0; i<bLen; i++)
                  System.out.print(b[i]);
            System.out.println("\n");
                  
            byte[] bEnc = desCrypt( b, "cucucucu", 638, true );
            byte[] bDec = desCrypt( bEnc, "cucucucu",638 , false );*/
            
            
            String sEnc = desCrypt( "l implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results.", "cucucucu", 115, true );
            //String sEnc = desCrypt( "e provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robthe provides robt", "cucucucu", 319, true );
            String sDec = desCrypt( sEnc, "cucucucu",115 , false );

      } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
      }

      }
      
      public static String desCrypt(String sInBuff,
                                                  String sKey,
                                                  int len,
                                                  boolean bCryptDirection)
      throws Exception{            
            byte [] bOutBuff;
            byte [] bInBuff;
            byte [] bKey;
            String sOutBuff;
            //String sPaddedInBuff;
            //String sPadder            = new String( "0000000" );
            
            
            /* Padding operation */
      
            
            bInBuff = new byte[ sInBuff.length() ];
            
            for(int i = 0; i < sInBuff.length(); i++ ){
                  bInBuff[ i ] = 0x0;      
            }
                  
            bInBuff                   = sInBuff.getBytes();      
            
            bKey = sKey.getBytes();
            
            for ( int i = 0; i < bKey.length; i++){
                  bKey[ i ]             = (byte)( bKey[ i ] << 1);
            }
            
            RawSecretKey key2       = new RawSecretKey( "DESECB/OneAndZeroes", bKey );
            RawKey rkey             = (RawKey) key2;
            
            
            
            bKey                         = rkey.getEncoded();
            
            
            
            Cipher des                  = Cipher.getInstance( "DES/ECB/OneAndZeroes","Cryptix" );
            bOutBuff                  = new byte[ bInBuff.length ];
            for( int i = 0; i< bInBuff.length; i++ ){
                  bOutBuff[ i ]       = 0x0;
            }
            
            /* Encrypt or decrypt */
            if( bCryptDirection ){
                  des.initEncrypt( key2 );      
                  bOutBuff             = des.crypt( bInBuff );
                  
                  int i                    = bOutBuff.length;
                  sOutBuff             = new String( bOutBuff,0,i );
                  System.out.println( "Crypted: "+ sOutBuff);
            /// ----------end crypt
            }
            else{                                    
                  des.initDecrypt( key2 );      
                  bOutBuff             = des.crypt( bInBuff );
            
                  //int i                    = bOutBuff.length;                  
                  sOutBuff             = new String( bOutBuff );
                  System.out.println( "Decrypted: "+sOutBuff );
            /// ----------end decrypt
            }
            
            return sOutBuff;            
      }
      
      
      public static byte[] desCrypt(byte[] bInBuff,
                                                  String sKey,
                                                  int len,
                                                  boolean bCryptDirection)
      throws Exception{            
            byte [] bOutBuff;      
            byte [] bKey;
            String sOutBuff;
      
            
                        
            //bInBuff                   = sInBuff.getBytes();
            bKey                        = sKey.getBytes();
            
            
            for ( int i = 0; i < bKey.length; i++){
                  bKey[ i ]             = (byte)( bKey[ i ] << 1);
            }
            /* generate byte[] keys */
            RawSecretKey key2       = new RawSecretKey( "DES/ECB/OneAndZeroes", bKey );
            RawKey rkey             = (RawKey) key2;            
            
            
            bKey                         = rkey.getEncoded();
            
            
            Cipher des                  = Cipher.getInstance( "DES/ECB/OneAndZeroes","Cryptix" );
            bOutBuff                  = new byte[ bInBuff.length ];
            for( int i = 0; i< bInBuff.length; i++ ){
                  bOutBuff[ i ]       = 0x0;
            }
            
            /* Encrypt or decrypt */
            if( bCryptDirection ){

                  des.initEncrypt( key2 );      
                  bOutBuff             = des.crypt( bInBuff );
                  
                  
                  int bLen =bOutBuff.length;
            for (int i=0; i<bLen; i++)
                  System.out.print(bOutBuff[i]);
            System.out.println("\n");
            /// ----------end crypt
            }
            else{                                    
                  des.initDecrypt( key2 );      
                  bOutBuff             = des.crypt( bInBuff );
            
                  
                  int bLen = bOutBuff.length;
            for (int i=0; i<bLen; i++)
                  System.out.print(bOutBuff[i]);
            System.out.println("\n");
            /// ----------end decrypt
            }
            
            return bOutBuff;            
      }
      
}
Commented:
The problem is that you must not store the encryted stuff as String.

I inserted:
               byte[] tmp = sOutBuff.getBytes();
               for (int ii=0;ii<tmp.length;ii++)
               {
                 if (tmp[ii]!=bOutBuff[ii])
                 System.out.println("Difference at "+ii+", Before "+bOutBuff[ii]+", after "+tmp[ii]);
               }
               System.out.println( "Crypted: "+ sOutBuff);
          /// ----------end crypt

It gave me:
Difference at 13, Before -113, after 63

Difference at 45, Before -115, after 63

Difference at 81, Before -127, after 63

Since DES is a block cipher the complete blocks 2,6 and 11 will be trashed.

Author

Commented:
Thank you very much!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial