Java Translation Lua+c+openssl Signature Project

Source: Internet
Author: User
Tags base64 ffi lua sin

The original project with Openresty Nginx+lua implementation Server,lua call C dynamic link library, to use OPENSS to do the signature, and generate 130 bytes (128 signature + 2 bit custom bytes) length of the file.

nginx:location/Get/Key {Content_by_lua_file'/data/www/sign.lua'; }sign.lualocal FFI= Require"FFI"--Dynamic Link gen_sig_ex_x.c,load ("") Name rules, lib***. solocal GS= Ffi.load ("Sin") ffi.cdef[[intGen_main (Char*param,unsignedChar*signature)]] Local PARAM_= Ngx.var. Arg_paramifPARAM_ then local signature= Ffi.New("unsigned char[130]", {}) local CPSN= Ffi.New("Char[20]", PARAM_) gs.gen_main (PARAM_, signature) ngx.header["content-disposition"] ="attachment; Filename="..string. Format ("%s.%d.key", PSN, key) ngx.header["Content-length"] = theNgx.say (FFI).string(Signature, the))ElseNgx.header.content_type="text/html"Ngx.say ("The param is not empty") End

For sin.so with C and OpenSSL to implement the private key signature, and use the RSA structure key, in order to get PKCS8, in the program through the following code to reverse the format of the private key. And get the public key by command.


Openssl> genrsa-out Rsa_private_key.pem 1024x768 #生成私钥
openssl> pkcs8-topk8-inform pem-in rsa_private_key.pem-outform pem-nocrypt-out Rsa_private_key_pkcs8.pem #Java开发者 Need to convert private key to PKCS8 format
openssl> rsa-in rsa_private_key.pem-pubout-out Rsa_public_key.pem #生成公钥
Openssl> Exit #退出OpenSSL程序

  //build the Key object from the RSA structure private key and get the private key base64:FILE *filename =NULL; FileName= fopen ("/data/www/unlock.lua/privatekey.pem","WB"); //generating the private key interfacePem_write_rsaprivatekey (filename, key, NULL, NULL,0, NULL, NULL);    fclose (filename); unsignedChar*n_b = (unsignedChar*)calloc(Rsa_size (Key),sizeof(unsignedChar)); unsignedChar*e_b = (unsignedChar*)calloc(Rsa_size (Key),sizeof(unsignedChar)); intN_size = Bn_bn2bin (key->N, N_b); intB_size = Bn_bn2bin (key->e, e_b); RSA*pubrsa =rsa_new (); Pubrsa->n =bn_bin2bn (N_b, N_size, NULL); Pubrsa->e =bn_bin2bn (E_b, B_size, NULL); FILE*publickey =NULL; PublicKey= fopen ("/data/www/unlock.lua/publickey.pem","WB");    Pem_write_rsapublickey (PublicKey, Pubrsa);    Fclose (PublicKey); Rsa_free (PUBRSA);
====Java Implementation Signature Core code: PackageCom.smartisan.genkey_sig.util;Importorg.apache.commons.codec.binary.Base64;Importjava.nio.charset.StandardCharsets;Importjava.security.KeyFactory;ImportJava.security.PrivateKey;Importjava.security.Signature;ImportJava.security.spec.PKCS8EncodedKeySpec;/*** Desc: * *@author[Email protected]*********.com *@since2017/8/11*/ Public classRsautil { Public Static FinalString key_algorithm = "RSA";  Public Static FinalString signature_algorithm = "Md5withrsa";  Public Static byte[] Sign (byte[] data, String Privatekey) {        Try {            byte[] Keybytes =base64.decodebase64 ((Privatekey)); Pkcs8encodedkeyspec Pkcs8keyspec=NewPkcs8encodedkeyspec (keybytes); //Key_algorithm The specified encryption algorithmKeyfactory keyfactory =keyfactory.getinstance (Key_algorithm); //take private Key ObjectPrivatekey Prikey =keyfactory.generateprivate (PKCS8KEYSPEC); //Generating digital signatures for information with the private keySignature Signature =signature.getinstance (Signature_algorithm);            Signature.initsign (Prikey);            Signature.update (data); returnsignature.sign (); } Catch(Exception ex) {Throw NewRuntimeException (ex); }    }     Public Static byte[] Sign (string data, String privatekey) {returnSign (Data.getbytes (standardcharsets.utf_8), Privatekey); }}

Java Translation Lua+c+openssl Signature Project

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.