zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java签名算法之HMAC-SHA1

JAVA算法 签名 SHA1 HMAC
2023-09-14 08:59:36 时间
 * This class defines common routines for generating authentication signatures  * for AWS requests. public class Signature { private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";  * Computes RFC 2104-compliant HMAC signature. * @param data The data to be  * signed.  * @param key  *            The signing key.  * @return The Base64-encoded RFC 2104-compliant HMAC signature.  * @throws java.security.SignatureException  *             when signature generation fails public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException { String result; try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // base64-encode the hmac result = BASE64Encoder.encode(rawHmac); // result = Encoding.EncodeBase64(rawHmac); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); return result; public static byte[] hmacSHA1(String data, String key) throws java.security.SignatureException { try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); return rawHmac; } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); }

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SignatureException;

import java.util.Formatter; 

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;  

 * The  tt HmacSha1Signature /tt  shows how to calculate  

 * a message authentication code using HMAC-SHA1 algorithm. 

 *  pre  

 * % java -version 

 * java version "1.6.0_11" 

 * % javac HmacSha1Signature.java  

 * % java -ea HmacSha1Signature 

 * 104152c5bfdca07bc633eebd46199f0255c9f49d 

 *  /pre  

 public class HmacSha1Signature { 

     private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";  

     private static String toHexString(byte[] bytes) { 

         Formatter formatter = new Formatter(); 

         for (byte b : bytes) { 

             formatter.format("%02x", b); 、

         }  

         return formatter.toString(); 

    }  

    public static String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { 

        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); 

        mac.init(signingKey); 

        return toHexString(mac.doFinal(data.getBytes())); 

     }  

     

     public static void main(String[] args) throws Exception { 

         String hmac = calculateRFC2104HMAC("data", "key");  

         System.out.println(hmac); 

         assert hmac.equals("104152c5bfdca07bc633eebd46199f0255c9f49d"); 

     }

}

非对称密钥PKCS#1和PKCS#8格式互相转换(Java) 之前在 《前后端RSA互相加解密、加签验签、密钥对生成》 中提到过PKCS#1格式和PKCS#8格式密钥的区别以及如何生成密钥。实际有些场景中有可能也会涉及到前后端密钥格式不一致,这篇文章我们会讨论关于PKCS#1和PKCS#8格式密钥的互相转换。
Java-AES加解密 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。高级加密标准算法从很多方面解决了令人担忧的问题。实际上,攻击数据加密标准的那些手段对于高级加密标准算法本身并没有效果。如果采用真正的128位加密技术甚至256位加密技术,蛮力攻击要取得成功需要耗费相当长的时间。安全研究人员还没有那么多的时间对这种加密方法进行破解试验。我们可能会随时发现一种全新的攻击手段会攻破这种高级加密标准。至少在理论上存在这种可能性。
Java数字签名——DSA算法 RSA数字加密算法参考:http://www.cnblogs.com/LexMoon/p/javaRSA.html DSS: 数字签名标准 DSA: 数字签名算法 DSA仅仅包含数字签名 ———————————————————————————————————— 密钥...
Java数字签名——RSA算法 数字签名:带有密钥(公钥,私钥)的消息摘要算法。 验证数据的完整性,认证数据的来源,抗否性 OSI参考模型 私钥签名,公钥验证 签名算法:RSA,DSA,ECDSA 算法1 :RSA MD,SHA两类 ———————————————...