我一直在忙于尝试在浏览器客户端之间建立安全交换,使用 webcrypto api 和使用 openssl 的 PHP 服务器。我已经尽可能多地分解了。我写了一些 javascript 来生成一个密钥对,打印出私有和公共的值,加密一个简单的字符串并将其打印出来。
我已将这些值直接复制到一个简单的 php 脚本中。尝试使用 javascipt 中的值对其进行解码。
编码和解码在 javascript 中正常工作(如下所示),在 php 中解码不能(也在 javascript 下发布)。我似乎也找不到在 PHP 中设置 SHA-512 声明的位置。有没有人有过这种交流的经验,也许能给我指明正确的方向。选择 RSA-OAEP 是因为它受到 chrome、mozilla、IE11 和 Safari 以及 PHP 的支持。
亲切的问候,吉迪恩
// JavaScript Document
var keyPair;
var pemPublicKey;
var pemPrivateKey;
var _spki;
var _pkcs8;
window.crypto.subtle.generateKey({
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]), // 24 bit representation of 65537
hash: {name: "SHA-512"}
}, true, ["encrypt", "decrypt"])
.then(function(newKeyPair) {
keyPair = newKeyPair;
return keyPair;
})
.then(function(keyPair) {
window.crypto.subtle.exportKey('spki', keyPair.publicKey)
.then(function(spki) {
_spki = spki;
var pemPublicKey = convertBinaryToPem(spki, "PUBLIC KEY");
document.writeln(pemPublicKey);
sendToPhp();
});
window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey)
.then(function(pkcs8) {
_pkcs8 = pkcs8;
var pemPrivateKey = convertBinaryToPem(pkcs8, "PRIVATE KEY");
document.writeln(pemPrivateKey);
})
});
function sendToPhp() {
window.crypto.subtle.importKey('spki', _spki, {name:"RSA-OAEP", hash: {name: "SHA-512"}}, false, ["encrypt"])
.then(function(cryptokey) {
window.crypto.subtle.encrypt({ name: "RSA-OAEP"}, cryptokey, str2ab('mijn geheimpje') )
.then(function(encrypted){
//returns an ArrayBuffer containing the encrypted data
document.writeln(arrayBufferToBase64String(encrypted));
receivedFromPhp(arrayBufferToBase64String(encrypted));
});
});
}
function receivedFromPhp(encrypted) {
window.crypto.subtle.importKey('pkcs8', _pkcs8, {name:"RSA-OAEP", hash: {name: "SHA-512"}}, false, ["decrypt"])
.then(function(cryptokey) {
window.crypto.subtle.decrypt({ name: "RSA-OAEP"}, cryptokey, base64StringToArrayBuffer(encrypted) )
.then(function(decrypted){
//returns an ArrayBuffer containing the encrypted data
var decryp = ab2str(decrypted);
debugger;
});
});
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function base64StringToArrayBuffer(base64) {
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function arrayBufferToBase64String(arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer)
var byteString = '';
for (var i=0; i<byteArray.byteLength; i++) {
byteString += String.fromCharCode(byteArray[i]);
}
return btoa(byteString);
}
function convertBinaryToPem(binaryData, label) {
var base64Cert = arrayBufferToBase64String(binaryData);
var pemCert = "-----BEGIN " + label + "-----\r\n";
var nextIndex = 0;
var lineLength;
while (nextIndex < base64Cert.length) {
if (nextIndex + 64 <= base64Cert.length) {
pemCert += base64Cert.substr(nextIndex, 64) + "\r\n";
} else {
pemCert += base64Cert.substr(nextIndex) + "\r\n";
}
nextIndex += 64;
}
pemCert += "-----END " + label + "-----\r\n";
return pemCert;
}
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$pemPublicKey = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvzJ07T/SiZsUPfC4ymwj
G/TqVdO04QZRUMcsmHeUG0BawSxlwoz+0YD48UZFYyTetw3egoasQfkvOPIUKuqq
mPEXwGsVlLbkqvPsgNA2K6Zye8El9DEp83eoPqylopU0L9zSnQp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EiSlNmBkvd8pB+iMyAwA7P2tmamrpNQYbEjoQu0mCNPUVrft1QI1IS4XWAL4+HP2vBWV41AttL8XjFxicrR3mXXZVukwiu7PJFPjwW9cLGEgTMkcpBkPZoTGPefiCQYVh4LEq6fYb4kdw==';
//just for testing if it works with the public/private keys supplied by javascript, which it does
//$publicKey = openssl_pkey_get_public($pemPublicKey);
//openssl_public_encrypt('mijn geheimpje',$encr,$publicKey,OPENSSL_PKCS1_OAEP_PADDING);
//$encr64 = base64_encode($encr);
$privateKey = openssl_get_privatekey($pemPrivateKey);
if (!$privateKey) {
echo "Cannot get private key";
}
$encr = base64_decode($encrMessage);
$b = openssl_private_decrypt($encr,$decr,$privateKey,OPENSSL_PKCS1_OAEP_PADDING);
if (!$b) {
echo "Cannot decode message";
}
echo "String decrypt :". $decr;
?>