मुझे यह जेसनक्रिप्ट लाइब्रेरी (http://travistidwell.com/jsencrypt) मिलती है, 2 दिन बाद मुझे अपना समाधान मिल गया।
मुझे एकमात्र समस्या है जब मैं एक लंबा पाठ भेजता हूं। ऐसा इसलिए है क्योंकि आरएसए, परिभाषा के अनुसार, सीमित लंबाई के तारों का समर्थन करता है।
https://security.stackexchange.com/questions/33434/rsa-maximum-bytes-to-encrypt-comparison-to-aes-in-terms-of-security/33445#33445
आरएसए, के रूप में PKCS # 1 द्वारा परिभाषित, सीमित आकार के "संदेश" एन्क्रिप्ट करता है। सामान्यतः "v1.5 पैडिंग" और 2048-बिट आरएसए कुंजी के साथ, अधिकतम डेटा का आकार जिसे आरएसए के साथ एन्क्रिप्ट किया जा सकता है, 245 बाइट्स है। अब और नहीं।
अर्थात अगर मैं 1024 के private_key_bits का उपयोग मैं
"José compró en Perú una vieja zampoña. Excusándose, Sofía tiró su whisky al desagüe de la banqueta."
कुछ भी नहीं रह गया है भेज सकते हैं। अगर मैं 512 के private_key_bits का उपयोग मैं
"José compró en Perú una vieja zampoña. Excusánd"
कुछ भी नहीं रह गया है भेज सकते हैं।
लंबे तार JavaScript कंसोल रिपोर्ट पर: "संदेश आरएसए के लिए बहुत लंबा"
तो अगर आप लंबे तार आप सेक चाहिए एन्क्रिप्ट करना चाहते हैं और उन्हें जावास्क्रिप्ट एन्क्रिप्शन से पहले और डिक्रिप्शन के बाद शामिल होने और php पर विस्तारित, मुझे लगता है कि विभाजित zlib विभाजन/जुड़ने के लिए एक अच्छा समाधान है क्योंकि यह जावास्क्रिप्ट और PHP पर समर्थित है।
<?php
//------------------------------------------------------------
// Global Settings.
//------------------------------------------------------------
ini_set('display_errors', 1);
error_reporting(E_ALL);
$directorio = "/path/to/key/directory/apache/writable/";
$nombre_base = "llaves_php";
//------------------------------------------------------------
// Initialization.
//------------------------------------------------------------
$encabezado_html = "";
$cuerpo_html = "";
//------------------------------------------------------------
// Loading keys
//------------------------------------------------------------
list($privateKey, $pubKey) =
cargar_llaves_RSA($directorio, $nombre_base);
//------------------------------------------------------------
// Form that uses javascript to encrypt data.
// (it uses only the public key)
//------------------------------------------------------------
$librerias_html = "
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/".
"jquery/3.2.1/jquery.min.js'></script>
<script type='text/javascript'
src='lib/jsencrypt.js'></script>
";
$pubKey_html = htmlentities($pubKey);
$datos_html = "
<h2>Cifrando con Javascript</h2>
<input type='text' id='mensaje' />
<br />
<button id='ENVIAR'>Enviar</button>
<br />
<textarea id='pubkey' style='display: none;'>".
$pubKey_html.
"</textarea>
<script type='text/javascript'>
$('#ENVIAR').click(function() {
var codificador = new JSEncrypt();
codificador.setKey($('#pubkey').val());
var cifrado = codificador.encrypt($('#mensaje').val());
window.open('?mensaje=' + encodeURIComponent(cifrado)
, '_top');
});
</script>
";
//------------------------------------------------------------
// Decrypting using php (it uses only the privateKey)
//------------------------------------------------------------
if (isset($_REQUEST['mensaje'])) {
openssl_private_decrypt(base64_decode($_REQUEST['mensaje'])
, $descifrado
, $privateKey);
$datos_html.= "
<h2>Descifrando con PHP</h2>
".$descifrado."
";
}
//------------------------------------------------------------
// HTML DISPLAY
//------------------------------------------------------------
$encabezado_html.= "<title>Receptor de mensaje cifrado</title>"
. $librerias_html;
$cuerpo_html.= $datos_html;
$contenido = "<head>$encabezado_html</head><body>$cuerpo_html</body>";
$contenido = "<html>$contenido</html>";
print $contenido;
//============================================================
//============================================================
// Functions
//============================================================
//============================================================
//------------------------------------------------------------
function cargar_llaves_RSA($directorio, $nombre_base) {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PROPÓSITO: Genera o carga desde archivos las llaves RSA
// ENTRADAS:
// $directorio: Directorio donde se encuentran los archivos.
// $nombre_base: Nombre, sin extensión, de los archivos con
// las llaves.
// SALIDAS:
//------------------------------------------------------------
if ( !file_exists($directorio.$nombre_base.".crt")
|| !file_exists($directorio.$nombre_base.".pub")) {
list($privateKey, $pubKey) = crear_llaves_RSA($directorio.$nombre_base);
} else {
//------------------------------------------------------------
// CARGA DE LLAVES RSA ARCHIVADAS
//------------------------------------------------------------
$privateKey = file_get_contents($directorio.$nombre_base.".crt");
if (!$privKey = openssl_pkey_get_private($privateKey))
die('Loading Private Key failed');
$pubKey = file_get_contents($directorio.$nombre_base.".pub");
}
return array($privateKey, $pubKey);
}
//------------------------------------------------------------
function crear_llaves_RSA($ruta_base) {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PROPÓSITO:
// generacion de llaves RSA en php
// ENTRADAS:
// $ruta_base: Ruta de los archivos a generar sin extensión.
// SALIDAS:
// Se generarán dos archivos, uno con la llave privada con
// extensión .crt, el otro con llave pública con extensión
// .pub; la función retorna tanto la llave pública como la
// privada en un arreglo.
//------------------------------------------------------------
$config = array(
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$llavePrivadaCruda = openssl_pkey_new($config);
openssl_pkey_export_to_file($llavePrivadaCruda, $ruta_base.".crt");
$privateKey = file_get_contents($ruta_base.".crt");
openssl_pkey_export($llavePrivadaCruda, $privKey);
$pubKeyData = openssl_pkey_get_details($llavePrivadaCruda);
$pubKey = $pubKeyData["key"];
file_put_contents($ruta_base.".pub", $pubKey);
openssl_free_key($llavePrivadaCruda);
return array($privateKey, $pubKey);
}
//------------------------------------------------------------
function Mostrar($valor) {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PROPÓSITO: Genera el código HTML para presentar una
// variable embebida en la página.
// ENTRADAS:
// $valor: el valor a presentar.
// SALIDAS: código html que permite visualizar la variable.
//------------------------------------------------------------
$retorno = htmlentities(stripslashes(var_export($valor, true)));
$retorno = "<pre>$retorno</pre>";
return $retorno;
}
?>
निर्देशिका पेड़ चाहिए की तरह दिखता है:
मेरे काम के कोड इस प्रकार है
├── script.php
└── lib
└── jsencrypt.js
और एक नामी निर्देशिका के सार्वजनिक क्षेत्र के बाहर php द्वारा लिखने योग्य
/path/to/key/directory/apache/writable/
ऐसा लगता है जैसे आप इस पर विचार कर रहे हैं एक ग्राहक और सर्वर के बीच संवेदनशील डेटा के एन्क्रिप्शन के लिए। आपको चेतावनी दी जाती है कि यह एक मैन-इन-द-बीच हमले के खिलाफ किसी भी सुरक्षा की पेशकश नहीं करेगा जहां से गुजरने वाले किसी भी डेटा से समझौता किया जाएगा। सुरक्षित, प्रमाणीकृत अंत-टू-एंड एन्क्रिप्शन के लिए HTTPS का उपयोग करें। – deed02392