Java encryption--abstract algorithm

Source: Internet
Author: User

MD5

    • Overview

MD5 is a message Digest algorithm 5 (Information Digest algorithm 5) to ensure complete and consistent information transmission. is one of the widely used hashing algorithms of computers (also known as digest algorithm, hash algorithm ), mainstream programming language has been widely MD5 implemented.

    • the MD5 algorithm has the following characteristics :

1, compressibility: Any length of data, calculated the length of the MD5 value is fixed.

2, easy to calculate: It is easy to calculate the MD5 value from the original data.

3, anti-modification: Any changes to the original data, even if only 1 bytes modified, the resulting MD5 value is very different.

4, weak anti-collision: known raw data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, falsification of data).

5, strong anti-collision: To find two different data, so that they have the same MD5 value, is very difficult

    • Principle

the brief description of the MD5 algorithm can be: MD5 with 512-bit grouping to process the input information, and each grouping is divided into 16 32-bit sub-groups, after a series of processing, the output of the algorithm is composed of four 32-bit groupings, the four 32-bit grouping cascade will generate a 128-bit hash value.

    • Instance Code

public static byte[] Encrypt (String str) throws NoSuchAlgorithmException {//MD5 object MessageDigest messagedigest based on MD5 algorithm  = Messagedigest.getinstance ("MD5");  byte[] Srcdata = Str.getbytes ();  Use Srcbytes Update summary md5.update (SRCDATA); Complete hash calculation, get result return md5.digest (); }

SHA

    • Overview

Secure Hash algorithm is primarily applicable to digital signature algorithms (digitally Signature standard DSS) defined in digital Signature Al Gorithm DSA). For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest.

    • Principle

SHA-1 is a Data Encryption algorithm, the idea of the algorithm is to receive a piece of clear text, and then in an irreversible way to convert it into a paragraph (usually smaller) ciphertext, can also be easily understood as a string of input code (called Pre-mapping or information), and convert them to a shorter length, A fixed number of bits of output sequence is the process of hashing values (also known as information digests or information authentication codes).

    • Instance Code

public static byte[] Encrypt (String str) throws NoSuchAlgorithmException {//MessageDigest object generated from Sha algorithm MessageDigest sha    = Messagedigest.getinstance ("SHA");  byte[] Srcdata = Str.getbytes ();  Use Srcbytes Update summary sha.update (SRCDATA); Complete hash calculation, get result return sha.digest (); }

Java encryption--abstract algorithm

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.