|
本帖最后由 autoit3CN 于 2020-1-11 01:38 编辑 public static string AesEncrypt(string input, string password, string salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] bytes2 = Encoding.UTF8.GetBytes(salt);
using (AesManaged aesManaged = new AesManaged())
{
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, bytes2);
aesManaged.BlockSize = aesManaged.LegalBlockSizes[0].MaxSize;
aesManaged.KeySize = aesManaged.LegalKeySizes[0].MaxSize;
aesManaged.Key = rfc2898DeriveBytes.GetBytes(aesManaged.KeySize / 8);
aesManaged.IV = rfc2898DeriveBytes.GetBytes(aesManaged.BlockSize / 8);
using (ICryptoTransform transform = aesManaged.CreateEncryptor())
{
MemoryStream memoryStream = new MemoryStream();
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
{
cryptoStream.Write(bytes, 0, bytes.Length);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
public Rfc2898DeriveBytes(string password, byte[] salt, int iterations)
: this(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(password), salt, iterations)
{
}
public class Rfc2898DeriveBytes : DeriveBytes
{
private const int BlockSize = 20;
private byte[] m_buffer;
private byte[] m_salt;
private HMACSHA1 m_hmacsha1;
private uint m_iterations;
private uint m_block;
private int m_startIndex;
private int m_endIndex;
public int IterationCount
{
get
{
return (int)m_iterations;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
}
m_iterations = (uint)value;
Initialize();
}
}
public byte[] Salt
{
get
{
return (byte[])m_salt.Clone();
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (value.Length < 8)
{
throw new ArgumentException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_FewBytesSalt"));
}
m_salt = (byte[])value.Clone();
Initialize();
}
}
public Rfc2898DeriveBytes(string password, byte[] salt)
: this(password, salt, 1000)
{
}
public Rfc2898DeriveBytes(string password, byte[] salt, int iterations)
: this(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(password), salt, iterations)
{
}
public Rfc2898DeriveBytes(byte[] password, byte[] salt, int iterations)
{
Salt = salt;
IterationCount = iterations;
m_hmacsha1 = new HMACSHA1(password);
Initialize();
}
public override byte[] GetBytes(int cb)
{
if (cb <= 0)
{
throw new ArgumentOutOfRangeException("cb", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
}
byte[] array = new byte[cb];
int i = 0;
int num = m_endIndex - m_startIndex;
if (num > 0)
{
if (cb < num)
{
Buffer.InternalBlockCopy(m_buffer, m_startIndex, array, 0, cb);
m_startIndex += cb;
return array;
}
Buffer.InternalBlockCopy(m_buffer, m_startIndex, array, 0, num);
m_startIndex = (m_endIndex = 0);
i += num;
}
for (; i < cb; i += 20)
{
byte[] src = Func();
int num2 = cb - i;
if (num2 > 20)
{
Buffer.InternalBlockCopy(src, 0, array, i, 20);
continue;
}
Buffer.InternalBlockCopy(src, 0, array, i, num2);
i += num2;
Buffer.InternalBlockCopy(src, num2, m_buffer, m_startIndex, 20 - num2);
m_endIndex += 20 - num2;
return array;
}
return array;
}
public override void Reset()
{
Initialize();
}
private void Initialize()
{
if (m_buffer != null)
{
Array.Clear(m_buffer, 0, m_buffer.Length);
}
m_buffer = new byte[20];
m_block = 1u;
m_startIndex = (m_endIndex = 0);
}
private byte[] Func()
{
byte[] array = Utils.Int(m_block);
m_hmacsha1.TransformBlock(m_salt, 0, m_salt.Length, m_salt, 0);
m_hmacsha1.TransformFinalBlock(array, 0, array.Length);
byte[] array2 = m_hmacsha1.Hash;
m_hmacsha1.Initialize();
byte[] array3 = array2;
for (int i = 2; i <= m_iterations; i++)
{
array2 = m_hmacsha1.ComputeHash(array2);
for (int j = 0; j < 20; j++)
{
array3[j] ^= array2[j];
}
}
m_block++;
return array3;
}
}
public static string ToBase64String(byte[] inArray)
{
if (inArray == null)
{
throw new ArgumentNullException("inArray");
}
return ToBase64String(inArray, 0, inArray.Length, Base64FormattingOptions.None);
}
求问: AesEncrypt(string input, string password, string salt)这个C#函数如何转化AU3所用?
三个参数已有:
input=AAAAAA|123456(私人账号|密码)
SecretKey Password = "Z_h$W@123";
SecretKey Salt = "123@Z_h$W";
自己摸索结果:
Cryptography.AesEncrypt结果:IA6PCpe/zZEtICN7E/EFXw==
但是我采用acn论坛、英文论坛、其他在线解密网站(http://tool.chacuo.net/cryptaes),
结算结果均为错误的或其他:WznD9w7icLss4DLVThTFUQ==
其中使用英文论坛:#include <ComboConstants.au3>
#include <Crypt.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $g_hKey = -1, $g_idInputEdit = -1, $g_idOutputEdit = -1
Example()
Func Example()
Local $hGUI = GUICreate("Realtime Encryption", 400, 320)
$g_idInputEdit = GUICtrlCreateEdit("AAAAAA|123456", 0, 0, 400, 150, $ES_WANTRETURN)
$g_idOutputEdit = GUICtrlCreateEdit("", 0, 150, 400, 150, $ES_READONLY)
Local $idCombo = GUICtrlCreateCombo("", 0, 300, 100, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData($idCombo, "AES (128bit)|AES (192bit)|AES (256bit)", "AES (128bit)")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState(@SW_SHOW, $hGUI)
_Crypt_Startup() ; To optimize performance start the crypt library.
_Crypt_DestroyKey($g_hKey) ; Destroy the cryptographic key.
;~ $g_hKey = _Crypt_DeriveKey("CryptPassword", $iAlgorithm) ; Re-declare a password string and algorithm to create a new cryptographic key.
$iAlgorithm = $CALG_AES_128
$g_hKey = _Crypt_DeriveKey("Z_h$W@123", $iAlgorithm)
Local $sRead = GUICtrlRead($g_idInputEdit)
If StringStripWS($sRead, $STR_STRIPALL) <> "" Then ; Check there is text available to encrypt.
Local $bEncrypted = _Crypt_EncryptData($sRead, $g_hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.
GUICtrlSetData($g_idOutputEdit, Base64($bEncrypted)) ; Set the output box with the encrypted text.
EndIf
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $idCombo ; Check when the combobox is selected and retrieve the correct algorithm.
Switch GUICtrlRead($idCombo) ; Read the combobox selection.
Case "AES (128bit)"
$iAlgorithm = $CALG_AES_128
Case "AES (192bit)"
$iAlgorithm = $CALG_AES_192
Case "AES (256bit)"
$iAlgorithm = $CALG_AES_256
EndSwitch
EndSwitch
WEnd
GUIDelete($hGUI) ; Delete the previous GUI and all controls.
_Crypt_DestroyKey($g_hKey) ; Destroy the cryptographic key.
_Crypt_Shutdown() ; Shutdown the crypt library.
EndFunc ;==>Example
Func Base64($vCode, $bEncode = True, $bUrl = False)
Local $oDM = ObjCreate("Microsoft.XMLDOM")
If Not IsObj($oDM) Then Return SetError(1, 0, 1)
Local $oEL = $oDM.createElement("Tmp")
$oEL.DataType = "bin.base64"
If $bEncode Then
$oEL.NodeTypedValue = Binary($vCode)
If Not $bUrl Then Return $oEL.Text
Return StringReplace(StringReplace(StringReplace($oEL.Text, "+", "-"), "/", "_"), @LF, "")
Else
If $bUrl Then $vCode = StringReplace(StringReplace($vCode, "-", "+"), "_", "/")
$oEL.Text = $vCode
Return $oEL.NodeTypedValue
EndIf
EndFunc ;==>Base64
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch _WinAPI_LoWord($wParam)
Case $g_idInputEdit
Switch _WinAPI_HiWord($wParam)
Case $EN_CHANGE
Local $bEncrypted = _Crypt_EncryptData(GUICtrlRead($g_idInputEdit), $g_hKey, $CALG_USERKEY) ; Encrypt the text with the cryptographic key.
GUICtrlSetData($g_idOutputEdit, Base64($bEncrypted)) ; Set the output box with the encrypted text.
EndSwitch
EndSwitch
EndFunc ;==>WM_COMMAND
以下为该函数的C#DLL,因无接口本想通过regasm注册再用au3调用,但注册失败:#PRE_UseX64=n
#RequireAdmin
Global $dll = 'Inhua.Silverlight.Common.dll'
Global $RegAsm = @ScriptDir & '\regasm.exe'
Local $obj = regdll()
Func regdll()
Local $obj = ObjCreate("Inhua.Silverlight.Common.Cryptography")
If Not IsObj($obj) Then
;~ RunWait($RegAsm & " /codebase " & $dll, @ScriptDir, @SW_HIDE) ; register the .net DLL
$obj = ObjCreate("Inhua.Silverlight.Common.Cryptography")
If Not IsObj($obj) Then
MsgBox(0, @error, '注册失败')
Exit
EndIf
EndIf
Return $obj
EndFunc ;==>regdll
如果能成功Obj调用就是最简单的解决方法了,故附上dll,望熟悉加密算法的坛友指点迷津,感激不尽!
亦可参考本文:https://blog.csdn.net/kangrydotnet/article/details/43987835(c#爬取Silverlight网页)此文解决思路基本符合。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入
×
|