> For the complete documentation index, see [llms.txt](https://docs.payments.thalescloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.payments.thalescloud.io/xpay-enablement/ja/api-rifarensu/sekyuriti/pkcs.md).

# PKCS

### 暗号化フォーマット <a href="#pkcs7-encryption-format" id="pkcs7-encryption-format"></a>

PKCS#7 メッセージ形式は RFC 2315/5652 で定義されています。

使用されるコンテンツ暗号化アルゴリズムは「AES256/CBC/PKCS7Padding」です。

鍵暗号化アルゴリズムは、TSP の構成によって 'RSAES-PKCS1-v1\_5' (RSA/NONE/PKCS1Padding) または 'RSA/NONE/OAEPWithSHA256AndMGF1Padding'（MGF1 が SHA-256 を使用）であり、プロジェクト開始時に共有された暗号化証明書を使用します。

暗号化結果は JSON メッセージで送信できるように base64 でエンコードされます。

#### 証明書交換

1. 顧客は RSA 2048 ビットまたは RSA 4096 ビットの鍵ペアを作成します。
2. 顧客はその RSA 鍵から証明書署名要求（CSR）を生成します。
3. 顧客は PEM 形式の CSR を Thales のプロジェクトチームに送信します。

#### コードサンプル

以下は BouncyCastle ライブラリを使用して PKCS#7 データの暗号化/復号化を扱う JAVA のコードサンプルです。

```java
package com.gemalto.test.pkcs7;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.util.Collection;

import org.bouncycastle.cms.CMSAlgorithm;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.KeyTransRecipientInformation;
import org.bouncycastle.cms.RecipientInformation;
import org.bouncycastle.cms.bc.BcCMSContentEncryptorBuilder;
import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.OutputEncryptor;
import org.bouncycastle.util.encoders.Base64;

public class MainPKCS7 {

	private static final String DATA_TO_ENCRYPT = "{\"fpan\":\"4974013055554646\",\"exp\":\"0316\",\"cvv\":\"123\"}";

	private static BouncyCastleProvider bcProvider;
	private static PrivateKey privKey;
	private static PublicKey pubKey;

	static {
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(2048);
			KeyPair kp = kpg.generateKeyPair();
			privKey = kp.getPrivate();
			pubKey = kp.getPublic();
			bcProvider = new BouncyCastleProvider();
			Security.addProvider(bcProvider);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		

		// 暗号化
		byte[] encData = encryptPKCS7(DATA_TO_ENCRYPT.getBytes());
		System.out.println("Encrypted data = " + Base64.toBase64String(encData));

		// 復号化
		byte[] result = decryptPKCS7(encData);
		System.out.println("Decrypted data = " + new String(result));

	}

	private static byte[] encryptPKCS7(byte[] plainData) throws Exception {
		// ジェネレータを設定する
		CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator();
		gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(new byte[] { 0x01 }, pubKey)
				.setProvider(bcProvider));
		// enveloped-data オブジェクトを作成する
		CMSProcessableByteArray data = new CMSProcessableByteArray(plainData);
		BcCMSContentEncryptorBuilder builder = new BcCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC);
		OutputEncryptor oe = builder.build();
		CMSEnvelopedData enveloped = gen.generate(data, oe);

		return enveloped.getEncoded();
	}

	private static byte[] decryptPKCS7(byte[] encryptedData) throws Exception {
		CMSEnvelopedData enveloped = new CMSEnvelopedData(encryptedData);
		Collection<RecipientInformation> recip = enveloped.getRecipientInfos().getRecipients();
		KeyTransRecipientInformation rinfo = (KeyTransRecipientInformation) recip.iterator().next();
		byte[] contents = rinfo.getContent(new JceKeyTransEnvelopedRecipient(privKey).setProvider(bcProvider));
		return (contents);
	}

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.payments.thalescloud.io/xpay-enablement/ja/api-rifarensu/sekyuriti/pkcs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
