如何通过传递密钥使用 sha-1 加密字符串?
我正在尝试加密字符串消息,以便在我的应用程序中为相同的字符串消息获取相同的加密代码。如何使用相同的密钥获得相同的加密代码?
下面是我的代码。它正在生成加密代码,但与服务器生成的代码不匹配
public String SHAsum(String convertme) {
MessageDigest md = null;
byte[] data;
try {
md = MessageDigest.getInstance("SHA-1");
data=convertme.getBytes("utf8");
md.update(data);
md.hashCode();
//md.update( key.getBytes() );
return new BigInteger( 1, md.digest() ).toString(16);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //This could also be SHA1withDSA, no exception handling
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
String password="qqqqq";
byte[] salt="guVoUubWwvniR2G0FgaC9".getBytes();
String abc="";
try
{
abc = getHash( password, salt);
System.out.println("Encypted password is -------------->"+abc);
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (UnsupportedEncodingException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
I'm trying to encrypt a string message to get same encrypted code for same string message in my app. How can i get the same encrypted code using same key?
Below is my code. It is generating encrypted code but it does not match with that code which is generated at server
public String SHAsum(String convertme) {
MessageDigest md = null;
byte[] data;
try {
md = MessageDigest.getInstance("SHA-1");
data=convertme.getBytes("utf8");
md.update(data);
md.hashCode();
//md.update( key.getBytes() );
return new BigInteger( 1, md.digest() ).toString(16);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //This could also be SHA1withDSA, no exception handling
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
String password="qqqqq";
byte[] salt="guVoUubWwvniR2G0FgaC9".getBytes();
String abc="";
try
{
abc = getHash( password, salt);
System.out.println("Encypted password is -------------->"+abc);
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (UnsupportedEncodingException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论