2013-10-12 68 views
0

感謝您的關注。我最近一直在將基於對稱的舊加密系統更新到基於非對稱的RSA系統,以提高安全性。我認爲自己是一個適度熟練的PHP程序員。有人說,我完全使用phpseclib難住這個問題。多次訪問phpseclib類

看來我無法在我的腳本中的任何地方創建兩個類的實例。看我的代碼如下:

<?php include_once('Crypt/RSA.php'); 

     function signRSA($string_to_sign){ 
     $rsa = new Crypt_RSA(); 
     $rsa->loadKey(file_get_contents('pvk.txt')); // private key 
     $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); 
     $signature = $rsa->sign($string_to_sign); 
     return base64_encode($signature); 
     } 

     function decryptRSA($string_to_decrypt){ 
     $rsa = new Crypt_RSA(); 
     $rsa->loadKey(file_get_contents('pvk.txt')); // private key 
     return $rsa->decrypt(base64_decode($string_to_decrypt)); 
     } 

    //Which ever function is on top is the only one that appears to work. The first line below will work, however the second will fail returning me a null value. 

    $sig = signRSA($_POST['data']); 
    $plain = decryptRSA($_POST['otherdata']); 
?> 

我也嘗試創建個人'全局'變量。同樣的事情發生..

我期待着解決這個問題。

UPDATE:

我是能夠成功地打擊了我的問題雖然無關與我想的那樣。看起來我的客戶端應用程序(C#)和上面看到的PHP結尾使用了兩種不同的填充算法。這導致客戶端應用程序考慮數據損壞。在PHP腳本的邏輯中,這只是導致上述功能之一工作。

回答

0

它正常工作對我來說:

include_once('Crypt/RSA.php'); 

     function signRSA($string_to_sign){ 
     $rsa = new Crypt_RSA(); 
     $rsa->loadKey('-----BEGIN RSA PRIVATE KEY----- 
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp 
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh 
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2 
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX 
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il 
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF 
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k 
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl 
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= 
-----END RSA PRIVATE KEY-----'); // private key 
     $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); 
     $signature = $rsa->sign($code); 
     return base64_encode($signature); 
     } 

     function decryptRSA($string_to_decrypt){ 
     $rsa = new Crypt_RSA(); 
     $rsa->loadKey('-----BEGIN RSA PRIVATE KEY----- 
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp 
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh 
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2 
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX 
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il 
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF 
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k 
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl 
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= 
-----END RSA PRIVATE KEY-----'); // private key 
     return $rsa->decrypt(base64_decode($string_to_decrypt)); 
     } 

    //Which ever function is on top is the only one that appears to work. The first line below will work, however the second will fail returning me a null value. 

    $sig = signRSA('zzzzzzzzz'); 
    $plain = decryptRSA('G0KqVsNP6qsjVkKbnfCkSsiUdDnxBzIjA1tNHCeeKgkzqdkJF/zFuzVdtc8PrEfeR+SfrvdLiR5DqlAA 
LReLD5lPrj19+3Ci+igCQebItT8xmOHmKNHM+nQK2l6uL9AW03Vy/15sCTHqZ/dfmQMarzIb05DQUX57 
ftrBGu0XLwM='); 

echo base64_encode($sig) . "\r\n"; 
echo $plain; 

也許你的關鍵是不支持的格式?也許你試圖解密的密文不能與私鑰一起使用?我發佈的代碼片段是否適合您?如果是這樣,你將需要發佈更多信息。就像填寫密鑰和明文的內容一樣。

+0

感謝您的留言。看來我真的無法將適當的填充算法從C#連接到PHP。這導致解密或簽名的輸出被破壞(就不匹配客戶端應用程序而言)。我相信這個問題在phpseclib構造函數中說謊,我無法創建該類的多個實例。 –