本文转载自https://hiramtan.wordpress.com
第一部分,MD5加密:
涉及到账号密码加密,以为是规定一个密钥,客户端根据密钥加密后传输给服务器,服务器根据密钥解密后验证密码是否正确,这样的话后端程序完全了解客户的帐号密码是多少.或者是已这种方式实现:客户端加密账号密码后发送给服务端,服务端存储玩家的账号密码的密文,登陆时验证玩家的密文就可以.
最常用的是md5加密,visual中代码如下:
using
System.Text;
namespace
Md5NamaSpace
{
public
class
MyMd5
{
///
///
md5加密方法
///
///
public
static
string
StringToMd5(
string
tempString)
{
System.Security.Cryptography.
MD5
md5 = System.Security.Cryptography.
MD5
.Create();
byte
[] bs =
Encoding
.UTF8.GetBytes(tempString);
byte
[] hs = md5.ComputeHash(bs);
return
System.
BitConverter
.ToString(hs).Replace(
“-“
,
“”
);
}
}
}
然后打包成dll,复制到unity的asset文加件下,在unity中引用命名空间,直接调用类中的方法就可以了:
using
UnityEngine;
using
System.Collections;
using
Md5NamaSpace;
using
System.Text;
public
class
test
:
MonoBehaviour
{
// Use this for initialization
void
Start()
{
string
test =
MyMd5
.StringToMd5(
“nihao”
);
Debug
.Log(test);
}
}
debug出来的信息就是nihao的md5加密后的值.
第二部分,AES加密:
使用unity中的socket传输游戏中的数据,必定要做加密解密功能,本次采用的是aes加密(des升级版),封装dll然后unity3d中调用使用.如下
using
System;
namespace
MyEncryptionNamespace
{
///
///
aes 加密
///
public
class
MyEncryption
{
///
///
32位密钥
///
public
static
string
key;
///
///
加密
///
///
public
static
string
Encrypt(
string
toEncrypt)
{
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(key);
Byte
[] toEncryptArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(toEncrypt);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateEncryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return
Convert
.ToBase64String(resultArray, 0, resultArray.Length);
}
///
///
加密密码
///
///
public
static
string
EncryptPassword(
string
toEncrypt)
{
string
keyForPassword =
“qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq”
;
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(keyForPassword);
Byte
[] toEncryptArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(toEncrypt);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateEncryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return
Convert
.ToBase64String(resultArray, 0, resultArray.Length);
}
///
///
解密
///
///
public
static
string
Decrypt(
string
toDecrypt)
{
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(key);
Byte
[] toDecryptArray =
Convert
.FromBase64String(toDecrypt);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateDecryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
return
System.Text.
UTF8Encoding
.UTF8.GetString(resultArray);
}
}
}
生成dll到untiy中,加密解密正常运行…
然后将加密类型修改为加密byte[]类型,代码如下:
using
System;
namespace
HiAES
{
public
class
MyAES
{
///
///
32位密钥
///
public
static
string
key;
///
///
加密
///
///
public
static
byte
[] Encrypt(
byte
[] toEncrypt)
{
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(key);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateEncryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
return
resultArray;
}
///
///
加密
///
///
public
static
byte
[] EncryptPassword(
byte
[] toEncrypt)
{
string
keyForPassword =
“qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq”
;
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(keyForPassword);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateEncryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
return
resultArray;
}
///
///
解密
///
///
public
static
byte
[] Decrypt(
byte
[] toDecrypt)
{
Byte
[] keyArray = System.Text.
UTF8Encoding
.UTF8.GetBytes(key);
System.Security.Cryptography.
RijndaelManaged
aes =
new
System.Security.Cryptography.
RijndaelManaged
();
aes.Key = keyArray;
aes.Mode = System.Security.Cryptography.
CipherMode
.ECB;
aes.Padding = System.Security.Cryptography.
PaddingMode
.PKCS7;
System.Security.Cryptography.
ICryptoTransform
cTransform = aes.CreateDecryptor();
Byte
[] resultArray = cTransform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);
return
resultArray;
}
}
}
在unity3d中添加命名空间myaesencryption调用如下:
void
Start()
{
MyEncryption
.key =
“00000000000000000000000000ww00qq”
;
string
temp =
“i am client”
;
byte
[] data = System.Text.
Encoding
.UTF8.GetBytes(temp);
byte
[] tempdata =
MyEncryption
.Encrypt(data);
//加密
byte
[] tempdata2 =
MyEncryption
.Decrypt(tempdata);
//解密
Debug
.Log(System.Text.
Encoding
.UTF8.GetString(tempdata2));
}
加密解密过程运算正常…
support: hiramtan@
qq.com