RSA Signature Verification

Source: Internet
Author: User

Importandroid.util.Base64;Importjava.security.KeyFactory;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;ImportJava.security.spec.PKCS8EncodedKeySpec;ImportJava.security.spec.X509EncodedKeySpec;/*** Author:jsonlu * DATETIME:2016/6/23 19:50 * email:[email protected] * DESC:RSA Signature Verification Tool class **/ Public classRsautil {Private Static FinalString sign_algorithms = "Sha1withrsa"; //private key PKCS8 format    Private Static FinalString Sumpay_payment_private_key = ""; Private Static FinalString Sumpay_payment_server_public_key = ""; /*** RSA Signature * *@paramcontent pending Signature data *@paraminput_charset encoded format *@returnSignature Value*/     Public Staticstring sign (string content, String input_charset) {Try{pkcs8encodedkeyspec priPKCS8=NewPkcs8encodedkeyspec (Base64.decode (Sumpay_payment_private_key, Base64.default)); Keyfactory Keyf= Keyfactory.getinstance ("RSA"); Privatekey Prikey=keyf.generateprivate (priPKCS8); Java.security.Signature Signature=java.security.Signature.getInstance (SIGN_ALGORITHMS);            Signature.initsign (Prikey);            Signature.update (Content.getbytes (Input_charset)); byte[] Signed =signature.sign (); returnbase64.encodetostring (signed, Base64.default); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }    /*** RSA Verification Signature Check * *@paramcontent pending Signature data *@paramSign Signature value *@paraminput_charset encoded format *@returnBoolean value*/     Public Static BooleanVerify (string content, string sign, string input_charset) {Try{keyfactory keyfactory= Keyfactory.getinstance ("RSA"); byte[] Encodedkey =Base64.decode (Sumpay_payment_server_public_key, Base64.default); PublicKey PubKey= Keyfactory.generatepublic (NewX509encodedkeyspec (Encodedkey)); Java.security.Signature Signature=java.security.Signature.getInstance (SIGN_ALGORITHMS);            Signature.initverify (PubKey);            Signature.update (Content.getbytes (Input_charset)); BooleanBverify =signature.verify (Base64.decode (sign, base64.default)); returnbverify; } Catch(Exception e) {e.printstacktrace (); }        return false; }}

RSA Signature Verification

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.