The classic digital Signature Algorithm-rsa

Source: Internet
Author: User

Records:

Public Abstract classRsacodersignature {Private Static FinalString Private_key = "Rsaprivate_key"; Private Static FinalString Public_key = "Rsapublic_key"; Private Static Final intKey_size = 512; Private Static FinalString key_algorithm = "RSA"; Private Static FinalString signature_algorithm = "Md5withrsa"; Public Static byte[] Getprivatekey (map<string, object> keyMap)throwsException {key key=(Key) keymap.get (Private_key); returnkey.getencoded (); } Public Static byte[] Getpublickey (map<string, object> keyMap)throwsException {key key=(Key) keymap.get (Public_key); returnkey.getencoded (); } /*** Production Public private key is saved to map *@return * @throwsException*/ Public StaticMap<string, object> Initkey ()throwsException {Map<string, object> KeyMap =NewHashmap<string, object> (2); Keypairgenerator Keypaigen=keypairgenerator.getinstance (Key_algorithm); Keypaigen.initialize (key_size); KeyPair pair=Keypaigen.generatekeypair (); Rsapublickey PublicKey=(Rsapublickey) pair.getpublic (); Rsaprivatekey Privatekey=(Rsaprivatekey) pair.getprivate (); Keymap.put (Public_key, PublicKey); Keymap.put (Private_key, Privatekey); returnKeyMap; } /*** Digital signature production with private key, *@paramData *@paramPrivatekey *@return * @throwsException*/ Public Static byte[] Sign (byte[] Data,byte[] privatekey)throwsException {pkcs8encodedkeyspec PKC=NewPkcs8encodedkeyspec (Privatekey); Keyfactory keyfactory=keyfactory.getinstance (Key_algorithm); Privatekey Prikey=keyfactory.generateprivate (PKC); Signature Sig=signature.getinstance (Signature_algorithm); Sig.initsign (Prikey); Sig.update (data); returnsig.sign (); } /*** Verify the correctness of the private key encryption with the public key decryption, *@paramData *@paramPublicKey *@paramSigns encryption digital certificate for private key production *@return * @throwsException*/ Public Static BooleanVerifybyte[] Data,byte[] PublicKey,byte[] signs)throwsException {x509encodedkeyspec PKC=NewX509encodedkeyspec (PublicKey); Keyfactory keyfactory=keyfactory.getinstance (Key_algorithm); PublicKey PubKey=keyfactory.generatepublic (PKC); Signature Sig=signature.getinstance (Signature_algorithm); Sig.initverify (PubKey); Sig.update (data); returnsig.verify (signs); }}

The classic digital Signature Algorithm-rsa

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.