Welcome to our new developer portal! Use the "Ask" button to chat with our AI Agent.
For the complete documentation index, see llms.txt. This page is also available as Markdown.

PKCS

Formato de cifrado

El formato de mensaje PKCS#7 está definido en RFC 2315/5652.

El algoritmo de cifrado de contenido utilizado es 'AES256/CBC/PKCS7Padding'.

El algoritmo de cifrado de clave es 'RSAES-PKCS1-v1_5' (RSA/NONE/PKCS1Padding) o 'RSA/NONE/OAEPWithSHA256AndMGF1Padding' (con MGF1 usando SHA-256), dependiendo de la configuración de TSP, con un certificado de cifrado compartido al comienzo del proyecto.

El resultado del cifrado se codifica usando base64 para poder ser transportado en el mensaje JSON.

Intercambio de certificados

  1. El cliente crea un par de claves RSA de 2048 bits o RSA de 4096 bits.

  2. El cliente genera una Solicitud de Firma de Certificado (CSR) a partir de esa clave RSA.

  3. El cliente envía al equipo de proyecto de Thales la CSR en formato PEM.

Ejemplo de código

Aquí hay un ejemplo de código JAVA que maneja el cifrado/descifrado de los datos PKCS#7 usando la biblioteca BouncyCastle.

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 {
		

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

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

	}

	private static byte[] encryptPKCS7(byte[] plainData) throws Exception {
		// configurar el generador
		CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator();
		gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(new byte[] { 0x01 }, pubKey)
				.setProvider(bcProvider));
		// crear el objeto 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);
	}

}

Última actualización

¿Te fue útil?