Cara menggunakan sha256 with rsa php

Note: The OAEP padding uses random bytes in the padding, and therefore each time encryption happens, even using the same data and key, the result will be different -- but still valid. One should not expect to get the same output.

Global Unlock Sample for sample code.

$pubkey = new CkPublicKey[];

$sbPem = new CkStringBuilder[];
$bCrlf = true;
$sbPem->AppendLine['-----BEGIN PUBLIC KEY-----',$bCrlf];
$sbPem->AppendLine['MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA33TqqLR3eeUmDtHS89qF',$bCrlf];
$sbPem->AppendLine['3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA',$bCrlf];
$sbPem->AppendLine['04DDnEFGAf+kDQiNSe2ZtqC7bnIc8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7s',$bCrlf];
$sbPem->AppendLine['Jp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwF',$bCrlf];
$sbPem->AppendLine['wnxCsU5+UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUE',$bCrlf];
$sbPem->AppendLine['f0YN3/Q0auBkdbDR/ES2PbgKTJdkjc/rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1W',$bCrlf];
$sbPem->AppendLine['SwIDAQAB',$bCrlf];
$sbPem->AppendLine['-----END PUBLIC KEY-----',$bCrlf];

// Load the public key object from the PEM. 
$success = $pubkey->LoadFromString[$sbPem->getAsString[]];
if [$success != true] {
    print $pubkey->lastErrorText[] . "\n";
    exit;
}

$originalData = 'This is the original data to be SHA-256 hashed and RSA encrypted.';

// First we SHA-256 hash the original data.
$crypt = new CkCrypt2[];
$crypt->put_HashAlgorithm['SHA-256'];
$hashBytes = new CkByteData[];
$success = $crypt->HashString[$originalData,$hashBytes];

// Now to RSA encrypt using OAEP padding with SHA-1 for the mask function.
$rsa = new CkRsa[];
$rsa->put_OaepPadding[true];
$rsa->put_OaepHash['SHA1'];
$rsa->ImportPublicKeyObj[$pubkey];
$rsa->put_EncodingMode['base64'];

// Note: The OAEP padding uses random bytes in the padding, and therefore each time encryption happens,
// even using the same data and key, the result will be different --  but still valid.  One should not expect
// to get the same output.
$bUsePrivateKey = false;
$encryptedStr = $rsa->encryptBytesENC[$hashBytes,$bUsePrivateKey];
if [$rsa->get_LastMethodSuccess[] != true] {
    print $rsa->lastErrorText[] . "\n";
    exit;
}

print 'Base64 RSA encrypted output: ' . $encryptedStr . "\n";

?>

From RFC 3174 - The US Secure Hash Algorithm 1: "SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."

Enkripsi akan menjamin data-data tetap aman meskipun berada di tangan orang lain, karena mereka tidak tahu isi aslinya.

Pada kesempatan ini, saya akan membahas beberapa fungsi enkripsi yang sudah disediakan oleh PHP, diantaranya:

Bài mới nhất

Chủ Đề