質問編集履歴

10

コード修正

2025/04/18 12:21

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -77,9 +77,9 @@
77
77
  std::vector<unsigned char> nonce (16);
78
78
  std::vector<unsignedchar› tag (16);
79
79
 
80
- file.read (reinterpret_cast<char*>(salt.data()), salt-size());
80
+ file.read (reinterpret_cast<char*>(salt.data()), salt.size());
81
- file.read (reinterpret_cast<char*>(nonce.data()), nonce-size ());
81
+ file.read (reinterpret_cast<char*>(nonce.data()), nonce.size ());
82
- file.read (reinterpret_cast<char*>(tag-data()), tag-size());
82
+ file.read (reinterpret_cast<char*>(tag.data()), tag.size());
83
83
 
84
84
  //暗号化されたモデルデータを取得
85
85
  std::vector<unsigned char› ciphertext ((std:: istreambuf_iterator<char›(file)), std::istreambuf_iterator<char›());
@@ -87,7 +87,8 @@
87
87
 
88
88
  //PBKDF2を使用してキーを取得
89
89
  std::vector<unsigned char> key (32) ;
90
- if (IPKCS5 PBKDF2_ HMAC(pass.c_str(), pass.size(), salt.data(), salt.size(), 1000000, EVP_sha256(), key-size(), key .data())){
90
+ if (IPKCS5 PBKDF2_ HMAC(pass.c_str(), pass.size(), salt.data(), salt.size(), 1000000,
91
+ EVP_sha256(), key-size(), key .data())){
91
92
  throw std::runtime_error ("Failed to derive key using PBKDF2.");
92
93
  }
93
94
 
@@ -98,8 +99,9 @@
98
99
  }
99
100
 
100
101
  if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), (), nullptr, nullptr, nullptr) != 1 ||
101
- EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, nonce.size(), nullptr) != 1 ||
102
+ EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, nonce.size(), nullptr) != 1 ||
102
- EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1{
103
+ EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1){
104
+
103
105
  EVP_CIPHER _CTX_free (ctx) ;
104
106
  throw std::runtime error ("Failed to initialize decrypt ion.");
105
107
  }
@@ -115,9 +117,10 @@
115
117
 
116
118
  int decryptedLen = len;
117
119
  if (EVP_CIPHER_CTX_ctr (ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), tag.data()) != 1 ||
118
- EVP_DecryptFinal_ex(ctx, decrypted.data() + len, &len) != 1) {
120
+ EVP_DecryptFinal_ex(ctx, decrypted.data() + len, &len) != 1) {
121
+
119
- EVP_CIPHER_CTX_free (ctx);
122
+ EVP_CIPHER_CTX_free (ctx);
120
- throw std::runtime_error ("Decryption failed.");
123
+ throw std::runtime_error ("Decryption failed.");
121
124
  }
122
125
 
123
126
  decryptedLen += len;

9

コード修正

2025/04/18 12:06

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -98,7 +98,7 @@
98
98
  }
99
99
 
100
100
  if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), (), nullptr, nullptr, nullptr) != 1 ||
101
- EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET _IVLEN, nonce.size(), nullptr) != 1 ||
101
+ EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET_IVLEN, nonce.size(), nullptr) != 1 ||
102
102
  EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1{
103
103
  EVP_CIPHER _CTX_free (ctx) ;
104
104
  throw std::runtime error ("Failed to initialize decrypt ion.");

8

コード修正

2025/04/18 12:05

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -98,7 +98,7 @@
98
98
  }
99
99
 
100
100
  if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), (), nullptr, nullptr, nullptr) != 1 ||
101
- EVP_CIPHER _CTX_ctrl (ctx, EVP_CTRL_GCM_SET _IVLEN, nonce.size(), nullptr) != 1 ||
101
+ EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_GCM_SET _IVLEN, nonce.size(), nullptr) != 1 ||
102
102
  EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1{
103
103
  EVP_CIPHER _CTX_free (ctx) ;
104
104
  throw std::runtime error ("Failed to initialize decrypt ion.");

7

コード修正

2025/04/18 11:56

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -35,7 +35,7 @@
35
35
  from Crypto.Protocol.KDF import PBKDF2
36
36
  from Crypto.Random import get_random_bytes
37
37
 
38
- def export_onnx_with_header(self, onnx_model,dummy_input, output_model_path, export_params, opset_version, do_constant_folding, input_names, output_names, dynamic_axes, verbose) :
38
+ def export_onnx(self, onnx_model,dummy_input, output_model_path, export_params, opset_version, do_constant_folding, input_names, output_names, dynamic_axes, verbose) :
39
39
 
40
40
  # メモリにONNXモデルをエクスポート
41
41
  buffer = io.BytesIO)

6

コード修正

2025/04/18 11:19

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
  別の方法でも気密性・完全性が担保されるような方法であれば問題ないのでご享受ください。
13
13
 
14
14
  ### 発生している問題・分からないこと
15
- C++の復号の関数
15
+ C++の復号の関数(53行目)
16
16
  EVP_CIPHER_CTX_ctr (ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), tag.data()) != 1
17
17
  がfalseのため、raiseでエラー終了します。
18
18
 

5

コード修正

2025/04/18 11:18

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -81,7 +81,7 @@
81
81
  file.read (reinterpret_cast<char*>(nonce.data()), nonce-size ());
82
82
  file.read (reinterpret_cast<char*>(tag-data()), tag-size());
83
83
 
84
- //暗号化されたヘッダー付きモデルデータを取得
84
+ //暗号化されたモデルデータを取得
85
85
  std::vector<unsigned char› ciphertext ((std:: istreambuf_iterator<char›(file)), std::istreambuf_iterator<char›());
86
86
  file.close();
87
87
 
@@ -104,7 +104,7 @@
104
104
  throw std::runtime error ("Failed to initialize decrypt ion.");
105
105
  }
106
106
 
107
- // ヘッダー付きモデルデータを復号化
107
+ // モデルデータを復号化
108
108
  std::vector<unsigned char› decrypted (ciphertext.size));
109
109
  int len = 0;
110
110
  if (EVP DecryptUpdate(ctx, decrypted.data(), &len, ciphertext.data(), ciphertext.size ()) != 1)

4

コード修正

2025/04/18 11:17

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
  pass = "abcdefg"
55
55
  salt = get_random_bytes(16)
56
56
  key = PBKDF2(pass, salt, dkLen=32, count=1000000)
57
- cipher = AES. new (key, AES.MODE_GCM)
57
+ cipher = AES.new (key, AES.MODE_GCM)
58
58
  ciphertext, tag = cipher.encrypt_and_digest(onnx_model_binary)
59
59
  # 暗号されたデータをファイルに保存
60
60
  with open (output_model_path, 'wb') as f:

3

コード修正

2025/04/18 11:16

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
  key = PBKDF2(pass, salt, dkLen=32, count=1000000)
57
57
  cipher = AES. new (key, AES.MODE_GCM)
58
58
  ciphertext, tag = cipher.encrypt_and_digest(onnx_model_binary)
59
- 暗号されたデータをファイルに保存
59
+ # 暗号されたデータをファイルに保存
60
60
  with open (output_model_path, 'wb') as f:
61
61
  f.write(salt + cipher.nonce + tag + ciphertext)
62
62
  ```

2

コード修正

2025/04/18 11:16

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,15 @@
40
40
  # メモリにONNXモデルをエクスポート
41
41
  buffer = io.BytesIO)
42
42
  torch.onnx.export (onnx_model,
43
- dummy_ input, buffer, export_params=export_params, opset_version=opset_version,do_constant_folding=do_constant_folding, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes, verbose=verbose)
43
+      dummy_ input,
44
+      buffer,
45
+      export_params=export_params,
46
+      opset_version=opset_version,
47
+      do_constant_folding=do_constant_folding,
48
+      input_names=input_names,
49
+      output_names=output_names,
50
+      dynamic_axes=dynamic_axes,
51
+      verbose=verbose)
44
52
  onnx_model_binary = buffer getvalue()
45
53
 
46
54
  pass = "abcdefg"
@@ -54,66 +62,71 @@
54
62
  ```
55
63
 
56
64
  ```C++
65
+ #include <openssl/evp.h>
66
+ #include <openssl/aes.h>
67
+ #include <openssl/err.h>
68
+
57
69
  std::vector<char› decryptModel(char* modelpath, std::string pass) {
58
- std:: ifstream file(modelpath, std:: ios: :binary):
70
+ std:: ifstream file(modelpath, std:: ios: :binary):
59
- if (!file) {
71
+ if (!file) {
60
- throw std::runtime_error ("Failed to open encrypted model file.");
72
+ throw std::runtime_error ("Failed to open encrypted model file.");
73
+ }
74
+
75
+ //ファイルから復号化に必要なデータを取得
76
+ std::vector <unsigned char > salt (16);
77
+ std::vector<unsigned char> nonce (16);
78
+ std::vector<unsignedchar› tag (16);
79
+
80
+ file.read (reinterpret_cast<char*>(salt.data()), salt-size());
81
+ file.read (reinterpret_cast<char*>(nonce.data()), nonce-size ());
82
+ file.read (reinterpret_cast<char*>(tag-data()), tag-size());
83
+
84
+ //暗号化されたヘッダー付きモデルデータを取得
85
+ std::vector<unsigned char› ciphertext ((std:: istreambuf_iterator<char›(file)), std::istreambuf_iterator<char›());
86
+ file.close();
87
+
88
+ //PBKDF2を使用してキーを取得
89
+ std::vector<unsigned char> key (32) ;
90
+ if (IPKCS5 PBKDF2_ HMAC(pass.c_str(), pass.size(), salt.data(), salt.size(), 1000000, EVP_sha256(), key-size(), key .data())){
91
+ throw std::runtime_error ("Failed to derive key using PBKDF2.");
92
+ }
93
+
94
+ // 復号化コンテキスト初期化
95
+ EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
96
+ if (!ctx) {
97
+ throw std::runtime error ("Failed to create EVP_CIPHER_СTX.");
98
+ }
99
+
100
+ if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), (), nullptr, nullptr, nullptr) != 1 ||
101
+ EVP_CIPHER _CTX_ctrl (ctx, EVP_CTRL_GCM_SET _IVLEN, nonce.size(), nullptr) != 1 ||
102
+ EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1{
103
+ EVP_CIPHER _CTX_free (ctx) ;
104
+ throw std::runtime error ("Failed to initialize decrypt ion.");
61
105
  }
62
106
 
63
- //ファイから復号化に必要なデータを取得
107
+ // ヘッダー付きモデルデータを復号化
64
- std::vector <unsigned char > salt (16);
108
+ std::vector<unsigned char decrypted (ciphertext.size));
109
+ int len = 0;
110
+ if (EVP DecryptUpdate(ctx, decrypted.data(), &len, ciphertext.data(), ciphertext.size ()) != 1)
111
+ {
112
+ EVP_CIPHER_CTX_free (ctx);
65
- std::vector<unsigned char> nonce (16);
113
+ throw std::runtime error ("Decrypt ion failed.");
66
- std::vector<unsignedchar› tag (16);
114
+ }
67
115
 
116
+ int decryptedLen = len;
68
- file.read (reinterpret_cast<char*>(salt.data()), salt-size());
117
+ if (EVP_CIPHER_CTX_ctr (ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), tag.data()) != 1 ||
69
- file.read (reinterpret_cast<char*>(nonce.data()), nonce-size ());
118
+ EVP_DecryptFinal_ex(ctx, decrypted.data() + len, &len) != 1) {
119
+ EVP_CIPHER_CTX_free (ctx);
70
- file.read (reinterpret_cast<char*>(tag-data()), tag-size());
120
+ throw std::runtime_error ("Decryption failed.");
121
+ }
71
122
 
123
+ decryptedLen += len;
72
- //暗号化されたヘッダー付きモデルデータを取得
124
+ decrypted.resize (decryptedLen) :
73
- std::vector<unsigned char› ciphertext ((std:: istreambuf_iterator<char›(file)), std::istreambuf_iterator<char›());
74
- file.close();
125
+ EVP_CIPHER_CTX_free (ctx);
75
126
 
76
- //PBKDF2を使用してキーを取得
77
- std::vector<unsigned char> key (32) ;
78
- if (IPKCS5 PBKDF2_ HMAC(pass.c_str(), pass.size(), salt.data(), salt.size(), 1000000, EVP_sha256(), key-size(), key .data())){
79
- throw std::runtime_error ("Failed to derive key using PBKDF2.");
80
- }
81
-
82
- // 復号化コンテキスト初期化
83
- EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
84
- if (!ctx) {
85
- throw std::runtime error ("Failed to create EVP_CIPHER_СTX.");
86
- }
87
-
88
- if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), (), nullptr, nullptr, nullptr) != 1 ||
89
- EVP_CIPHER _CTX_ctrl (ctx, EVP_CTRL_GCM_SET _IVLEN, nonce.size(), nullptr) != 1 ||
90
- EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), nonce.data()) != 1{
91
- EVP_CIPHER _CTX_free (ctx) ;
92
- throw std::runtime error ("Failed to initialize decrypt ion.");
93
- }
94
-
95
- // ヘッダー付きモデルデータを復号化
96
- std::vector<unsigned char› decrypted (ciphertext.size));
97
- int len = 0;
98
- if (EVP DecryptUpdate(ctx, decrypted.data(), &len, ciphertext.data(), ciphertext.size ()) != 1) {
99
- EVP_CIPHER_CTX_free (ctx);
100
- throw std::runtime error ("Decrypt ion failed.");
101
- }
102
-
103
- int decryptedLen = len;
104
- if (EVP_CIPHER_CTX_ctr (ctx, EVP_CTRL_GCM_SET_TAG, tag.size(), tag.data()) != 1 ||
105
- EVP_DecryptFinal_ex(ctx, decrypted.data() + len, &len) != 1) {
106
- EVP_CIPHER_CTX_free (ctx);
107
- throw std::runtime_error ("Decryption failed.");
108
- }
109
-
110
- decryptedLen += len;
111
- decrypted.resize (decryptedLen) :
112
- EVP_CIPHER_CTX_free (ctx);
113
-
114
- //復号化されたデータからモデル情報を取得
127
+ //復号化されたデータからモデル情報を取得
115
- std::vector<char› mode_Data(decrypted.begin(), decrypted.end ());
128
+ std::vector<char› mode_Data(decrypted.begin(), decrypted.end ());
116
- return modelData;
129
+ return modelData;
117
130
  }
118
131
  ```
119
132
 

1

コード修正

2025/04/18 11:10

投稿

kkjiji
kkjiji

スコア45

test CHANGED
File without changes
test CHANGED
@@ -29,26 +29,28 @@
29
29
  ### 該当のソースコード
30
30
 
31
31
  ```Python
32
+ import io
33
+ import torch
32
34
  from Crypto.Cipher import AES
33
35
  from Crypto.Protocol.KDF import PBKDF2
34
36
  from Crypto.Random import get_random_bytes
35
37
 
36
38
  def export_onnx_with_header(self, onnx_model,dummy_input, output_model_path, export_params, opset_version, do_constant_folding, input_names, output_names, dynamic_axes, verbose) :
37
39
 
38
- # メモリにONNXモデルをエクスポート
39
- buffer = io. BytesIO)
40
- torch. onx. export (onnx_model,
41
- dummy_ input, buffer, export_params=export_params, opset_version=opset_version, do_constant_folding=do_constant_folding, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes, verbose=verbose)
42
- onnx_model_binary = buffer getvalue()
40
+ # メモリにONNXモデルをエクスポート
41
+ buffer = io.BytesIO)
42
+ torch.onnx.export (onnx_model,
43
+ dummy_ input, buffer, export_params=export_params, opset_version=opset_version,do_constant_folding=do_constant_folding, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes, verbose=verbose)
44
+ onnx_model_binary = buffer getvalue()
43
45
 
44
- pass = "abcdefg"
46
+ pass = "abcdefg"
45
- salt = get_random_bytes(16)
47
+ salt = get_random_bytes(16)
46
- key = PBKDF2(pass, salt, dkLen=32, count=1000000)
48
+ key = PBKDF2(pass, salt, dkLen=32, count=1000000)
47
- cipher = AES. new (key, AES.MODE_GCM)
49
+ cipher = AES. new (key, AES.MODE_GCM)
48
- ciphertext, tag = cipher.encrypt_and_digest(onnx_model_binary)
50
+ ciphertext, tag = cipher.encrypt_and_digest(onnx_model_binary)
49
- #暗号されたデータをファイルに保存
51
+ #暗号されたデータをファイルに保存
50
- with open (output_model_path, 'wb') as f:
52
+ with open (output_model_path, 'wb') as f:
51
- f.write(salt + cipher.nonce + tag + ciphertext)
53
+ f.write(salt + cipher.nonce + tag + ciphertext)
52
54
  ```
53
55
 
54
56
  ```C++