3.Java encryption and decryption technology series of SHA

Source: Internet
Author: User

The Java encryption and decryption technology series SHA

    • Order
    • Background
    • Body
    • Comparison of SHA-1 and MD5
    • Code implementation
    • Conclusion

The basic one-way encryption algorithm--MD5 is introduced in the previous article, and the principle of its realization is also roughly stated. This article continues the one-way encryption mentioned earlier, mainly on Sha, like MD5, Sha is also a series, which includes several algorithms such as sha-1,sha-224,sha-256,sha-384, and SHA-512. Among them, sha-1,sha-224 and SHA-256 apply to messages that do not exceed 2^64 bits in length. SHA-384 and SHA-512 are suitable for messages that do not exceed 2^128 bits in length.

Before starting the text, simply say the background. At first blush you may not know, but when it comes to hashing and hashing algorithms, you will know, that is, the usual meaning of the hash. So, first look at what a hash is. Hashing, is the refinement of information, usually its length is much smaller than the information, and is a fixed length. A cryptographically strong hash must be irreversible, which means that no part of the original information can be rolled out by hashing the result. It is clear that the result of the hash is irreversible, and the original information cannot be released according to the hash result.

Having understood the background, we began to introduce SHA.
SHA, all known as "secure Hash Algorithm", the Chinese name "Secure Hash Algorithm", is mainly applicable to the digital Signature Algorithm (digital Signature standard DSS), which is defined in the digitally Signature Algorithm DSA). For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest.
The idea of the algorithm is to receive a 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 short length, fixed number of bits of the output sequence is the process of hashing values.
Also mentioned above, SHA stipulates a number of algorithms, including sha-1,sha-224,sha-256, and many other kinds. Here I take SHA-1 as an example, tell me how SHA-1 works.
There are two features of SHA-1:
      SHA-1 is a data encryption algorithm, mainly to receive a piece of plaintext, and then convert it into a cipher in an irreversible way, or simply to take a string of input code, and convert them to a short length, fixed number of bits of output sequence is the process of hashing values.

The security of one-way hash function is that its operation process of generating hash value has a strong unidirectional nature. If the password is embedded in the input sequence, then no one can produce the correct hash value without knowing the password, thus guaranteeing its security. SHA blocks the input stream by 512 bits per block (64 bytes) and produces 20 bytes of output called the Information authentication Code or information digest.
The input message length of the algorithm is unlimited, the output is a 160-bit message digest. The input is processed in 512-bit groupings. SHA-1 is irreversible, conflict-proof and has a good avalanche effect.
The digital signature is realized by hashing algorithm, the principle of the digital signature is to transfer the plaintext through a function operation (Hash) to the report digest (different clear text corresponding to different message digest), the digest to be encrypted and sent to the receiver with the clear text, The receiving party will accept the clear text generated by the new digest to be decrypted with the sender of the digest to decrypt the comparison, the comparison results uniformly indicate that the plaintext has not been altered, if inconsistent, indicating that the plaintext has been tampered with.

Comparison of SHA-1 and MD5
Because both are exported by MD4, SHA-1 and MD5 are very similar to each other. Correspondingly, their strength and other characteristics are similar, but there are several differences:

      because of the MD5 design, vulnerable to password analysis attacks, SHA-1 appears to be vulnerable to such attacks.

      on the same hardware, the SHA-1 runs slower than MD5.

Code implementation
import com.google.common.base.Strings;
import java.security.MessageDigest;
* *
* Created by xiang.li on 2015/2/11.
* /
public class SHA {
* *
*Define encryption method
* /
private final static String KEY_SHA = "SHA";
private final static String KEY_SHA1 = "SHA-1";
* *
*Global array
* /
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
* *
* /
public SHA() {
* *
* SHA encryption
*@ param data byte array to be encrypted
*@ return encrypted byte array
* @throws Exception
* /
public static byte[] encryptSHA(byte[] data) throws Exception {
//Create a summary of information with the specified algorithm name
//        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
MessageDigest sha = MessageDigest.getInstance(KEY_SHA1);
//Last update summary with specified byte array
//Complete summary calculation and return to
return sha.digest();
* *
* SHA encryption
*@ param data needs encrypted string
*@ return encrypted string
* @throws Exception
* /
public static String encryptSHA(String data) throws Exception {
//Validate incoming string
if (Strings.isNullOrEmpty(data)) {
Return "";
//Create a summary of information with the specified algorithm name
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
//Last update summary with specified byte array
//Complete summary calculation
byte[] bytes = sha.digest();
//Turn the resulting byte array into a string and return
return byteArrayToHexString(bytes);
* *
*Convert a byte to a string in hexadecimal form
*@ param B byte array
*@ return string
* /
private static String byteToHexString(byte b) {
Int RET = B;
//System.out.println("ret = " + ret);
if (ret < 0) {
RET + = 256;
int m = ret / 16;
int n = ret % 16;
return hexDigits[m] + hexDigits[n];
* *
*Convert byte array to hexadecimal string
*@ param bytes byte array
*@ return hexadecimal string
* /
private static String byteArrayToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
return sb.toString();
* *
*Test method
* @param args
* /
public static void main(String[] args) throws Exception {
String key = "123";

See this, I think SHA-1 simple principle you should be aware of, and, for the application is not difficult, you can refer to the above Java code. Back to think MD5, through the above article, you can know, in fact, SHA-1 and MD5 is expatiating, but their respective implementation of different ways, SHA-1 in the number of operations is more complex than MD5, therefore, for security considerations, SHA-1 is relatively reliable.
As to when it will be used, it is necessary to consider the characteristics of SHA-1. Very clear, irreversible, and unique. Well, I think that the encryption applied to MD5 also applies to SHA-1. Moreover, in terms of security, SHA-1 more than MD5, if the speed is strict, then, or priority to consider MD5 it.

3.Java encryption and decryption technology series of SHA

Related Article

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.