Examples of HMACMD5 encryption algorithms implemented by Python _python

Source: Internet
Author: User
Tags hmac md5 encryption md5 hash
This article mainly introduced the Python implementation of the HMACMD5 encryption algorithm, simply explained the concept of HMAC-MD5 encryption algorithm, the principle and combined with the case form analysis of Python implementation of HMAC-MD5 encryption algorithm related operation skills, The end also comes with Java implementation HMAC-MD5 encryption algorithm example, the need for friends can refer to the following





In this paper, the HMACMD5 encryption algorithm implemented by Python is described. Share to everyone for your reference, as follows:



What is HMAC-MD5?



1, such as you and each other to share a key K, now you want to send a message to each other, both to ensure that the message has not been tampered with, but also to prove that the information is indeed your own hair, then the original information and the use of the value of the HMAC calculated with the values sent past. After the other party receives, using their own k to calculate the message HMAC, if and you send the HMAC consistent, then you can think that the message has not been tampered with and not impersonating.



2, MD5 is through the hash of the data to be output summary, received data, and then the same MD5 hash, and the given MD5 hash value comparison, consistent inconsistency is very clear. Generally speaking, the transmitted data and MD5 are given by different channels, such as the display of MD5 on the webpage, the download link is a mirror site. If you want to send data and hash values through the same channel (such as Message authentication code), it is necessary to consider the problem that the data and MD5 are simultaneously tampered with, if the third party modifies the data and then MD5 the hash, and sends it to the receiver, the receiver does not perceive that the data has been tampered with. The HMAC-MD5 can be computed with a key from both the sender and receiver, and the third party without this key cannot calculate the correct hash value, which prevents the data from being tampered with.



Python version:





#coding: utf-8
import sys
reload (sys)
sys.setdefaultencoding ('utf-8')
import hmac
import hashlib
###################### Set Key Value ##############
ekey = 'laidefa'
###############Input data############
to_enc = '{"name": "zhangsan"}'
enc_res = hmac.new (ekey, to_enc, hashlib.md5) .hexdigest ()
print enc_res





Output Result:


"D:\Program Files\python27\python.exe" d:/pycharmprojects/learn2017/hmacmd5.py
2cbb94ce78b35e4030851c4d40dacf12
Process finished with exit code 0


Java Edition:





package tom;
import java.security.MessageDigest;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/ **
* Basic encryption components
* @version 1.0
* /
public class Hmacmd5 {
  / **
   * MAC algorithm can choose from the following algorithms
   *
   * <pre>
   * HmacMD5
   * HmacSHA1
   * HmacSHA256
   * HmacSHA384
   * HmacSHA512
   * </ pre>
   * /
  public static final String KEY_MAC = "HmacMD5";
  / **
   * HMAC encryption
   *
   * @param data
   * @param key
   * @return
   * @throws Exception
   * /
  public static byte [] encryptHMAC (byte [] data, String key) throws Exception {
    SecretKey secretKey = new SecretKeySpec (key.getBytes (), KEY_MAC);
    Mac mac = Mac.getInstance (secretKey.getAlgorithm ());
    mac.init (secretKey);
    return mac.doFinal (data);
  }
  / * byte array converted to HexString * /
  public static String byteArrayToHexString (byte [] b) {
    StringBuffer sb = new StringBuffer (b.length * 2);
    for (int i = 0; i <b.length; i ++) {
     int v = b [i] & 0xff;
     if (v <16) {
      sb.append ('0');
     }
     sb.append (Integer.toHexString (v));
    }
    return sb.toString ();
   }
  public static void main (String [] args) throws Exception {
    String inputStr = "{\" name \ ": \" zhangsan \ "}";
    byte [] inputData = inputStr.getBytes ();
    String key = "laidefa";
    System.out.println (Hmacmd5.byteArrayToHexString (Hmacmd5.encryptHMAC (inputData, key)));
  }
} 





Output Result:


2cbb94ce78b35e4030851c4d40dacf12


PS: About encryption and decryption of interested friends can also refer to the site online tools:



Text online encryption and decryption tool (includes AES, DES, RC4, etc.):
Http://tools.jb51.net/password/txt_encode



MD5 Online Encryption tool:
Http://tools.jb51.net/password/CreateMD5Password



Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt



Online MD5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:
Http://tools.jb51.net/password/hash_md5_sha



Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:


Http://tools.jb51.net/password/sha_encode









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.