delphi key解密转c# 解决string 不可变长度问题
遇见问题:
delphi的解密需要在c#里面实现
方法一:delphi编写delphi dll组件,c#里面调用
方法二:c#重写delphi的代码进行解析
方法一:
delphi部分代码:
library Project2;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes;
function UncrypStr(Src, Key: String): string;//字符串解密函数
//对字符串解密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='delphi';
KeyPos:=0;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen
Then KeyPos := KeyPos + 1
else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset
then TmpSrcAsc := 255 + TmpSrcAsc - offset
else TmpSrcAsc := TmpSrcAsc - offset;
dest:=dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=Dest;
end;
function filesinfo(files:string): string;stdcall;
//var
//t:TStringlist;
//strs:string;
//strsss:string;
begin
//strs:=StrPas(files);
//t := TStringlist.Create;
//t.LoadFromFile(strs);
//strsss := StringReplace(UncrypStr(t.Strings[0], 'Cernet@4206'), ' ', '', [rfReplaceAll]);
Result:=files;
end;
function Check23(AA:string):Pchar;stdcall;
var
t:TStringlist;
strs:string;
begin
t := TStringlist.Create;
t.LoadFromFile(pchar(AA));
strs := StringReplace(UncrypStr(t.Strings[0], 'Cernet@4206'), ' ', '', [rfReplaceAll]);
result:=pchar(strs);
end ;
exports
Check23;
begin
end.
c# 调用 (如果数据量小还可以读出) 这里 的dll组建是x86的 需要把debug改成x86运行
[DllImport(@"D:\rwas\dlls\Project2.dll", EntryPoint = "Check23")]
static extern IntPtr Check23(string sx);
string ss = Marshal.PtrToStringAnsi(Check23("C:\\Users\\忧郁的小学生\\Desktop\\997.azjx"));
if (string.IsNullOrEmpty(ss)) {
Console.WriteLine("xss");
}
方法二:c# 重写 (刚开始用string是不可变长度,每次追加相当于开辟新的空间了,cpu占用过大,最后使用stringbuffer里面可变的)
public string UncrypStr(string src="",string key="") {
int KeyLen = key.Length;
src = src.Replace("\n", "");
src = src.Replace("\r", "");
if (KeyLen==0) {
key = "delphi";
}
int KeyPos= 0;
int SrcAsc = 0;
StringBuilder ps = new StringBuilder();
string dest = "";
int ks = 0;
//16进制转换位10进制 delph 从起始位开始 c# 字节数组 从0开始
string xs = src.Substring(0, 2);
int offset = Convert.ToInt32(xs, 16);
int SrcPos= 2;
int TmpSrcAsc = 0;
char[] chars = key.ToCharArray();
while (SrcPos < (int)src.Length) {
//截取字符串
string tx = src.Substring(SrcPos, 2);
//转换为16进制 将10进制转换为16进制
SrcAsc = Convert.ToInt32(tx, 16);
//判断解密密钥的起始
if (KeyPos < KeyLen)
{
KeyPos++;
}
else {
KeyPos = 1;
}
//分割成单个字符
char s = chars[KeyPos-1];
//byte[] array = System.Text.Encoding.ASCII.GetBytes(chars);
TmpSrcAsc = SrcAsc ^ (int)s;
if (TmpSrcAsc <= offset)
{
TmpSrcAsc = 255 + TmpSrcAsc - offset;
}
else
{
TmpSrcAsc= TmpSrcAsc - offset;
}
//这里十进制直接转换ascii表不行,待转换为16进制
//char chs = (char)TmpSrcAsc;
string chs = TmpSrcAsc.ToString("X");
ps.Append(chs);
offset = SrcAsc;
SrcPos= SrcPos + 2;
}
return ps.ToString();
}
/// <summary>
/// ascii码转换为中文
/// </summary>
/// <param name="textAscii"></param>
/// <returns></returns>
public string getstring(string textAscii="")
{
string textStr = string.Empty;
int k = 0;//字节移动偏移量
byte[] buffer = new byte[textAscii.Length / 2];//存储变量的字节
for (int i = 0; i < textAscii.Length / 2; i++)
{
//每两位合并成为一个字节
buffer[i] = byte.Parse(textAscii.Substring(k, 2),System.Globalization.NumberStyles.HexNumber);
k = k + 2;
}
//将字节转化成汉字
textStr = Encoding.Default.GetString(buffer);
return textStr;
}
delphi key解密转c# 解决string 不可变长度问题的更多相关文章
- Delphi加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- Linux安装rpm包时报错Header V3 DSA/SHA1 Signature, key ID 1d1e034b: NOKEY解决办法
这是因为yum安装了旧版本的GPG key造成的,解决办法: rpm --import /etc/pki/rpm-gpg/RPM* Header V3 DSA/SHA1 Signature, key ...
- GitHub提示 Error: Key already in use解决办法
GitHub提示 Error: Key already in use解决办法GitHub提示 Error: Key already in use解决办法2014年09月05日 ⁄ 综合 ⁄ 共 290 ...
- 微信 AES 解密报错 Illegal key size 三种解决办法
微信 AES 解密报错 Illegal key size Java 环境 java version "1.8.0_151" Java(TM) SE Runtime Environm ...
- C# .net 填充无效,无法被移除 微信小程序解密失败的解决办法
微信小程序获取用户信息诸如unionId的时候需要解密,如果遇到偶然的解密失败(填充无效,无法被移除),原因很有可能是session_key错误, 也是就你用作解密的session_key并不是微信用 ...
- Delphi中的各种字符串、String、PChar、Char数组
参考博客:http://www.cnblogs.com/pchmonster/archive/2011/12/14/2287686.html 其中的所有代码均在Delphi7下测试通过. Delphi ...
- Error 'Cannot add or update a child row: a foreign key constraint fails故障解决
一大早的,某从库突然报出故障:SQL线程中断! 查看从库状态: mysql> show slave status\G Slave_IO_State: Waiting for master to ...
- vetur插件提示 'v-for' directives require 'v-bind:key' directives.错误的解决办法
在用vscode编写vue代码时,因为安装的有vetur插件,所以当代码中有v-for语法时,会提示 [vue-language-server] 'v-for' directives require ...
- Elements in iteration expect to have 'v-bind:key' directives错误的解决办法
一.错误如下 [eslint-plugin-vue][vue/require-v-for-key]Elements in iteration expect to have 'v-bind:key' d ...
随机推荐
- The comparison between object and constructor
1.相似的地方 1.举个栗子:public struct Student{ string name; int age;}public class bike{ int weight; ...
- NOI 2011 【阿狸的打字机】
之前讲了[AC自动姬],今天我终于把这题给刚下来了...嗯,来给大家讲一讲. 题目描述: 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工 ...
- docker-compose应用
docker-compose应用 需求编写compose模板文件,实现同时启动tomcat.mysql.redis容器. 1.编写模板文件 #新建文件夹mkdir -p /tzh/compose_te ...
- 从源码的角度解析Mybatis的会话机制
坐在我旁边的钟同学听说我精通Mybatis源码(我就想不通,是谁透漏了风声),就顺带问了我一个问题:在同一个方法中,Mybatis多次请求数据库,是否要创建多个SqlSession会话? 可能最近撸多 ...
- MeteoInfoLab脚本示例:风场矢量图
读取风场U/V变量数据,可以从U/V计算出风速:speed = sqrt(u*u+v*v).quiverm函数用来绘制风场矢量图,参数中包括U/V变量,如果要绘制彩色风场还需要第三个变量,这里是风速s ...
- .net 手动建DataTable 获取DataTable列名 修改DataTable 列的顺序
//创建 表 DataTable tables = new DataTable(); //添加 创建 列 //第一列 DataColumn cums = new DataColumn(); cums. ...
- lumen-ioc容器测试 (2)
lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...
- FrameworkElementFactory中的SetBinding与SetValue
public static Microsoft.Windows.Controls.DataGridColumn CreateDateColumn(string path, string header) ...
- 论文学习笔记 - Classifification of Hyperspectral and LiDAR Data Using Coupled CNNs
Classifification of Hyperspectral and LiDAR Data Using Coupled CNNs 来源:IEEE TGRS 2020 下载:https://arx ...
- windows下安装mongodb4.x版本
一个无名前辈的血汗经验,提醒来者 现在mongod出到4.x的版本,而网上的大多数教程是针对3.x的版本的.在4.x的版本中,不要再试图使用自定义安装,我搞了3个多小时都没搞定,如果土豪c盘很大,直接 ...