The Java encryption and decryption technology series SHA

Source: Internet
Author: User
Tags md5 digest

The basic one-way encryption algorithm--MD5 is described 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. Background 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. After the text has understood the background, we begin 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:
    • You cannot restore information from a message digest
    • Two different messages that do not produce the same message digest
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. The comparison between 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:
    • Security for brute force attacks
The most significant and important difference is that the SHA-1 digest is 32 bits longer than the MD5 digest. Using the brute force technique, generating any message to make its digest equal to the difficulty of a given report digest is a 2^128 order of magnitude, while for SHA-1 it is a 2^160 order of magnitude of operation. MD5 In this way, the SHA-1 has greater strength for brute force attacks.
    • Security for password analysis
Because of the MD5 design, vulnerable to password analysis attacks, SHA-1 appears to be vulnerable to such attacks.
    • Speed
On the same hardware, the SHA-1 runs slower than MD5. Code implementation [Java]View PlainCopy 
  1. <span style="font-family:comic Sans ms;font-size:12px;"  > PackageCom.sica.sha;
  2. Import com.google.common.base.Strings;
  3. Import Java.security.MessageDigest;
  4. /**
  5. * Created by Xiang.li on 2015/2/11.
  6. */
  7. Public class SHA {
  8. /** 
  9. * Define Encryption method
  10. */
  11. Private final static String Key_sha = "SHA";
  12. Private final static String key_sha1 = "SHA-1";
  13. /** 
  14. * Global Array
  15. */
  16. Private final static string[] Hexdigits = { "0", "1", "2", "3", "4", "5",
  17. "6", "7", "8", "9", "a", "B", "C", "D", "E", "f"};
  18. /** 
  19. * Constructor function
  20. */
  21. Public SHA () {
  22. }
  23. /** 
  24. * SHA Encryption
  25. * @param data requires an encrypted byte array
  26. * @return byte array after encryption
  27. * @throws Exception
  28. */
  29. public static byte[] Encryptsha (byte[] data) throws Exception {
  30. //Create a summary of information with the specified algorithm name
  31. MessageDigest sha = Messagedigest.getinstance (Key_sha);
  32. MessageDigest sha = Messagedigest.getinstance (KEY_SHA1);
  33. //Use the specified byte array for the last update of the Digest
  34. Sha.update (data);
  35. //Complete summary calculation and return
  36. return Sha.digest ();
  37. }
  38. /** 
  39. * SHA Encryption
  40. * @param data requires an encrypted string
  41. * @return The string after encryption
  42. * @throws Exception
  43. */
  44. public static string Encryptsha (string data) throws Exception {
  45. //Verify the passed-in string
  46. if (strings.isnullorempty (data)) {
  47. return "";
  48. }
  49. //Create a summary of information with the specified algorithm name
  50. MessageDigest sha = Messagedigest.getinstance (Key_sha);
  51. //Use the specified byte array for the last update of the Digest
  52. Sha.update (Data.getbytes ());
  53. //Complete Summary calculation
  54. byte[] bytes = Sha.digest ();
  55. //The resulting byte array becomes a string return
  56. return bytearraytohexstring (bytes);
  57. }
  58. /** 
  59. * Converts a byte into a string of 16 binary form
  60. * @param B-byte array
  61. * @return String
  62. */
  63. private static String bytetohexstring (byte b) {
  64. int ret = b;
  65. //system.out.println ("ret =" + ret);
  66. if (Ret < 0) {
  67. RET + = 256;
  68. }
  69. int m = ret/ 16;
  70. int n = ret% 16;
  71. return hexdigits[m] + hexdigits[n];
  72. }
  73. /** 
  74. * Convert byte array to hexadecimal string
  75. * @param bytes byte array
  76. * @return Hexadecimal string
  77. */
  78. private static String bytearraytohexstring (byte[] bytes) {
  79. StringBuffer sb = new StringBuffer ();
  80. For (int i = 0; i < bytes.length; i++) {
  81. Sb.append (bytetohexstring (bytes[i));
  82. }
  83. return sb.tostring ();
  84. }
  85. /** 
  86. * Test method
  87. * @param args
  88. */
  89. public static void Main (string[] args) throws Exception {
  90. String key = "123";
  91. System.out.println (Encryptsha (key));
  92. }
  93. }</span>
Concluding remarks See this, I think the simple principle of SHA-1 you should be understood, 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.

The Java encryption and decryption technology series SHA

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.