@rasalom1985:
1.tworzę XML z fa3 wypełniam nip, daty
2.skryptem testowym generuję dane
Kopiuj
<?php
declare(strict_types=1);
require __DIR__ . '/encryptor/vendor/autoload.php';
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA;
// --- Ścieżki do plików ---
$xmlPath = 'FA_3_Przykład_1.xml';
$publicKeyPath = 'mf_public_symmetric.pem';
// --- Wczytaj XML i Wyczyść/Znormalizuj ---
$rawXmlContent = file_get_contents($xmlPath);
function removeUtf8Bom(string $content): string {
// Usuń BOM na początku, jeśli występuje
if (substr($content, 0, 3) === "\xEF\xBB\xBF") {
$content = substr($content, 3);
}
// Usuń ukryty znak U+FEFF (zero-width no-break space), jeśli się ostał
$content = preg_replace('/^\x{FEFF}/u', '', $content);
// Usuń ewentualny początkowy znak końca linii (\n, \r, \r\n)
$content = preg_replace('/^\R/u', '', $content);
return $content;
}
$originalXml = removeUtf8Bom($rawXmlContent); // czysty XML
$originalXml = preg_replace("/\r\n/", "\n", $originalXml);
file_put_contents($xmlPath, $originalXml);
if ($rawXmlContent === false) {
http_response_code(500);
die(json_encode(['error' => "Nie można wczytać pliku XML: $xmlPath"]));
}
file_put_contents('debug_original_xml.bin', $originalXml);
// --- Oblicz hash i rozmiar faktury (na podstawie znormalizowanego XML) ---
$invoiceHash = base64_encode(hash('sha256', $originalXml, true));
// Używamy strlen() na znormalizowanej treści, co daje rozmiar w bajtach.
$invoiceSize = strlen($originalXml);
// --- Generuj klucz AES i IV ---
try {
$symmetricKey = random_bytes(32); // 256-bit AES
$iv = random_bytes(16); // 128-bit IV
} catch (Exception $e) {
http_response_code(500);
die(json_encode(['error' => 'Błąd generowania losowych bajtów: ' . $e->getMessage()]));
}
// --- Szyfruj XML za pomocą AES-256-CBC ---
// 2. KOREKTA: Zmieniono OPENSSL_RAW_DATA na 0, by wymusić standardowe dopełnienie PKCS#7.
$encrypted = openssl_encrypt($originalXml, 'aes-256-cbc', $symmetricKey, OPENSSL_RAW_DATA, $iv);
if ($encrypted === false) {
http_response_code(500);
die(json_encode(['error' => 'Błąd szyfrowania AES: ' . openssl_error_string()]));
}
// --- Połącz IV + zaszyfrowany XML ---
//$encryptedWithIv = $iv . $encrypted;
//file_put_contents('debug_encrypted_invoice.bin', $encryptedWithIv);
//$encryptedBase64 = base64_encode($encryptedWithIv);
$encryptedBase64 = base64_encode($encrypted);
// --- Oblicz hash i rozmiar zaszyfrowanej faktury ---
$encryptedHash = base64_encode(hash('sha256', $encrypted, true));
$encryptedSize = strlen($encrypted);
// --- Wczytaj certyfikat MF do zaszyfrowania klucza AES ---
$publicKeyPem = file_get_contents($publicKeyPath);
if ($publicKeyPem === false) {
http_response_code(500);
die(json_encode(['error' => "Nie można wczytać klucza publicznego: $publicKeyPath"]));
}
try {
$public = PublicKeyLoader::load($publicKeyPem);
$publicOaep = $public
->withPadding(RSA::ENCRYPTION_OAEP)
->withHash('sha256')
->withMGFHash('sha256');
$encryptedKeyRaw = $publicOaep->encrypt($symmetricKey);
$encryptedKeyBase64 = base64_encode($encryptedKeyRaw);
} catch (\Throwable $e) {
http_response_code(500);
die(json_encode(['error' => 'Błąd szyfrowania klucza RSA: ' . $e->getMessage()]));
}
// --- Przygotuj dane JSON ---
$initializationVectorBase64 = base64_encode($iv);
// ... [Kod diagnostyczny] ...
// Oblicz rozmiary lokalnie
$size_original_strlen = strlen($originalXml);
$size_encrypted_strlen = strlen($encrypted);
$diagnostics = [
'invoiceSize_sent' => $invoiceSize,
'size_original_strlen_normalized' => $size_original_strlen,
'encryptedSize_sent' => $encryptedSize,
'size_encrypted_strlen' => $size_encrypted_strlen,
'encryptedInvoiceHash_sent' => $encryptedHash,
];
$sessionRequest = [
"formCode" => [
"systemCode" => "FA (3)",
"schemaVersion" => "1-0E",
"value" => "FA"
],
"encryption" => [
"encryptedSymmetricKey" => $encryptedKeyBase64,
"initializationVector" => $initializationVectorBase64
]
];
$invoicePayload = [
"invoiceHash" => $invoiceHash,
"invoiceSize" => $invoiceSize,
"encryptedInvoiceHash" => $encryptedHash,
"encryptedInvoiceSize" => $encryptedSize,
"encryptedInvoiceContent" => $encryptedBase64,
"offlineMode" => false
];
// --- Wyślij wynik JSON do stdout ---
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
"formCode" => [
"systemCode" => "FA (3)",
"schemaVersion" => "1-0E",
"value" => "FA"
],
"encryption" => [
"encryptedSymmetricKey" => $encryptedKeyBase64,
"initializationVector" => $initializationVectorBase64
],
"invoicePayload" => $invoicePayload
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
- otwieram sesję podając wynik
- wysyłam fakturę podając to samo cu wygenerowałem ale tylko w zakresie
Kopiuj
{
"invoiceHash": "LSnV3Tfg/Isj5fdafOLG6ocvm+FfThULwFTWQuV0JAA=",
"invoiceSize": 3278,
"encryptedInvoiceHash": "odlduX/GLRO+HUk8dE8QtDLnkOthtb/oWvKPSe909Uo=",
"encryptedInvoiceSize": 3280,
"encryptedInvoiceContent": "G1SxYHiB19x6WaHIGNPyxNUred5Kyo.....",
"offlineMode": false
}
- zamykam sesję
- sprawdzam faktury w sesji
sprawdzałem i mam sukces faktury
Kopiuj
{
"invoices": [
{
"ordinalNumber": 1,
"invoiceNumber": "FV2025/12/150",
"ksefNumber": "7010421971-20251218-010000782431-8C",
"referenceNumber": "20251218-EE-1892332000-3153AF1783-19",
"invoiceHash": "LSnV3Tfg/Isj5fdafOLG6ocvm+FfThULwFTWQuV0JAA=",
"acquisitionDate": "2025-12-18T07:09:24.800635+00:00",
"invoicingDate": "2025-12-18T07:09:24.6584301+00:00",
"permanentStorageDate": "2025-12-18T07:09:25.021246+00:00",
"upoDownloadUrl": "https://ksef-test.mf.gov.pl/storage/08/20251218-so-18798b1000-3f1c22cc2d-06/invoice-upo/upo_7010421971-20251218-010000782431-8C.xml?skoid=0e92608a-831d-404b-9945-197ed82a5dbc&sktid=647754c7-3974-4442-a425-c61341b61c69&skt=2025-12-17T15%3A22%3A17Z&ske=2025-12-24T15%3A22%3A17Z&sks=b&skv=2025-01-05&sv=2025-01-05&st=2025-12-18T07%3A05%3A51Z&se=2025-12-21T07%3A10%3A51Z&sr=b&sp=r&sig=My1gQdiTazqV3itEezlfOYxvfS%2FRFBxOXBnqndG0xqY%3D",
"upoDownloadUrlExpirationDate": "2025-12-21T07:10:51.1058158+00:00",
"invoicingMode": "Online",
"status": {
"code": 200,
"description": "Sukces"
}
}
]
}