質問編集履歴

2

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

2020/07/16 01:28

投稿

fuoh
fuoh

スコア19

test CHANGED
File without changes
test CHANGED
@@ -167,3 +167,23 @@
167
167
 
168
168
 
169
169
  ```
170
+
171
+
172
+
173
+
174
+
175
+ 共有ライブラリの作成手順を以下にまとめました。
176
+
177
+ ※既存の処理を部分的に抜き出して共有ライブラリとして作成しています。
178
+
179
+
180
+
181
+ 0. Eclipseの新規プロジェクト(C/C++プロジェクト、C++管理ビルドテンプレート、空のプロジェクトを指定)を作成しました。
182
+
183
+ 0. 作成したプロジェクトにsrcディレクトリとincludeディレクトリを追加しました。
184
+
185
+ 0. 既存のプログラムをプロジェクトに追加(空ファイルを作成し、既存プログラムの内容をコピーする)しました。
186
+
187
+ 0. プロジェクトのプロパティで既存と同じ設定を指定しました(設定内容は、前出のコンパイルオプション、リンクオプションです)。
188
+
189
+ 0. ビルドしてライブラリを生成しました。

1

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

2020/07/16 01:28

投稿

fuoh
fuoh

スコア19

test CHANGED
@@ -1 +1 @@
1
- C++で作成した共有ライブラリ(so)をJava(JNA)で参照しようとしたが、「未定義のシンボル」で失敗します。
1
+ C++で作成した共有ライブラリ(so)をJava(JNA)で参照しようとしたが、「undefined symbol (未定義のシンボル)」で失敗します。
test CHANGED
@@ -1,6 +1,6 @@
1
1
  C++で作成した共有ライブラリ(so)をJNAを利用してJavaプログラムで参照しようとしましたが、
2
2
 
3
- 「未定義のシンボル」で失敗します。
3
+ undefined symbol (未定義のシンボル)」で失敗します。
4
4
 
5
5
 
6
6
 
@@ -47,3 +47,123 @@
47
47
   gcc-c++(4.8.5)
48
48
 
49
49
   JDK(11)
50
+
51
+
52
+
53
+
54
+
55
+ 以下、エラーメッセージです。
56
+
57
+ ```error message
58
+
59
+ Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'CAESSecurity': /home/css/eclipse-workspace/CryptoWithA4kCommonLibrarySample/bin/libAESSecurityLibrary.so: undefined symbol: CAESSecurity
60
+
61
+ at com.sun.jna.Function.<init>(Function.java:252)
62
+
63
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:600)
64
+
65
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:576)
66
+
67
+ at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:562)
68
+
69
+ at com.sun.jna.Library$Handler.invoke(Library.java:243)
70
+
71
+ at com.sun.proxy.$Proxy0.CAESSecurity(Unknown Source)
72
+
73
+ at CryptoSample.encryptToken(CryptoSample.java:25)
74
+
75
+ at mainSample.main(mainSample.java:11)
76
+
77
+
78
+
79
+ ```
80
+
81
+
82
+
83
+ 以下、プログラムです。
84
+
85
+ ```C++
86
+
87
+ CAESSecurity::CAESSecurity(bytes& key, bytes& IV, int keytype)
88
+
89
+ : m_key(key), m_IV(IV), m_keytype(keytype)
90
+
91
+ {
92
+
93
+ /* Load up the software EVP_CIPHER definitions */
94
+
95
+ OpenSSL_add_all_ciphers();
96
+
97
+ #ifndef OPENSSL_NO_ENGINE
98
+
99
+ /* Load all compiled-in ENGINEs */
100
+
101
+ ENGINE_load_builtin_engines();
102
+
103
+ /* Register all available ENGINE implementations of ciphers. */
104
+
105
+ ENGINE_register_all_ciphers();
106
+
107
+ #endif
108
+
109
+
110
+
111
+ }
112
+
113
+
114
+
115
+ ```
116
+
117
+ ```Java
118
+
119
+ // 暗号化&復号化で使用する鍵
120
+
121
+ 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 };
122
+
123
+ // 初期ベクトル
124
+
125
+ 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 };
126
+
127
+
128
+
129
+ public interface A4kCommonLibrary extends Library {
130
+
131
+ A4kCommonLibrary INSTANCE = (A4kCommonLibrary)Native.loadLibrary("AESSecurityLibrary", A4kCommonLibrary.class);
132
+
133
+
134
+
135
+ void CAESSecurity(byte[] key, byte[] iv, int keyType);
136
+
137
+ boolean Encode(byte[] in_bytes, byte[] out_bytes);
138
+
139
+ }
140
+
141
+
142
+
143
+ /**
144
+
145
+ * 暗号化処理
146
+
147
+ */
148
+
149
+ public String encryptToken(String token) throws Exception {
150
+
151
+ byte[] data = new byte[5000];
152
+
153
+ A4kCommonLibrary Cipher = A4kCommonLibrary.INSTANCE;
154
+
155
+ Cipher.CAESSecurity(ENCRYPT_KEY, INIT_VECTOR, 128);
156
+
157
+ Cipher.Encode(token.getBytes(), data);
158
+
159
+
160
+
161
+ // 暗号化
162
+
163
+ return new String(data);
164
+
165
+ }
166
+
167
+
168
+
169
+ ```