質問編集履歴
1
DESで復号したソースに修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -193,3 +193,115 @@
|
|
193
193
|
|
194
194
|
|
195
195
|
以上、よろしくお願いします。
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
##2020/2/17追記
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
DES形式でソースを修正しました。
|
204
|
+
|
205
|
+
```java
|
206
|
+
|
207
|
+
public static void main(String[] args) {
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
String key = "foobar00";
|
212
|
+
|
213
|
+
String origin = "hoge";
|
214
|
+
|
215
|
+
String enc = "U2FsdGVkX19eAkcLovbInztwfaMnjKwZ";
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
try {
|
222
|
+
|
223
|
+
/*
|
224
|
+
|
225
|
+
* 鍵
|
226
|
+
|
227
|
+
*/
|
228
|
+
|
229
|
+
// 秘密鍵を準備
|
230
|
+
|
231
|
+
byte[] kagi = key.getBytes();
|
232
|
+
|
233
|
+
DESKeySpec dk = new DESKeySpec(kagi);
|
234
|
+
|
235
|
+
Arrays.fill(kagi, (byte)0x00); // セキュリティ情報を上書きして削除
|
236
|
+
|
237
|
+
SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
|
238
|
+
|
239
|
+
SecretKey sk = kf.generateSecret(dk);
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
|
246
|
+
|
247
|
+
cipher.init(Cipher.DECRYPT_MODE, sk);
|
248
|
+
|
249
|
+
Cipher c = Cipher.getInstance("DES");
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
//暗号化されたStringをデコード
|
254
|
+
|
255
|
+
byte[] enc_decode = Base64.getDecoder().decode(enc.getBytes());
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
//復号
|
260
|
+
|
261
|
+
byte[] output2 = c.doFinal(enc_decode);
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// 表示
|
268
|
+
|
269
|
+
System.out.println("The string was ");
|
270
|
+
|
271
|
+
System.out.println(new String(output2));
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
} catch (Exception e) {
|
278
|
+
|
279
|
+
e.printStackTrace();
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
#実行結果
|
290
|
+
|
291
|
+
上記のソースを実行すると、下記エラーがでます。
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
> javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
|
296
|
+
|
297
|
+
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:975)
|
298
|
+
|
299
|
+
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1056)
|
300
|
+
|
301
|
+
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
|
302
|
+
|
303
|
+
at java.base/com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
|
304
|
+
|
305
|
+
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)
|
306
|
+
|
307
|
+
at com.example.demo.test2.main(test2.java:133)
|