質問するログイン新規登録

質問編集履歴

2

ライブラリ作成手順を追記しました

2020/07/16 01:28

投稿

fuoh
fuoh

スコア19

title CHANGED
File without changes
body CHANGED
@@ -82,4 +82,14 @@
82
82
  return new String(data);
83
83
  }
84
84
 
85
- ```
85
+ ```
86
+
87
+
88
+ 共有ライブラリの作成手順を以下にまとめました。
89
+ ※既存の処理を部分的に抜き出して共有ライブラリとして作成しています。
90
+
91
+ 0. Eclipseの新規プロジェクト(C/C++プロジェクト、C++管理ビルドテンプレート、空のプロジェクトを指定)を作成しました。
92
+ 0. 作成したプロジェクトにsrcディレクトリとincludeディレクトリを追加しました。
93
+ 0. 既存のプログラムをプロジェクトに追加(空ファイルを作成し、既存プログラムの内容をコピーする)しました。
94
+ 0. プロジェクトのプロパティで既存と同じ設定を指定しました(設定内容は、前出のコンパイルオプション、リンクオプションです)。
95
+ 0. ビルドしてライブラリを生成しました。

1

情報の追記(エラーメッセージとプログラム)

2020/07/16 01:28

投稿

fuoh
fuoh

スコア19

title CHANGED
@@ -1,1 +1,1 @@
1
- C++で作成した共有ライブラリ(so)をJava(JNA)で参照しようとしたが、「未定義のシンボル」で失敗します。
1
+ C++で作成した共有ライブラリ(so)をJava(JNA)で参照しようとしたが、「undefined symbol (未定義のシンボル)」で失敗します。
body CHANGED
@@ -1,5 +1,5 @@
1
1
  C++で作成した共有ライブラリ(so)をJNAを利用してJavaプログラムで参照しようとしましたが、
2
- 「未定義のシンボル」で失敗します。
2
+ undefined symbol (未定義のシンボル)」で失敗します。
3
3
 
4
4
  お教えていただきたいのは、共有ライブラリを作成する時に必ず指定しなければならない
5
5
  コンパイルオプションかリンクオプションが、あるのでしょうか?
@@ -22,4 +22,64 @@
22
22
   Eclipse 2020-03(4.15.0)
23
23
   CDT 9.11(2020-03)
24
24
   gcc-c++(4.8.5)
25
-  JDK(11)
25
+  JDK(11)
26
+
27
+
28
+ 以下、エラーメッセージです。
29
+ ```error message
30
+ Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'CAESSecurity': /home/css/eclipse-workspace/CryptoWithA4kCommonLibrarySample/bin/libAESSecurityLibrary.so: undefined symbol: CAESSecurity
31
+ at com.sun.jna.Function.<init>(Function.java:252)
32
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:600)
33
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:576)
34
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:562)
35
+ at com.sun.jna.Library$Handler.invoke(Library.java:243)
36
+ at com.sun.proxy.$Proxy0.CAESSecurity(Unknown Source)
37
+ at CryptoSample.encryptToken(CryptoSample.java:25)
38
+ at mainSample.main(mainSample.java:11)
39
+
40
+ ```
41
+
42
+ 以下、プログラムです。
43
+ ```C++
44
+ CAESSecurity::CAESSecurity(bytes& key, bytes& IV, int keytype)
45
+ : m_key(key), m_IV(IV), m_keytype(keytype)
46
+ {
47
+ /* Load up the software EVP_CIPHER definitions */
48
+ OpenSSL_add_all_ciphers();
49
+ #ifndef OPENSSL_NO_ENGINE
50
+ /* Load all compiled-in ENGINEs */
51
+ ENGINE_load_builtin_engines();
52
+ /* Register all available ENGINE implementations of ciphers. */
53
+ ENGINE_register_all_ciphers();
54
+ #endif
55
+
56
+ }
57
+
58
+ ```
59
+ ```Java
60
+ // 暗号化&復号化で使用する鍵
61
+ private static final byte[] ENCRYPT_KEY = new byte[] { (byte)0x9C, (byte)0x5D, (byte)0x5A, (byte)0x42, (byte)0x8A, (byte)0x20, (byte)0x3D, (byte)0x7B, (byte)0x38, (byte)0x5D, (byte)0xAD, (byte)0x63, (byte)0x29, (byte)0xE6, (byte)0xD2, (byte)0x16 };
62
+ // 初期ベクトル
63
+ private static final byte[] INIT_VECTOR = new byte[] { (byte)0xBB, (byte)0xC0, (byte)0xCE, (byte)0xC6, (byte)0xAE, (byte)0xB8, (byte)0x47, (byte)0x9D, (byte)0xE2, (byte)0xA3, (byte)0x85, (byte)0x93, (byte)0x13, (byte)0xA7, (byte)0x88, (byte)0x65 };
64
+
65
+ public interface A4kCommonLibrary extends Library {
66
+ A4kCommonLibrary INSTANCE = (A4kCommonLibrary)Native.loadLibrary("AESSecurityLibrary", A4kCommonLibrary.class);
67
+
68
+ void CAESSecurity(byte[] key, byte[] iv, int keyType);
69
+ boolean Encode(byte[] in_bytes, byte[] out_bytes);
70
+ }
71
+
72
+ /**
73
+ * 暗号化処理
74
+ */
75
+ public String encryptToken(String token) throws Exception {
76
+ byte[] data = new byte[5000];
77
+ A4kCommonLibrary Cipher = A4kCommonLibrary.INSTANCE;
78
+ Cipher.CAESSecurity(ENCRYPT_KEY, INIT_VECTOR, 128);
79
+ Cipher.Encode(token.getBytes(), data);
80
+
81
+ // 暗号化
82
+ return new String(data);
83
+ }
84
+
85
+ ```