@@ -153,8 +153,14 @@ object KotlinPGP {
153
153
ByteArrayInputStream (encrypted.toByteArray()).use {
154
154
PGPUtil .getDecoderStream(it)
155
155
}.use { inputStream ->
156
-
157
- val encryptedDataKeyId = arrayListOf<Long >()
156
+ val allKeys = privateKeyData.flatMap {
157
+ OpenPGPUtils .getAllEncryptionPrivateKeys(
158
+ getSecretKeyRingFromString(
159
+ it.key,
160
+ it.password
161
+ ), it.password.toCharArray()
162
+ )
163
+ }
158
164
PGPObjectFactory (inputStream, jcaKeyFingerprintCalculator)
159
165
.let {
160
166
when (val obj = it.nextObject()) {
@@ -165,45 +171,28 @@ object KotlinPGP {
165
171
it.encryptedDataObjects.iterator()
166
172
}.forEach { data ->
167
173
if (data is PGPPublicKeyEncryptedData ) {
168
- encryptedDataKeyId.add(data.keyID)
169
- privateKeyData.forEach { privateKeyData ->
170
- val privKey = OpenPGPUtils .getMasterPrivateKey(getSecretKeyRingFromString(privateKeyData.key, privateKeyData.password), privateKeyData.password.toCharArray())
171
- if (data.keyID == 0L ) {
174
+ if (data.keyID == 0L ) {
175
+ for (key in allKeys) {
172
176
kotlin.runCatching {
173
- data.getDataStream(BcPublicKeyDataDecryptorFactory (privKey )).use {
177
+ data.getDataStream(BcPublicKeyDataDecryptorFactory (key )).use {
174
178
PGPObjectFactory (it, jcaKeyFingerprintCalculator)
175
179
}.let {
176
- return getDecryptResultFromFactory(it, encryptedDataKeyId )
180
+ return getDecryptResultFromFactory(it, arrayListOf () )
177
181
}
178
182
}.onFailure {
179
183
180
184
}
181
- } else if (data.keyID == privKey?.keyID) {
182
- data.getDataStream(BcPublicKeyDataDecryptorFactory (privKey)).use {
183
- PGPObjectFactory (it, jcaKeyFingerprintCalculator)
184
- }.let {
185
- return getDecryptResultFromFactory(it, encryptedDataKeyId)
186
- }
185
+ }
186
+ } else if (allKeys.any { key -> key.keyID == data.keyID }) {
187
+ val key = allKeys.first { key -> key.keyID == data.keyID }
188
+ data.getDataStream(BcPublicKeyDataDecryptorFactory (key)).use {
189
+ PGPObjectFactory (it, jcaKeyFingerprintCalculator)
190
+ }.let {
191
+ return getDecryptResultFromFactory(it, arrayListOf ())
187
192
}
188
193
}
189
194
}
190
195
}
191
- // .let {
192
- // OpenPGPUtils.getMasterPrivateKey(privateKeyRing)
193
- // var privKey: PGPPrivateKey? = null
194
- // var encryptedData: PGPPublicKeyEncryptedData? = null
195
- // while (it.hasNext()) {
196
- // val data = it.next() as PGPPublicKeyEncryptedData
197
- // encryptedDataKeyId.add(data.keyID)
198
- // if (privKey == null) {
199
- // encryptedData = data
200
- // privKey = OpenPGPUtils.getMasterPrivateKey(privateKeyRing, encryptedData.keyID, password.toCharArray())
201
- // }
202
- // }
203
- // encryptedData?.getDataStream(BcPublicKeyDataDecryptorFactory(privKey))
204
- // }?.use { clear ->
205
- // PGPObjectFactory(clear, jcaKeyFingerprintCalculator)
206
- // }
207
196
}
208
197
return null
209
198
}
@@ -278,7 +267,11 @@ object KotlinPGP {
278
267
encryptedDataKeyId.add(data.keyID)
279
268
if (privKey == null ) {
280
269
encryptedData = data
281
- privKey = OpenPGPUtils .getMasterPrivateKey(privateKeyRing, encryptedData.keyID, password.toCharArray())
270
+ privKey = OpenPGPUtils .getEncryptionPrivateKey(
271
+ privateKeyRing,
272
+ encryptedData.keyID,
273
+ password.toCharArray()
274
+ )
282
275
}
283
276
}
284
277
encryptedData?.getDataStream(BcPublicKeyDataDecryptorFactory (privKey))
@@ -409,7 +402,7 @@ object KotlinPGP {
409
402
PGPEncryptedDataGenerator (it)
410
403
}.also {
411
404
encryptParameter.publicKey.map {
412
- OpenPGPUtils .getEncryptionKey (getPublicKeyRingFromString(it.key)) to it
405
+ OpenPGPUtils .getEncryptionPublicKey (getPublicKeyRingFromString(it.key)) to it
413
406
}.forEach { data ->
414
407
if (data.second.isHidden) {
415
408
it.addMethod(KtHiddenPublicKeyKeyEncryptionMethodGenerator (data.first))
@@ -474,7 +467,6 @@ object KotlinPGP {
474
467
}
475
468
476
469
477
-
478
470
fun verify (signatureData : SignatureData , publicKey : List <String >): VerifyResult {
479
471
return if (signatureData.onePassSignatureList == null || signatureData.signatureList == null ) {
480
472
if (signatureData.signatureList != null ) {
0 commit comments