`

java密钥研究_完成

阅读更多
未限制的策略文件,需要将jdk中的jar文件替换一下
jce_policy-6.zip (Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6)
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html


api
http://www.oschina.net/uploads/doc/javase-6-doc-api-zh_CN/javax/crypto/Cipher.html#unwrap(byte[], java.lang.String, int)

java中这个加密类com.sun.crypto.provider.SunJCE() 这个类需要sunjce_provider.jar包,解决办法:
http://wenwen.soso.com/z/q361351384.htm
http://zjhwl.iteye.com/blog/1006128

相关网站:
http://www.bouncycastle.org/resources.html

使用java从证书中读取私钥
http://xwhoyeah.iteye.com/blog/86377
分享到:
评论
2 楼 zfms 2012-04-23  
引用
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncodeHmacSHA512 {

/**
* @param args
*/
public static void main(String[] args) {
try {
testEncodeHmacSHA512();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 测试HmacSHA512
*
* @throws Exception
*/

public static void testEncodeHmacSHA512() throws Exception {
// Security.addProvider(new
// org.bouncycastle.jce.provider.BouncyCastleProvider());
String str = "JNLH";

// 初始化密钥
byte[] key = { 0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc,
(byte) 0xde, (byte) 0xf0, 0x34, 0x56, 0x78, (byte) 0x9a,
(byte) 0xbc, (byte) 0xde, (byte) 0xf0, 0x12, 0x56, 0x78,
(byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, 0x12, 0x34,
0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, 0x12,
0x34, 0x56 };
// 获得摘要信息
byte[] data = encodeHmacSHA512(str.getBytes(), key);
System.out.println(new String(data));
// 校验
// assertArrayEquals(data1, data2);
}

/**
* HmacSHA512消息摘要
*
* @param data
*            待做摘要处理的数据
* @param key
*            密钥
* @return byte[] 消息摘要
* @throws Exception
*/
public static byte[] encodeHmacSHA512(byte[] data, byte[] key)
throws Exception {
// 还原密钥
SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
// 实例化Mac
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
// 初始化Mac
mac.init(secretKey);
// 执行消息摘要
return mac.doFinal(data);
}
}
1 楼 zfms 2012-04-21  
public class TamperedWithHMacExample {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 随机
SecureRandom random = new SecureRandom();
IvParameterSpec ivSpec = Utils.createCtrIvForAES(1, random);
Key key = Utils.createKeyForAES(256, random);
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
String input = "Transfer 0000100 to AC 1234-5678";
Mac hMac = Mac.getInstance("HMacSHA512", "BC");
Key hMacKey = new SecretKeySpec(key.getEncoded(), "HMacSHA512");

System.out.println("input : " + input);

// encryption step 加密步骤

cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

byte[] cipherText = new byte[cipher.getOutputSize(input.length()
+ hMac.getMacLength())];

int ctLength = cipher.update(Utils.toByteArray(input), 0,
input.length(), cipherText, 0);

hMac.init(hMacKey);
hMac.update(Utils.toByteArray(input));

ctLength += cipher.doFinal(hMac.doFinal(), 0, hMac.getMacLength(),
cipherText, ctLength);

// tampering step

// cipherText[9] ^= '0' ^ '9';

// replace digest

// ?

// decryption step 解密步骤

cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
System.out.println(Utils.toString(cipherText));
byte[] plainText = cipher.doFinal(cipherText, 0, ctLength);
int messageLength = plainText.length - hMac.getMacLength();

hMac.init(hMacKey);
hMac.update(plainText, 0, messageLength);

byte[] messageHash = new byte[hMac.getMacLength()];
System.arraycopy(plainText, messageLength, messageHash, 0,
messageHash.length);

System.out.println("plain : "
+ Utils.toString(plainText, messageLength) + " verified: "
+ MessageDigest.isEqual(hMac.doFinal(), messageHash));
}
}

相关推荐

Global site tag (gtag.js) - Google Analytics