13 kinds of encryption and decryption algorithms [four]

[10] San Lie Haxi the SHA1 encryption

SHA1 (English: Secure Hash Algorithm 1, Chinese name: Secure Hash Algorithm 1) is a cryptographic hash function, the National Security Agency designed by the American National Standards Institute (NIST) published as Federal Data Processing Standard ( FIPS).
SHA-1 can generate 160 (20 byte) hash value is referred to as a message digest, a hash value is typically presented in the form of 40 hexadecimal numbers.
** [SHA-1 and SHA-0]
SHA-1 and SHA-0 algorithm only partially compressed message conversion function of the difference between the cyclic shift of one bit.

[SHA1 hash of]

 /**
         * SHA1
         * @param inStr 需要摘要的内容
         * @return
         */
        public static String sha1Encode(String inStr) {
                MessageDigest sha = null;
                try {
                        sha = MessageDigest.getInstance("SHA");
                        byte[] byteArray = inStr.getBytes("UTF-8");
                        byte[] md5Bytes = sha.digest(byteArray);
                        StringBuffer hexValue = new StringBuffer();
                        for (int i = 0; i < md5Bytes.length; i++) {
                                int val = ((int) md5Bytes[i]) & 0xff;
                                if (val < 16) {
                                        hexValue.append("0");
                                }
                                hexValue.append(Integer.toHexString(val));
                        }
                        return hexValue.toString();
                } catch (Exception e) {
                        System.out.println(e.toString());
                        e.printStackTrace();
                        return "";
                }
        }

[SHA1 hash of small demo]
System.out.println ( "SHA1 hash of the summary:" + sha1Encode (str)) ;

13 kinds of encryption and decryption algorithms [four]

[PS: SHA1 with MD5, is irreversible, can be used as the article summary judgment as to identify]

[11] hash of the encrypted CRC32

[Principle]
CRC test principle is actually attached a r-bit binary check code (sequence) After a p-bit binary data sequence, so as to constitute a total length p + r-bit binary sequence is n =; appended to the data sequence of the check codes between the content data sequence and there is a particular relationship. If the interference due to reasons such as the one bit or some bit data sequence error occurs, this particular relationship will be destroyed. Therefore, by examining this relationship, we can achieve a test of the correctness of the data.
[Nature] CRC
is the remainder of the modulo-2 division, different divisors used, the type of CRC is not the same. Typically, CRC divisor represented by generating polynomial. The most common CRC code generator polynomial shown in Table 1. The most commonly used CRC code generator polynomial and the generator polynomial Name
13 kinds of encryption and decryption algorithms [four]

[CRC32 hash of small demo]
[PS: hash encryption process irreversible, take a piece of text as an encrypted content does not make sense, here use as a content file]

13 kinds of encryption and decryption algorithms [four]

[CRC32 hash of encryption]

 /**
         * 使用CheckedInputStream计算CRC
         */
        public static Long getCRC32(String filepath) {
                try {
                        CRC32 crc32 = new CRC32();
                        FileInputStream fileinputstream = new FileInputStream(new File(filepath));
                        CheckedInputStream checkedinputstream = new CheckedInputStream(fileinputstream, crc32);
                        while (checkedinputstream.read() != -1) {
                        }
                        checkedinputstream.close();
                        return crc32.getValue();
                }catch (Exception e){
                        e.printStackTrace();
                        return null;
                }
        }
        /**
         * 采用BufferedInputStream的方式加载文件
         */
        public static long bufferedInputStream(String filepath) {
                try {
                        InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath));
                        CRC32 crc = new CRC32();
                        byte[] bytes = new byte[1024];
                        int cnt;
                        while ((cnt = inputStream.read(bytes)) != -1) {
                                crc.update(bytes, 0, cnt);
                        }
                        inputStream.close();
                        return crc.getValue();
                }catch (Exception e){
                        e.printStackTrace();
                        return 0;
                }
        }

[Test] small demo

 String path = "D:\\huatu.eapx";
 Long ll = getCRC32(path);
 System.out.println("使用CheckedInputStream计算CRC得到:" + ll);
 Long ll1 = bufferedInputStream(path);
 System.out.println("采用BufferedInputStream计算CRC得到:"+ll1);
![](https://s4.51cto.com/images/blog/202004/04/0cbe6d676f3c829013d8bb2ef1d82670.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

13 kinds of encryption and decryption algorithms [four]

【12、Rabbit】

Rabbit stream cipher, the key length 128,

The maximum length of the encrypted message 264 Bytes, i.e. 16 TB, over the length of the message if you need to replace the rest of the key message is processed. It is one of the high security encryption / decryption speed is relatively efficient stream cipher, in a variety of processor platforms have extraordinary performance.
[Test] small demo
slightly 1

【13、Escape】

In many applications scripting language which, escape function is a conversion function code.
Such transfer parameters in javascript? DeptName = test, can be first "test" with the escape re-encoding, and then transmitting, decoding will not garbled reception at the server before.
URL parameter for transmitting escape general urlencode base64_encode and similar functions are similar. For details, see "Web project in common coding"

Guess you like

Origin blog.51cto.com/13479739/2484886