Example of the rsa encryption/Decryption class library-php & python

Source: Internet
Author: User
Create an object in the early stage and write a php rsa help class library. first look at the usage method $ keysRsaHelper: new_rsa_key. After the key is generated, record the key value. $ privkey $ keysprivkey is omitted here; $ pubkey $ keyspubkey; * $ privkey ----- BEGINPRIVATEKEY ----- MIICdwIBADANBgkqhkiG9w0BA

Create an object early and write a php rsa help class library. first look at the usage method $ keys = RsaHelper: new_rsa_key; // record the key value after the key is generated, $ privkey = $ keys 'privkey'; $ pubkey = $ keys 'pubkey';/* $ privkey = '----- begin private key ----- MIICdwIBADANBgkqhkiG9w0BA

Create an object early, write a php rsa help class library, first look at the usage method

$ Keys = RsaHelper: new_rsa_key (); // record the key value after the key is generated. $ privkey = $ keys ['privkey'] is omitted here; $ pubkey = $ keys ['pubkey']; /* $ privkey = '----- begin private key ----- encrypt/decrypt + VpfKVX6DEagoRBZvtz + encrypt/decrypt/ROjChC9utrZWby6E + nuTwd3c0QA4Bp49 +/decrypt + encrypt/decrypt/ HMpY // 0ucpjfyxa32ipkpg%z + %+ %/ 6992Y0DSMaHt50peI6uJoMV + %= ----- end private key -----'; $ pubkey = '----- begin public key ----- BEGIN + gxGoKEQWb7c/users + mpD + 0TJuFs6ZzL3A6v/users ----- end public key -----'; * // initialize rsaobjectRsaHelper: init ($ privkey, $ pubkey); // original $ data = 'hello'; // Private Key Encryption example $ encode = RsaHelper: priv_encode ($ data); $ ret = RsaHelper :: pub_decode ($ encode); var_dump ($ ret); // example of public key encryption $ encode = RsaHelper: pub_encode ($ data); $ ret = RsaHelper :: priv_decode ($ encode); var_dump ($ ret );

The source code of the class library is as follows:

Class RsaHelper {private static $ _ privkey = ''; private static $ _ pubkey =''; private static $ _ isbase64 = false; /*** initialize the key value * @ param string $ privkey private key * @ param string $ pubkey public key * @ param boolean $ isbase64 base64 encoded? * @ return null */public static function init ($ privkey, $ pubkey, $ isbase64 = false) {self ::$ _ privkey = $ privkey; self ::$ _ pubkey = $ pubkey; self :$ _ isbase64 = $ isbase64 ;} /*** private key encryption ** @ param string $ data original text * @ return string ciphertext */public static function priv_encode ($ data) {$ outval = ''; $ res = openssl_pkey_get_private (self: $ _ privkey); openssl_private_encrypt ($ data, $ outval, $ res); if (self: $ _ isbase64) {$ outval = base64_encode ($ outval);} return $ outval ;} /*** public Key decryption * @ param string $ data ciphertext * @ return string original text */public static function pub_decode ($ data) {$ outval = ''; if (self :: $ _ isbase64) {$ data = base64_decode ($ data);} $ res = openssl_pkey_get_public (self ::$ _ pubkey); openssl_public_decrypt ($ data, $ outval, $ res ); return $ outval;}/*** public key encryption * @ param string $ data original * @ return string ciphertext */public static function pub_encode ($ data) {$ outval = ''; $ res = openssl_pkey_get_public (self: $ _ pubkey); openssl_public_encrypt ($ data, $ outval, $ res); if (self: $ _ isbase64) {$ outval = base64_encode ($ outval);} return $ outval ;} /*** Private Key decryption * @ param string $ data ciphertext * @ return string original text */public static function priv_decode ($ data) {$ outval = ''; if (self:: $ _ isbase64) {$ data = base64_decode ($ data);} $ res = openssl_pkey_get_private (self ::$ _ privkey); openssl_private_decrypt ($ data, $ outval, $ res); return $ outval;}/*** create a set of public key private keys * @ return array public Key Private Key array */public static function new_rsa_key () {$ res = openssl_pkey_new (); openssl_pkey_export ($ res, $ privkey); $ d = openssl_pkey_get_details ($ res); $ pubkey = $ d ['key']; return array ('privkey' =>$ privkey, 'pubkey' =>$ pubkey );}}

Mom, the php protobuf under the win platform is tossing me and changing it to py. So, the python version of the rsa encryption helper class is coming.

def new_keys():    (bob_pub, bob_priv) = rsa.newkeys(1024)    return {'pubkey':bob_pub.save_pkcs1(), 'privkey':bob_priv.save_pkcs1()} class RsaHelper:     def __init__(self, privkey, pubkey):        self._privkey = privkey.decode('string-escape')        self._pubkey = pubkey.decode('string-escape')     def encode_priv(self, data):        privkey = rsa.PrivateKey.load_pkcs1(self._privkey)        crypto = rsa.encrypt(data, privkey)        return crypto     def decode_pub(self, data):        pubkey = rsa.PublicKey.load_pkcs1(self._pubkey)        msg = rsa.decrypt(data, pubkey)        return msg     def decode_priv(self, data):        privkey = rsa.PrivateKey.load_pkcs1(self._privkey)        msg = rsa.decrypt(data, privkey)        return msg     def encode_pub(self, data):        pubkey = rsa.PublicKey.load_pkcs1(self._pubkey)        crypto = rsa.encrypt(data, pubkey)        return crypto

Original article address: Example of the rsa encryption/Decryption class library-php & python. Thank you for sharing it with me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.