这是我的java代码,使用密钥及其正常工作加密String值,但我不知道如何将这些ecrypted值解密为My original values ..?
package com.password;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Sha256 {
//Main Method that have the String values and key
public static void main(String s[]) {
try {
String str ="HelloWorld"; //String Values
String key = "test@12345"; //Secret Key
String encry = hashMac(str, key);
//call the hashMac Method that encrypt the String using key and return the encrypted values....
System.out.println("Encryption : " + encry);
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//hashMac Method that encrypt the data and convert into hex values...
public static String hashMac(String text, String secretKey)
throws SignatureException {
try {
Key sk = new SecretKeySpec(secretKey.getBytes(), HASH_ALGORITHM);
Mac mac = Mac.getInstance(sk.getAlgorithm());
mac.init(sk);
final byte[] hmac = mac.doFinal(text.getBytes());
return toHexString(hmac);//call toHexString Methods....
} catch (NoSuchAlgorithmException e1) {
// throw an exception or pick a different encryption method
throw new SignatureException(
"error building signature, no such algorithm in device "
+ HASH_ALGORITHM);
} catch (InvalidKeyException e) {
throw new SignatureException(
"error building signature, invalid key " + HASH_ALGORITHM);
}
}
private static final String HASH_ALGORITHM = "HmacSHA256";
//toHexString Method...
public static String toHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02x", b);
}
return sb.toString();
}
}
如何解密加密字符串....?
答案 0 :(得分:1)
这是我使用密钥加密String值的java代码
不,不是。这是使用SHA-256安全散列算法,使用字符串值的安全散列创建消息验证代码的Java代码。
它的正常工作,但我不知道如何将这些ecrypted值解密为我的原始值..?
此处没有加密值。
如何解密加密字符串....?
此处没有加密String
。存在String
的安全散列,安全散列的主要特性是它不可逆。
你的问题没有意义。
您正在寻找的是检查HMAC的方法吗?在这种情况下,您需要使用相同的密钥从同一消息重新创建HMAC,并进行比较。