Home > Net >  Encounter a difficult problem that how to implement Java messageDigest in c # environment. The diges
Encounter a difficult problem that how to implement Java messageDigest in c # environment. The diges

Time:12-23

Public static String getSHA256 (String STR)
{
The MessageDigest MessageDigest.
String encodestr="";
Try
{
MessageDigest=messageDigest. GetInstance (" SHA - 256 ");
The messageDigest. Update (STR. GetBytes (StandardCharsets. UTF_8));
Encodestr=byte2Hex (messageDigest. Digest ());
}
The catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
Return encodestr;
}

The above is a Java class,
I'm over here is the c # and the.net framework, you need to post an interface, the interface is a Java implementation, but the found, a parameter is to encrypt, use a class is shown above, now I'm stuck in the messageDigest. Digest () here, sha256 can implement these have a similar way, but I'm looking for a long time, do not fall messageDigest. Find digest () is implemented in c #,
Dear bosses, sincere, boy grateful,

CodePudding user response:

 
String encodestr="";
Using (SHA256 mySHA256=SHA256. The Create ())
{
//Compute and print the hash values for each file in the directory.
Try
{

Byte [] hashValue=https://bbs.csdn.net/topics/mySHA256.ComputeHash (Encoding UTF8. GetBytes (STR));
Foreach (byte b in hashValue)
{
Encodestr +=b.T oString (" X2 ");
}
Console. WriteLine (encodestr);

}
The catch (UnauthorizedAccessException e)
{
Console. WriteLine ($" Access Exception: {e.M essage} ");
}

CodePudding user response:

 
Foreach (byte b in hashValue)
{
Encodestr +=b.T oString (" X2 ");
}

Can be replaced by
 
The Convert. ToHexString (hashValue)

CodePudding user response:

refer to the second floor icoolno1 response:
 
Foreach (byte b in hashValue)
{
Encodestr +=b.T oString (" X2 ");
}

Can be replaced by
 
The Convert. ToHexString (hashValue)

Need to.net 5.0

CodePudding user response:

reference 1st floor icoolno1 response:
 
String encodestr="";
Using (SHA256 mySHA256=SHA256. The Create ())
{
//Compute and print the hash values for each file in the directory.
Try
{

Byte [] hashValue=https://bbs.csdn.net/topics/mySHA256.ComputeHash (Encoding UTF8. GetBytes (STR));
Foreach (byte b in hashValue)
{
Encodestr +=b.T oString (" X2 ");
}
Console. WriteLine (encodestr);

}
The catch (UnauthorizedAccessException e)
{
Console. WriteLine ($" Access Exception: {e.M essage} ");
}


Excuse me, whether need to consider the byte [] of JAVA and c #, is not the same as the scope? Because the Java byte is - 128-127

CodePudding user response:

reference 4 floor genshi12309 response:
Quote: reference 1st floor icoolno1 response:
 
String encodestr="";
Using (SHA256 mySHA256=SHA256. The Create ())
{
//Compute and print the hash values for each file in the directory.
Try
{

Byte [] hashValue=https://bbs.csdn.net/topics/mySHA256.ComputeHash (Encoding UTF8. GetBytes (STR));
Foreach (byte b in hashValue)
{
Encodestr +=b.T oString (" X2 ");
}
Console. WriteLine (encodestr);

}
The catch (UnauthorizedAccessException e)
{
Console. WriteLine ($" Access Exception: {e.M essage} ");
}


Excuse me, whether need to consider the byte [] of JAVA and c #, is not the same as the scope? Because the Java byte is - 128-127

Hex is the same
  •  Tags:  
  • C#
  • Related