मेरे पास PHP में एक वेब-सेवा है जो एक संदेश एन्क्रिप्ट करने के लिए एक कीपैयर उत्पन्न करती है, और जावा में एक एप्लिकेशन जो निजीकी को पुनर्प्राप्त करता है और संदेश को डिक्रिप्ट करता है।एन्क्रिप्शन PHP, डिक्रिप्शन जावा
php मैं http://phpseclib.sourceforge.net/ उपयोग कर रहा हूँ और इस दो फ़ाइलों के लिए:
keypair.php
<?php
set_time_limit(0);
if(file_exists('private.key'))
{
echo file_get_contents('private.key');
}
else
{
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->createKey();
$res = $rsa->createKey();
$privateKey = $res['privatekey'];
$publicKey = $res['publickey'];
file_put_contents('public.key', $publicKey);
file_put_contents('private.key', $privateKey);
}
?>
encrypt.php
<?php
include('Crypt/RSA.php');
//header("Content-type: text/plain");
set_time_limit(0);
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->loadKey(file_get_contents('public.key')); // public key
$plaintext = 'Hello World!';
$ciphertext = $rsa->encrypt($plaintext);
echo base64_encode($ciphertext);
?>
और जावा में मैं इस कोड है:
package com.example.app;
import java.io.DataInputStream;
import java.net.URL;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args)
{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
try {
BASE64Decoder decoder = new BASE64Decoder();
String b64PrivateKey = getContents("http://localhost/api/keypair.php").trim();
String b64EncryptedStr = getContents("http://localhost/api/encrypt.php").trim();
System.out.println("PrivateKey (b64): " + b64PrivateKey);
System.out.println(" Encrypted (b64): " + b64EncryptedStr);
SecretKeySpec privateKey = new SecretKeySpec(decoder.decodeBuffer(b64PrivateKey) , "AES");
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainText = decoder.decodeBuffer(b64EncryptedStr);
System.out.println(" Message: " + plainText);
}
catch(Exception e)
{
System.out.println(" Error: " + e.getMessage());
}
}
public static String getContents(String url)
{
try {
String result = "";
String line;
URL u = new URL(url);
DataInputStream theHTML = new DataInputStream(u.openStream());
while ((line = theHTML.readLine()) != null)
result = result + "\n" + line;
return result;
}
catch(Exception e){}
return "";
}
}
मेरे प्रश्न हैं:
- मुझे "आरएसए कुंजी नहीं" कहने का अपवाद क्यों है?
- मैं इस कोड को कैसे सुधार सकता हूं? मैंने जावा और PHP के बीच एन्कोडिंग और कम्यूनिकेशन त्रुटियों से बचने के लिए बेस 64 का उपयोग किया है।
- यह अवधारणा सही है? मेरा मतलब है, मैं इसे सही तरीके से उपयोग कर रहा हूँ?
क्या पूर्व-बेस 64 डेटा डीकोडेड बेस 64 डेटा से मेल खाता है? –
हां, मैंने अभी इसका परीक्षण किया है, और बेस 64 के साथ डीकोड किए जाने के बाद PHP और जावा स्ट्रिंग का md5 चेकसम समान है। –
शायद मेरा SecretKeySpec गलत है? मैंने बिना सफलता के एल्गोरिदम के मूल्य को बदलने की कोशिश की है। –