Commonly used Data Encryption Algorithm rule (PHP)

1. dictionary sort, and md5 encrypted (field = 1 Field 2 Field Value = & field value plus key)

/ ** 
* @param $ Array need to encrypt data
* @param $ signKey Encryption Key
* @return String keyed MD5 result
* /
function Sign (Array $, $ signkey) {
ksort ($ Array); // dictionary sorting ( ascending order)
$ STR = ""; // the results are combined into key1 = value & key2 = value & signkey format
the foreach ($ Array aS $ K => $ V) {
$ STR = $ K '=' $ V '&.... ';
}
$ = STR $ STR $ signkey;. signkey // add (if the demand is required to remove the encryption key & coupled, the function may be used to remove the last substr php an ampersand) return md5 ($ str); }

 

2.rsa public key to encrypt and decrypt the private key to decrypt the private key encryption and signature verification

/ ** 
* public key cryptography
* @param unknown_type $ sourcestr encrypted string
* @param unknown_type $ publickey public key
* /
function publickey_encodeing ($ sourcestr, the publickey $)
{
  // To initialize the public, to ensure whether it is formatted or unformatted when you fill out the public key can be verified. 
str_replace the publickey = $ ( "the PUBLIC KEY ----- ----- the BEGIN", "", $ the publickey);
$ the publickey = str_replace ( "the PUBLIC KEY ----- ----- the END", " ", the publickey $);
$ the publickey = str_replace (" \ n-"," ", $ the publickey);
. $ = the publickey 'the BEGIN the PUBLIC KEY ----- -----' PHP_EOL.wordwrap (the publickey $, 64 , "\ n", true) .PHP_EOL .'----- END PUBLIC KEY ----- ';

$ pubkeyid = openssl_get_publickey ($ publickey) ; // generates a key resource ID
IF (openssl_public_encrypt ($ sourcestr, crypttext $, $ pubkeyid, OPENSSL_PKCS1_PADDING)) // OPENSSL_PKCS1_PADDING RSA_PKCS1_PADDING fill mode using
{
return $ crypttext;
}
return FALSE;
}

/ ** 
* decryption public key
* /
function publickey_decodeing ($ crypttext, the publickey $)
{
    // To initialize the public, to ensure whether it is formatted or unformatted when you fill out the public key can be verified. 
str_replace the publickey = $ ( "the PUBLIC KEY ----- ----- the BEGIN", "", $ the publickey);
$ the publickey = str_replace ( "the PUBLIC KEY ----- ----- the END", " ", the publickey $);
$ the publickey = str_replace (" \ n-"," ", $ the publickey);
. $ = the publickey 'the BEGIN the PUBLIC KEY ----- -----' PHP_EOL.wordwrap (the publickey $, 64 , "\ n", true) .PHP_EOL .'----- END PUBLIC KEY ----- ';

$ pubkeyid = openssl_get_publickey ($ publickey) ; // generates a key resource ID
IF (openssl_public_decrypt ($ crypttext, sourcestr $, $ pubkeyid, OPENSSL_PKCS1_PADDING)) // OPENSSL_PKCS1_PADDING RSA_PKCS1_PADDING fill mode using
{
return $ sourcestr;
}
return FALSE;
}
/ ** 
* private key to decrypt
* /
function privatekey_decodeing ($ crypttext, $ PrivateKey)
{
// To initialize the private key, warranties, whether formatted or unformatted when you fill out the private key can be verified.
str_replace PrivateKey = $ ( "the RSA PRIVATE KEY ----- ----- the BEGIN", "", $ PrivateKey);
$ PrivateKey = str_replace ( "the RSA PRIVATE KEY ----- ----- the END" , "", $ PrivateKey);
$ PrivateKey = str_replace ( "\ n-", "", $ PrivateKey);

. PrivateKey $ = "the RSA PRIVATE KEY ----- ----- the BEGIN" value is PHP_EOL .wordwrap ($ PrivateKey, 64, "\ n-", to true) value is PHP_EOL "the RSA PRIVATE KEY ----- ----- the END";..

$ prikeyid = openssl_get_privatekey ($ PrivateKey);
IF (openssl_private_decrypt (crypttext $, $ sourcestr, $ prikeyid,




}


/ **
* private key encryption
* /
function privatekey_encodeing ($ sourcestr, $ PrivateKey)
{

// To initialize the private key, warranties, whether formatted or unformatted when you fill out the private key can be verified.
str_replace PrivateKey = $ ( "the RSA PRIVATE KEY ----- ----- the BEGIN", "", $ PrivateKey);
$ PrivateKey = str_replace ( "the RSA PRIVATE KEY ----- ----- the END" , "", $ PrivateKey);
$ PrivateKey = str_replace ( "\ n-", "", $ PrivateKey);

. PrivateKey $ = "the RSA PRIVATE KEY ----- ----- the BEGIN" value is PHP_EOL .wordwrap ($ PrivateKey, 64, "\ n-", to true) value is PHP_EOL "the RSA PRIVATE KEY ----- ----- the END";..
$ prikeyid = openssl_get_privatekey ($ PrivateKey);
IF (openssl_private_encrypt (sourcestr $, $ crypttext, $ prikeyid,



return FALSE;
}


/ ** RSA signature 
* $ data data to be signed
* $ priKey private businesses
* signed with the private key of the merchant
* use MD5 digest algorithm
* last signature needed base64 encoding
* return Sign signature
* /
function Rsasign (the Data $, $ prikey) {
// convert openssl key
$ openssl_get_privatekey RES = ($ prikey);

// call to the built openssl signature method, generates the signature Sign $
openssl_sign ($ Data, Sign $, $ RES, OPENSSL_ALGO_MD5);

// release resources
openssl_free_key ($ RES);

// Base64 encoding
$ = sign the base64_encode ($ sign);
return $ sign;
}


/ ** check the RSA test
* $ data data to be signed
* $ sign needs to check a signature test
* $ pubKey public key
* return check whether the test value by bool
* /
function Rsaverify ($ Data, sign $, $ pubkey) {
// Key format is converted to openssl
$ openssl_get_publickey RES = ($ pubkey);

// call the built-in methods openssl been stamped return bool value
$ result = (bool) openssl_verify ( $ data, base64_decode ($ sign), $ res, OPENSSL_ALGO_MD5 );

// release resources
openssl_free_key ($ RES);

// returns whether successful resource
return $ the Result;
}





Guess you like

Origin www.cnblogs.com/shuniuniu/p/11326463.html