<code>
public
static
function
envelope(
$data
){
$Millisecond
= SignEnvelope::getMillisecond();
$dataFile
=
"./rsa/"
.
$Millisecond
.
"data.txt"
;
$signedFile
=
"./rsa/"
.
$Millisecond
.
"signed.txt"
;
$signedDataFile
=
"./rsa/"
.
$Millisecond
.
"signedData.txt"
;
$envelopeFile
=
"./rsa/"
.
$Millisecond
.
"envelope.txt"
;
#加载p12
openssl_pkcs12_read (
file_get_contents
(Contants::pri),
$certs
, Contants::passWord);
$signCert
=
$certs
[
'cert'
];
$signKey
=
$certs
[
'pkey'
];
#加载加密证书
$encryCert
=
file_get_contents
(Contants::pub);
#加密原文
$fp
=
fopen
(
$dataFile
,
"w"
);
fwrite(
$fp
,
$data
);
fclose(
$fp
);
#签名
openssl_pkcs7_sign(
$dataFile
,
$signedFile
,
$signCert
,
array
(
$signKey
,
""
), NULL, PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSIGS);
$signedBase64
=
file_get_contents
(
$signedFile
);
$signedBase64
=
substr
(
$signedBase64
,
strpos
(
$signedBase64
,
"base64"
) +
strlen
(
"base64"
));
trim(
$signedBase64
);
#print_r(
$signedBase64
);
#
echo
"<br><br><br>"
;
$signedData
=
base64_decode
(
$signedBase64
);
$fp
=
fopen
(
$signedDataFile
,
"w"
);
fwrite(
$fp
,
$signedData
);
fclose(
$fp
);
#信封
openssl_pkcs7_encrypt(
$signedDataFile
,
$envelopeFile
,
$encryCert
, NULL, PKCS7_BINARY, OPENSSL_CIPHER_3DES);
$envelopeBase64
=
file_get_contents
(
$envelopeFile
);
$envelopeBase64
=
substr
(
$envelopeBase64
,
strpos
(
$envelopeBase64
,
"base64"
) +
strlen
(
"base64"
));
trim(
$envelopeBase64
);
$envelopeBase64
=
base64_decode
(
$envelopeBase64
);
$envelopeBase64
=
base64_encode
(
$envelopeBase64
);
unlink(
$dataFile
);
unlink(
$signedFile
);
unlink(
$signedDataFile
);
unlink(
$envelopeFile
);
return
$envelopeBase64
;</code>