"Java Security Technology Exploration Path series: Java Extensible security Architecture" IV: JCA (III): JCA programming model

Source: Internet
Author: User

Guo Jia
Email: [Email protected]
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwell

A message summary

To calculate a message digest using MD5

        try        {            MessageDigest md5 = MessageDigest.getInstance("MD5");            byte12345 };            md5.update(testdata);            byte[] myhash = md5.digest();        }        catch (NoSuchAlgorithmException e)        {        }

To calculate a message digest using SHA-1

        try        {            MessageDigest sha = MessageDigest.getInstance("SHA-1");            byte12345 };            sha.update(testdata);            byte[] myhash = sha.digest();        }        catch (NoSuchAlgorithmException e)        {        }
Generation of two key pairs

The key is represented by an interface Java.security.Key, which provides 3 methods:

    • getalgorithm () returns the key algorithm;
    • getencoded () returns the key that is returned in the original encoded format as a byte array;
    • GetFormat () returns the encoded format of the key.

DSA algorithm and DH algorithm generate public private key pair

    Try{//10,241 bit Digital Signature algorithm (DSA) key pairsKeypairgenerator KeyGen = keypairgenerator.getinstance ("DSA"); Keygen.initialize (1024x768);            KeyPair KeyPair = Keygen.genkeypair ();            Privatekey Privatekey = Keypair.getprivate (); PublicKey PublicKey = Keypair.getpublic ();//576-bit Diffiehellman key pairKeyGen = Keypairgenerator.getinstance ("DH"); Keygen.initialize (576);            KeyPair = Keygen.genkeypair ();            Privatekey = Keypair.getprivate ();        PublicKey = Keypair.getpublic (); }Catch(Java.security.NoSuchAlgorithmException e) {        }
Generation of three digital signatures

Digital signature technology is generated using public key cryptography, the sender uses the private key to sign the message, and the receiver decrypts the message with the public key, so that the receiver can verify the source or signer of the message, thus ensuring the integrity and authenticity of the message.

Private key Signature

            try            {                byte12345 };                Signature dsig = Signature.getInstance(privateKey                        .getAlgorithm());                dsig.initSign(privateKey);                dsig.update(testdata);                byte[] signedData = dsig.sign();            }            catch (SignatureException e)            {            }            catch (InvalidKeyException e)            {            }            catch (NoSuchAlgorithmException e)            {            }

Public key validation

            try            {            Signature publicDsig =     Signature.getInstance(publicKey.getAlgorithm());            publicDsig.initVerify(publicKey);            publicDsig.update(signedData);            boolean result = publicDsig.verify(signatureToVerify);            }            catch(SignatureException e)            {            }            catch (InvalidKeyException e)            {            }            catch (NoSuchAlgorithmException e)            {            }

"Java Security Technology Exploration Path series: Java Extensible security Architecture" IV: JCA (III): JCA programming model

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.