Java DPAPI JNI error java.lang.UnsatisfiedLinkError: - java

Exception in thread "main" java.lang.UnsatisfiedLinkError: dpapi.DPAPI.CryptProtectData(Ljava/lang/String;[BZ)[B
at dpapi.DPAPI.CryptProtectData(Native Method)
at dpapi.DataProtector.protect(DataProtector.java:60)
at dpapi.Main.main(Main.java:36)
code
public class Main {
static {
System.load(new File("jdpapi-native-1.0.dll").getAbsolutePath());
}
public static void main(String[] args) {
DataProtector p = new DataProtector();
String x ="abc";
String y = p.protect(x).toString();
}
}
I know the library is loaded but i have no idea why i am still getting the execption.
link:http://jdpapi.sourceforge.net/

check your Java CPU architecture (32bit or 64bit) equals you dll.
32-bit windows process can't load 64-bit dll and via versa...

Related

com.sun.tools.attach.AttachNotSupportedException: no providers installed

I trying to attach agent to self JVM. When I calling VirtualMachine#attach it throws this exception:
java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider: Provider sun.tools.attach.WindowsAttachProvider not a subtype
com.sun.tools.attach.AttachNotSupportedException: no providers installed
There is my code:
static {
System.load("FullPathToAttachDLL/attach.dll");
}
public static void main(final String[] args){
final VirtualMachine virtualMachine = VirtualMachine.attach("" + ImplUtils.getSelfPID());
virtualMachine.loadAgent(file.getAbsolutePath()); // file is my agent.jar
}
P.S I need to fix it at runtime

Error in the webapp while connecting with JVM using jni4net from C#

I'm trying to access a simple java code from inside my C# webapp using jni4net, but it is throwing some errors.
Generated all proxies and dlls to access the java class.
I wrote the code for connecting with JVM inside the 'Program.cs' file.
Later on the custom java function ie. display_msg() is called from the testfunc() which can be called from anywhere inside the bot using Program.testfunc().
I'm attaching the Program.cs file and the exception occurring.
Also I've named my java file as Test.java and it's inside package mypack.
Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using net.sf.jni4net;
using System;
using mypack;
namespace ValidationBot
{
public class Program
{
public static void Main(string[] args)
{
var setup = new BridgeSetup();
setup.Verbose = true;
setup.AddAllJarsClassPath("./");
Bridge.CreateJVM(setup);
Bridge.RegisterAssembly(typeof(Test).Assembly);
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((logging) =>
{
logging.AddDebug();
logging.AddConsole();
}).UseStartup<Startup>();
public static void testfunc()
{
Test test = new Test();
test.display_msg();
Console.WriteLine("\nPress any key to quit.");
Console.ReadKey();
}
}
}
Test.java
package mypack;
import java.io.*;
public class Test
{
public static void main(String args[])
{
int s =10;
System.out.println(s);
}
public void display_msg()
{
System.out.println("Hello, I'm inside a java program");
}
}
Exception
Exception thrown: 'System.MissingMethodException' in jni4net.n-0.8.8.0.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll
Exception thrown: 'System.TypeInitializationException' in jni4net.n-0.8.8.0.dll
An unhandled exception of type 'System.TypeInitializationException' occurred in jni4net.n-0.8.8.0.dll
The type initializer for 'net.sf.jni4net.utils.Registry' threw an exception.
I'm a beginner to C# so please help me out with this.
From the comments we deduced that .NET core 2.1 does not support this method: (and maybe others)
System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)

java: Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V

It's very interesting to use Class.forName() to load a class and get errors... I write one line code and get an error as above.
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream"));
}
}
When I use java UnsatisfiedLinkErrorTest, I get an error:
> java UnsatisfiedLinkErrorTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V
at java.net.SocketOutputStream.init(Native Method)
at java.net.SocketOutputStream.<clinit>(SocketOutputStream.java:44)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at UnsatisfiedLinkErrorTest.main(UnsatisfiedLinkErrorTest.java:3)
But when I run it in Idea, I get:
> java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49247:/Applications/IntelliJ IDEA.app/Contents/bin" UnsatisfiedLinkErrorTest
class java.net.SocketOutputStream
I don't know why in Idea I can get the true answer, (maybe jni?) I hope someone can tell me the reason. Thanks.
This question has been solved. I then cancelled the initialization of the class, then the error goes. The error is about the initialization, but not about finding the native library. So:
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream", false, null)); // will be okay!
}
}

when hs_err_pidxxxx.log will generate?

public class Crash {
public static void main(String[] args) {
System.exit(0);
}
}
How to crash the JVM so it will generate the hs_err_pidxxxx.log
The log file is located in the "current directory" of the process by default. This can changed - see It is posible to write hs_err_pid*.log in a specific directory (different than the class directory) when java virtual machine crash?.

include PKCS11 library in Java

I have PCKS11 dll and so files for my token.and i want to use [jacknji11][1] library.
my problem is how can i import the dll OR so files to my project.
i use System.loadLibrary("parskey11"); to import my library but i get below Error :
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.pkcs11.jacknji11.jna.JNANative.C_InitToken(Lcom/sun/jna/NativeLong;[BLcom/sun/jna/NativeLong;[B)I
at org.pkcs11.jacknji11.jna.JNANative.C_InitToken(Native Method)
at org.pkcs11.jacknji11.jna.JNA.C_InitToken(JNA.java:119)
at org.pkcs11.jacknji11.C.InitToken(C.java:235)
at org.pkcs11.jacknji11.CE.InitToken(CE.java:272)
at com.mmdt.Main.main(Main.java:13)
i just copy my libparskey11.so file to jni folder in project and use below code :
private static final long INITSLOT = 0;
public static void main(String[] args) {
CE.InitToken(INITSLOT, "pincode".getBytes(), "TEST".getBytes());
long session = CE.OpenSession(INITSLOT, CKS.RW_PUBLIC_SESSION, null,
null);
CE.Login(session, CKU.SO, "pincode".getBytes());
CE.InitPIN(session, "pincode2".getBytes());
CE.Logout(session);
CE.Login(session, CKU.USER, "pincode".getBytes());
byte[] somenewpin = "somenewpin".getBytes();
CE.SetPIN(session, "pincode2".getBytes(), somenewpin);
CE.SetPIN(session, somenewpin, "pincode2".getBytes());
}

Categories

Resources