Note that openssl_seal() causes Internal Server Error if $data is an empty string.
openssl_seal
(PHP 4 >= 4.0.4, PHP 5)
openssl_seal — データをシール(暗号化)する
説明
int openssl_seal
( string $data
, string &$sealed_data
, array &$env_keys
, array $pub_key_ids
)
openssl_seal() は、ランダムに生成された秘密鍵により RC4 を使用して data をシール(暗号化) します。このキーは、pub_key_ids を ID とする 公開鍵で暗号化されます。これは、暗号化されたデータを複数の受信者に 送信できることを意味します(この際、各受信者は送信側に公開鍵を 提供します)。各受信者は、暗号化されたデータとその受信者の 公開鍵で暗号化されたエンベロープキーを受け取る必要があります。
パラメータ
- data
-
- sealed_data
-
- env_keys
-
- pub_key_ids
-
返り値
成功時にシール(暗号化)されたデータの長さ、エラー時に FALSE を返します。成功時には、暗号化されたデータが sealed_data に、エンベロープキーが env_keys に返されます。
例
例1 openssl_seal() の例
<?php
// $data には、暗号化されるデータが含まれていると仮定
// 受信者の公開鍵を取得し、使用可能にする
$fp = fopen("/src/openssl-0.9.6/demos/maurice/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk1 = openssl_get_publickey($cert);
// 2 番目の受信者についても同様
$fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pk2 = openssl_get_publickey($cert);
// メッセージを暗号化。$pk1 および $pk2 の所有者のみが、$sealed を
// データをそれぞれ $ekeys[0] および $ekeys[1] で復号化することが
// 可能
openssl_seal($data, $sealed, $ekeys, array($pk1, $pk2));
// キーをメモリから開放する
openssl_free_key($pk1);
openssl_free_key($pk2);
?>
openssl_seal
omnibus at omnibus dot edu dot pl
19-Feb-2009 11:38
19-Feb-2009 11:38
hfuecks at nospam dot org
29-Oct-2007 03:44
29-Oct-2007 03:44
openssl_seal() can work well when you need to pass data securely to other platforms / languages. What openssl_seal() does is;
1. Generate a random key
2. Encrypt the data symmetrically with RC4 using the random key
3. Encrypt the random key itself with RSA using the public key / certificate
4. Returns the encrypted data and the encrypted key
So to decrypt the steps are simply;
1. Decrypt the key using RSA and your private key
2. Decrypt the data using RC4 and the decrypted key
The trickiest part may be figuring out how handle the private key - BouncyCastle ( http://www.bouncycastle.org/ ) provides a PEMReader for Java and C# while Not Yet commons-ssl ( http://juliusdavies.ca/commons-ssl/ ) has a KeyStoreBuilder to build Java keystores out of a PEM certificate.
A complete example in Java is described at http://blog.local.ch/archive/2007/10/29/openssl-php-to-java.html
devel@no-spam
15-Jun-2005 01:20
15-Jun-2005 01:20
"seals (encrypts) data by using RC4 with a randomly generated secret key"
It should be noted that the randomly generated secret key is 128 bits long (openssl: EVP_rc4(void): RC4 stream cipher. This is a variable key length cipher with default key length 128 bits.)
