第十二篇:JAVA加密解密之常用签名算法

来源:互联网 发布:汽车零部件出口数据 编辑:程序博客网 时间:2024/06/05 10:48

Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)。 
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法),主流编程语言普遍已有MD5实现。将数据(如汉字)运算为另一固定长度值,是杂凑算法的基础原理,MD5的前身有MD2、MD3和MD4。 
MD5算法具有以下特点: 
1. 压缩性:任意长度的数据,算出的MD5值长度都是固定的。 
2. 容易计算:从原数据计算出MD5值很容易。 
3. 抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。 
4. 强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。

HMAC是密钥相关的哈希运算消息认证码(Hash-based Message Authentication Code),HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。

在下面的示例中,将演示MD5、Hmac等签名算法的使用方式

import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.zip.CRC32;import javax.crypto.KeyGenerator;import javax.crypto.Mac;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * 加密解密基类 *  * @author jianggujin *  */public class BasicCoder{   /**    * MD5(Message Digest algorithm 5,信息摘要算法)    *     * @param data    *           加密数据    * @return    * @throws UnsupportedEncodingException    * @throws NoSuchAlgorithmException    */   public byte[] makeMD5(byte[] data) throws NoSuchAlgorithmException   {      MessageDigest mdTemp = MessageDigest.getInstance("MD5");      mdTemp.update(data);      return mdTemp.digest();   }   /**    * SHA(Secure Hash Algorithm,安全散列算法)    *     * @param data    *           加密数据    * @return    * @throws NoSuchAlgorithmException    * @throws Exception    */   public byte[] makeSHA(byte[] data) throws NoSuchAlgorithmException   {      MessageDigest sha = MessageDigest.getInstance("SHA");      sha.update(data);      return sha.digest();   }   /**    * HmacSHA1    *     * @param data    *           加密数据    * @param key    *           密钥    * @return    * @see Hmac    * @throws UnsupportedEncodingException    * @throws NoSuchAlgorithmException    * @throws InvalidKeyException    */   public byte[] makeHmacSHA1(byte[] data, byte[] key)         throws NoSuchAlgorithmException, InvalidKeyException   {      return makeHmac(Hmac.HmacSHA1, data, key);   }   /**    * HmacMD5    *     * @param data    *           加密数据    * @param key    *           密钥    * @return    * @see Hmac    * @throws UnsupportedEncodingException    * @throws NoSuchAlgorithmException    * @throws InvalidKeyException    */   public byte[] makeHmacMD5(byte[] data, byte[] key)         throws NoSuchAlgorithmException, InvalidKeyException   {      return makeHmac(Hmac.HmacMD5, data, key);   }   /**    * Hmac    *     * @param hmac    *           加密类型    * @param data    *           加密数据    * @param key    *           密钥    * @return    * @see Hmac    * @throws UnsupportedEncodingException    * @throws NoSuchAlgorithmException    * @throws InvalidKeyException    */   public byte[] makeHmac(Hmac hmac, byte[] data, byte[] key)         throws NoSuchAlgorithmException, InvalidKeyException   {      Mac mac = Mac.getInstance(hmac.toString());      SecretKeySpec spec = new SecretKeySpec(key, hmac.toString());      mac.init(spec);      return mac.doFinal(data);   }   /**    * CRC32,循环冗余校验    *     * @param data    *           校验数据    * @return    */   public long makeCRC32(byte[] data)   {      CRC32 crc32 = new CRC32();      crc32.update(data);      return crc32.getValue();   }   /**    * Hmac(Hash Message Authentication Code,散列消息鉴别码)    *     * @author jianggujin    *     */   public enum Hmac   {      HmacMD5, HmacSHA1, HmacSHA256, HmacSHA384, HmacSHA512   }   /**    * 初始化HMAC密钥    *     * @return    * @see Hmac    * @throws Exception    */   public byte[] initMacKey(Hmac hmac) throws Exception   {      KeyGenerator keyGenerator = KeyGenerator.getInstance(hmac.toString());      SecretKey secretKey = keyGenerator.generateKey();      return secretKey.getEncoded();   }   private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6',         '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };   /**    * 将字节流按16进制转换成字符串.    */   public static String byte2String(byte[] buf)   {      int count = buf.length;      StringBuffer sbuf = new StringBuffer();      for (int i = 0; i < count; i++)      {         byte byte0 = buf[i];         sbuf.append(hexDigits[byte0 >>> 4 & 0xf]).append(               hexDigits[byte0 & 0xf]);      }      return sbuf.toString().toUpperCase();   }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171

原创粉丝点击