0

I added crytlib.jar to my Crytest project but when I run it.

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: cryptlib.crypt.CreateContext(II)I at cryptlib.crypt.CreateContext(Native Method)

picture1
How can I fix this error?


Update 1:

This is my code:

try{     
    int context = cryptlib.crypt.CreateContext(cryptlib.crypt.UNUSED, cryptlib.crypt.ALGO_AES);
            cryptlib.crypt.Init();
            cryptlib.crypt.SetAttributeString(context, crypt.CTXINFO_KEYING_SALT, "abcxyz");
            cryptlib.crypt.SetAttributeString(context, crypt.CTXINFO_KEYING_VALUE, key, 0,key.length);
            cryptlib.crypt.Encrypt(context, ciphertext, 0, ciphertext.length);
            cryptlib.crypt.DestroyContext(context);  
            cryptlib.crypt.End();
        }catch(CryptException e){
            e.printStackTrace();
        }
Mr2uang
  • 55
  • 2
  • 9
  • 1
    You added it to the folder, did you also edited the build path? – Nadir Mar 18 '16 at 12:06
  • @Nadir I don't understand. I always do the same with other project. Can give me more detail? – Mr2uang Mar 18 '16 at 12:07
  • @Nadir I'm using Netbeans and I added library like [this](http://stackoverflow.com/questions/7598623/how-to-setup-classpath-in-netbeans) – Mr2uang Mar 18 '16 at 12:43
  • Well, the error itself is caused when the jvm cannot find the appropiate native code to execute your java code. Does that library comes with native libraries (.dll, .so, etc.)? – Nadir Mar 18 '16 at 12:47
  • It's from [this page](https://www.cs.auckland.ac.nz/~pgut001/cryptlib/download.html). I already installed it to Ubuntu and the file jar in the `/binding` – Mr2uang Mar 18 '16 at 12:50

1 Answers1

1

As @Nadir alraedy guessed the cryptlib.jar itself does not contain any native code.

You need to download http://www.cypherpunks.to/~peter/cl343_beta.zip and unzip it. The Java binding is in bindings/cryptlib.jar.

To build the native code I believe you can follow the build instructions for Unix in the manual on page 35.

To build the shared library, use make shared. Once cryptlib has been built, use make testlib to build the cryptlib self-test program testlib, or make stestlib to build the shared-library self-test program stestlib. This will run fairly extensive self-tests of cryptlib that you can run after you’ve built it to make sure that everything is working OK.

edit Following a step-by-step instruction how to build a working library.

# download the archive http://www.cypherpunks.to/~peter/cl343_beta.zip
# extract the archive to any directory
# -a switch to ensure text files not extracted with DOS lineend
unzip -a cl343_beta.zip -d ${CL_HOME}/

# fix executable bits
chmod +x tools/mkhdr.sh

# re-generate the bindings
tools/mkhdr.sh

# modify some source files
# change in ${CL_HOME}/misc/config.h
   from  /* #define USE_JAVA */
   to    #define USE_JAVA
# change in ${CL_HOME}/bindings/java_jni.c
   from  #include <jni.h>
   to    #include "jni.h"

# copy JDK header files
cd ${CL_HOME}/bindings/
cp ${JAVA_HOME}/include/jni.h .
cp ${JAVA_HOME}/inlcude/linux/jni_md.h .

# build the library
cd ${CL_HOME}/
make
make shared

As test code I took the example from the manual.

import cryptlib.*;
class Cryptlib {
    public static void main( String[] args) {
        System.loadLibrary( "cl" );
        try {
            crypt.Init();
            //Calls to cryptlib routines
            crypt.End();
        } catch(CryptException e) {
            e.printStackTrace();
        }
    }
};

copy library files into the directory of Cryptlib.java.

cd ${dir_of_Cryptlib.java}
cp ${CL_HOME}/bindings/cryptlib.jar .
cp ${CL_HOME}/libcl.so.3.4.3 .
ln -s libcl.so.3.4.3 libcl.so

compile the code

javac -cp cryptlib.jar Cryptlib.java

run the code

java -cp cryptlib.jar:. -Djava.library.path=`pwd` Cryptlib

There will be no output and no exception if the build was successful.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • The problem is: I already done that before. But when I run `make testlib`. It returned `testlib.c:(.text.startup+0x11f): undefined reference to `fuzz'` – Mr2uang Mar 18 '16 at 13:11
  • And I did copy the `cl343` to `/usr/lib` . But still error :( – Mr2uang Mar 18 '16 at 13:24
  • I already did eveything in the manual. But still no luck and already run a command from `Terminal` :`java -Djava.library.path=/usr/lib/cl/ -jar ~/NetBeansProjects/Cryp/dist/Cryp.jar` – Mr2uang Mar 18 '16 at 14:51
  • @Mr2uang Maybe some additional steps needs to be executed. Will have a look later and try to build the library by myself. – SubOptimal Mar 18 '16 at 15:01
  • @Mr2uang Have a look at my updated answer. It has a step by step guide to build the library. – SubOptimal Mar 20 '16 at 20:24
  • Do I need `dos2unix` because `unzip -a` already helped me. And `cp ${JAVA_HOME}/include/jni.h .` need destination bro! – Mr2uang Mar 20 '16 at 20:50
  • @Mr2uang **1)** `dos2unix` is not needed as `unzip -a` converts the lineends as well, have updated my answer, **2)** in `cp ${JAVA_HOME}/include/jni.h .` the `.` means copy into the current directory (which is `${CL_HOME}/bindings/` after you did `cd ${CL_HOME}/bindings/` – SubOptimal Mar 21 '16 at 05:29
  • Woow amazing but `make testlib` can't run bro!. Anyway why does `crytlib` use so hard? – Mr2uang Mar 21 '16 at 14:39
  • @Mr2uang **1)** Building the testlib wasn't the original question. Isn't it? **2)** Ask the author. ;-) – SubOptimal Mar 22 '16 at 06:54
  • `-Djava.library.path=`pwd`` Cound you tel me What is `-Djava.library.path=`pwd`` and Should I call `System.loadLibrary( "cl" );` or not? – Mr2uang Mar 22 '16 at 13:01
  • @Mr2uang **1)** The path in `-Djava.library.path` must point to the directory where the (file or softlink) `libcl.so` is located. As in the test all files where in the current directory the `pwd` returned the current directory. **2)** Yes you need to load the library with `System.loadLibrary("cl")` if you want to use it. – SubOptimal Mar 22 '16 at 18:36