62,620
社区成员
发帖
与我相关
我的任务
分享using System;
namespace CEWebService
{
/// <summary>
/// 字符串加密类
/// </summary>
public class Encryption
{
//机密密钥
private static Byte[] XorKey = { 0xB2, 0x09, 0xAA, 0x55, 0x93, 0x6D, 0x84, 0x47 };
/// <summary>
/// 字符串加密
/// </summary>
/// <param name="Str">明文</param>
/// <returns>密文</returns>
public static string Enc(string Str)
{
string res = "";
int j = 0;
foreach (char c in Str)
{
res += (Convert.ToByte(c) ^ XorKey[j]).ToString("X");
j = (j + 1) % 8;
}
return res;
}
/// <summary>
/// 字符串解密
/// </summary>
/// <param name="Str">密文</param>
/// <returns>明文</returns>
public static string Denc(string Str)
{
string res = "";
int j = 0;
for (int i = 1; i <= Str.Length / 2; i++)
{
res += Convert.ToChar((Convert.ToInt32(("0x" + Str.Substring(i * 2 - 1, 2))) ^ XorKey[j]));
j = (j + 1) % 8;
}
return res;
}
}
}
unit uPubStrChg;
interface
uses
SysUtils;
const
XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47);
function Enc(Str: string): string;
function Denc(Str: string): string;
implementation
function Enc(Str: string): string;
var
i, j: Integer;
begin
Result := '';
j := 0;
for i := 1 to Length(Str) do
begin
Result := Result + IntToHex(Byte(Str[i]) xor XorKey[j], 2);
j := (j + 1) mod 8;
end;
end;
function Denc(Str: string): string;
var
i, j: Integer;
begin
Result := '';
j := 0;
for i := 1 to Length(Str) div 2 do
begin
Result := Result + Char(StrToInt('$' + Copy(Str, i * 2 - 1, 2)) xor
XorKey[j]);
j := (j + 1) mod 8;
end;
end;
end.
public static String Enc(String Str) {
String res = "";
int j = 0;
for (char c : Str.toCharArray()) {
res += String.valueOf(Integer.toHexString((byte) c ^ XorKey[j])).substring(6);
j = (j + 1) % 8;
}
return res;
}
public class Encryption {
private static byte[] XorKey = { (byte) 0xB2, (byte) 0x09, (byte) 0xAA,
(byte) 0x55, (byte) 0x93, (byte) 0x6D, (byte) 0x84, (byte) 0x47 };
public static String Enc(String Str) {
String res = "";
int j = 0;
for (char c : Str.toCharArray()) {
res += String.valueOf((byte) c ^ XorKey[j]);
j = (j + 1) % 8;
}
return res;
}
public static String Denc(String Str) {
String res = "";
int j = 0;
for (int i = 1; i <= Str.length() / 2; i++) {
res += (char) (Integer
.parseInt(("0x" + Str.substring(i * 2 - 1, 2))) ^ XorKey[j]);
j = (j + 1) % 8;
}
return res;
}
}