C# 在不同的机器上具有不同行为的CHAP
我正在开发一个android应用程序,它使用RESTful服务与服务器通信。对于登录,我使用CHAP方法C# 在不同的机器上具有不同行为的CHAP,c#,java,android,C#,Java,Android,我正在开发一个android应用程序,它使用RESTful服务与服务器通信。对于登录,我使用CHAP方法 在机器1(Win7 32位)上,登录可以工作 在机器2(Win7 64位)上,登录不起作用 以下是我使用的代码: 服务器端(C#): 公共字符串AuthenticateByPassword(字符串authUserName,长CRC值) { 如果(!CHAPUtil.CheckAuth(authUserName,crcvalue)) 抛出新的PermissionDeniedExceptio
- 在机器1(Win7 32位)上,登录可以工作
- 在机器2(Win7 64位)上,登录不起作用
公共字符串AuthenticateByPassword(字符串authUserName,长CRC值)
{
如果(!CHAPUtil.CheckAuth(authUserName,crcvalue))
抛出新的PermissionDeniedException();
返回masterdata.AuthenticateByPassword(authUserName,CHAPUtil.getPasswordByUsername(authUserName));
}
公共长GetAuthenticationChallenge(字符串用户名)
{
长挑战=CHAPUtil.getChallenge();
CHAPUtil.CalculateHash(用户名,挑战);
回归挑战;
}
公共类CHAPUtil
{
私有静态哈希表crcValues=新哈希表();
公共静态长getChallenge()
{
随机r=新随机();
返回r.Next();
}
公共静态void CalculateHash(字符串用户名,长随机)
{
Crc32 crc=新的Crc32();
long crcResult=crc.ComputeChecksum(StringToByteArray(getPasswordByUsername(username));
if(crcValues.Contains(用户名))
{
crcValues.Remove(用户名);
}
添加(用户名,crcResult*random);
}
公共静态bool CheckAuth(字符串用户名,长哈希值)
{
if(crcValues.Contains(用户名))
{
长值=(长)CRC值[用户名];
if(value==hashvalue)
{
crcValues.Remove(用户名);
返回true;
}
}
返回false;
}
公共静态字符串getPasswordByUsername(字符串用户名)
{
}
私有静态字节[]StringToByteArray(字符串str)
{
System.Text.asciencoding enc=新的System.Text.asciencoding();
返回enc.GetBytes(str);
}
}
公共级Crc32
{
uint[]表;
公共长计算机密钥(字节[]字节)
{
uint crc=0xFFFFFF;
for(int i=0;i>8)^表[索引];
}
返回~crc;
}
公共字节[]计算字节数(字节[]字节)
{
返回BitConverter.GetBytes(ComputeChecksum(bytes));
}
公共Crc32()
{
uint poly=0xedb88320;
表=新uint[256];
单位温度=0;
对于(uint i=0;i0;--j)
{
如果((温度和1)=1)
{
温度=(uint)((温度>>1)^poly);
}
其他的
{
温度>>=1;
}
}
表[i]=温度;
}
}
}
客户端(Java/Android)
私有静态字符串登录(字符串用户名、字符串密码){
//挑战握手身份验证
//第一步-获取身份验证挑战(随机长)
长挑战=MasterdataServices.getAuthentificationChallenge(用户名);
如果(挑战==0 | |挑战==-1){
返回null;
}
//获取哈希密码
字符串hashedPassword=getHashedPassword(passwd);
//第二步-获取crc值
long crcValue=getCrcValue(hashedPassword,challenge);
//第三步-获取会话令牌
String sessionId=MasterdataServices.authenticateByPassword(用户名,crcValue);
返回会话ID;
}
私有静态长getCrcValue(字符串密码,长challengeValue){
CRC32 crc=新的CRC32();
试一试{
crc.update(password.getBytes(“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
返回crc.getValue()*challengeValue;
}
私有静态字符串getHashedPassword(字符串密码){
MessageDigest=null;
试一试{
messageDigest=messageDigest.getInstance(“SHA-512”);
}捕获(无算法异常){
e、 printStackTrace();
返回null;
}
字节[]encryptedPw=null;
试一试{
encryptedPw=messageDigest.digest(password.getBytes(“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
返回null;
}
StringBuilder十六进制=新StringBuilder(encryptedPw.length*2);
for(字节b:encryptedPw)
{
if((b&0xff)<0x10)十六进制附加(“0”);
hex.append(Integer.toHexString(b&0xff));
}
返回hex.toString();
}
有人知道这些差异可能来自哪里吗?好的,这个问题问得太早了……问题是由用户的欢呼声引起的
public string AuthenticateByPassword(string authUserName, long crcvalue)
{
if (!CHAPUtil.CheckAuth(authUserName, crcvalue))
throw new PermissionDeniedException();
return masterdata.AuthenticateByPassword(authUserName, CHAPUtil.getPasswordByUsername(authUserName));
}
public long getAuthentificationChallenge(string username)
{
long challenge = CHAPUtil.getChallenge();
CHAPUtil.CalculateHash(username, challenge);
return challenge;
}
public class CHAPUtil
{
private static Hashtable crcValues = new Hashtable();
public static long getChallenge()
{
Random r = new Random();
return r.Next();
}
public static void CalculateHash(string username, long random)
{
Crc32 crc = new Crc32();
long crcResult = crc.ComputeChecksum(StringToByteArray(getPasswordByUsername(username)));
if (crcValues.Contains(username))
{
crcValues.Remove(username);
}
crcValues.Add(username, crcResult * random);
}
public static bool CheckAuth(string username, long hashvalue)
{
if (crcValues.Contains(username))
{
long value = (long)crcValues[username];
if (value == hashvalue)
{
crcValues.Remove(username);
return true;
}
}
return false;
}
public static string getPasswordByUsername(string username)
{
}
private static byte[] StringToByteArray(string str)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(str);
}
}
public class Crc32
{
uint[] table;
public long ComputeChecksum(byte[] bytes)
{
uint crc = 0xffffffff;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(((crc) & 0xff) ^ bytes[i]);
crc = (uint)((crc >> 8) ^ table[index]);
}
return ~crc;
}
public byte[] ComputeChecksumBytes(byte[] bytes)
{
return BitConverter.GetBytes(ComputeChecksum(bytes));
}
public Crc32()
{
uint poly = 0xedb88320;
table = new uint[256];
uint temp = 0;
for (uint i = 0; i < table.Length; ++i)
{
temp = i;
for (int j = 8; j > 0; --j)
{
if ((temp & 1) == 1)
{
temp = (uint)((temp >> 1) ^ poly);
}
else
{
temp >>= 1;
}
}
table[i] = temp;
}
}
}
private static String login(String username, String passwd) {
//Challenge handshake authentification
//1st step - get authentification challenge (random long)
long challenge = MasterdataServices.getAuthentificationChallenge(username);
if(challenge == 0 || challenge == -1) {
return null;
}
//get hashed password
String hashedPassword = getHashedPassword(passwd);
//2nd step - get crc value
long crcValue = getCrcValue(hashedPassword, challenge);
//3rd step - get session token
String sessionId = MasterdataServices.authenticateByPassword(username, crcValue);
return sessionId;
}
private static long getCrcValue(String password, long challengeValue) {
CRC32 crc = new CRC32();
try {
crc.update(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return crc.getValue() * challengeValue;
}
private static String getHashedPassword(String password) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
byte[] encryptedPw = null;
try {
encryptedPw = messageDigest.digest(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
StringBuilder hex = new StringBuilder(encryptedPw.length * 2);
for(byte b : encryptedPw)
{
if((b & 0xff) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xff));
}
return hex.toString();
}