DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: ahttp123
今日帖子: 28
在线用户: 21
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/10 23:23:08
标题:
求 delphi DES32 浏览:1819
加入我的收藏
楼主: java 的代码是: public static String a(byte[] paramArrayOfByte)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    while (i < paramArrayOfByte.length)
    {
      String str = Integer.toHexString(paramArrayOfByte[i] & 0xFF);
      Object localObject = str;
      if (str.length() < 2)
      {
        localObject = new StringBuilder();
        ((StringBuilder)localObject).append("0");
        ((StringBuilder)localObject).append(str);
        localObject = ((StringBuilder)localObject).toString();
      }
      localStringBuffer.append((String)localObject);
      i += 1;
    }
    return localStringBuffer.toString();
  }
  
  public static byte[] a(String paramString1, String paramString2)
    throws Exception
  {
    Cipher localCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec localDESKeySpec = new DESKeySpec(paramString2.getBytes("UTF-8"));
    localCipher.init(1, SecretKeyFactory.getInstance("DES").generateSecret(localDESKeySpec), new IvParameterSpec(paramString2.getBytes("UTF-8")));
    return localCipher.doFinal(paramString1.getBytes("UTF-8"));
  }
}

输出的是32位加密

我用delphi des输出的都是16位的  现求32位算法
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/11 9:07:35
1楼: 你给一个 
key iv 和加密结果。
我看看,能不能解密。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sail2000 (小帆工作室) ★☆☆☆☆ -
盒子活跃会员
2019/3/11 9:20:01
2楼: 同1楼,给个例子出来才好搞,
例子格式这样子:
源字符:XXXX
密匙:XXXX
结果:XXXX
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/3/11 9:58:25
3楼: des是把8byte(64bit)作为一个Block进行加密,没听说过16位、32位加密。
----------------------------------------------
-
作者:
男 crystalmoon (crystalmoon) ★☆☆☆☆ -
盒子活跃会员
2019/3/11 10:33:43
4楼: 老猫的库有标准des(非固定的)。。。另外,你说的到底是md5还是des?,只有md5才有16,32的说法
----------------------------------------------
-
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/11 12:37:14
5楼: 比如我输入a123456a   上面java的加密是b76ad0906cb0bc416e9c3eaf713346c4
----------------------------------------------
-
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/11 12:39:20
6楼: unit Des;
 
interface
 
uses SysUtils,IniFiles,classes;
 
type
  TKeyByte = array[0..5] of Byte;
  TDesMode = (dmEncry, dmDecry);
  function zz(Str, Key: AnsiString): string;
  function EncryStr(Str, Key: AnsiString): AnsiString;
  function DecryStr(Str, Key: AnsiString): AnsiString;
  function EncryStrHex(Str, Key: AnsiString): AnsiString;
  function DecryStrHex(StrHex, Key: AnsiString): AnsiString;
 
const
  BitIP: array[0..63] of Byte =
    (57, 49, 41, 33, 25, 17,  9,  1,
     59, 51, 43, 35, 27, 19, 11,  3,
     61, 53, 45, 37, 29, 21, 13,  5,
     63, 55, 47, 39, 31, 23, 15,  7,
     56, 48, 40, 32, 24, 16,  8,  0,
     58, 50, 42, 34, 26, 18, 10,  2,
     60, 52, 44, 36, 28, 20, 12,  4,
     62, 54, 46, 38, 30, 22, 14,  6 );
 
  BitCP: array[0..63] of Byte =
    ( 39,  7, 47, 15, 55, 23, 63, 31,
      38,  6, 46, 14, 54, 22, 62, 30,
      37,  5, 45, 13, 53, 21, 61, 29,
      36,  4, 44, 12, 52, 20, 60, 28,
      35,  3, 43, 11, 51, 19, 59, 27,
      34,  2, 42, 10, 50, 18, 58, 26,
      33,  1, 41,  9, 49, 17, 57, 25,
      32,  0, 40,  8, 48, 16, 56, 24 );
 
  BitExp: array[0..47] of Integer =
    ( 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9,10,
      11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,
      21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0  );
 
  BitPM: array[0..31] of Byte =
    ( 15, 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9,
       1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24 );
 
  sBox: array[0..7] of array[0..63] of Byte =
    ( ( 14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
         0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
         4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
        15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13 ),
 
      ( 15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
         3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
         0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
        13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9 ),
 
      ( 10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
        13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
        13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
         1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12 ),
 
      (  7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
        13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
        10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
         3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14 ),
 
      (  2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
        14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
         4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
        11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3 ),
 
      ( 12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
        10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
         9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
         4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 ),
 
      (  4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
        13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
         1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
         6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12 ),
 
      ( 13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
         1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
         7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
         2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11 ) );
 
  BitPMC1: array[0..55] of Byte =
    ( 56, 48, 40, 32, 24, 16,  8,
       0, 57, 49, 41, 33, 25, 17,
       9,  1, 58, 50, 42, 34, 26,
      18, 10,  2, 59, 51, 43, 35,
      62, 54, 46, 38, 30, 22, 14,
       6, 61, 53, 45, 37, 29, 21,
      13,  5, 60, 52, 44, 36, 28,
      20, 12,  4, 27, 19, 11,  3 );
 
  BitPMC2: array[0..47] of Byte =
    ( 13, 16, 10, 23,  0,  4,
       2, 27, 14,  5, 20,  9,
      22, 18, 11,  3, 25,  7,
      15,  6, 26, 19, 12,  1,
      40, 51, 30, 36, 46, 54,
      29, 39, 50, 44, 32, 47,
      43, 48, 38, 55, 33, 52,
      45, 41, 49, 35, 28, 31 );
 
var
  subKey: array[0..15] of TKeyByte;
 
implementation
const KEY='hx12jxfp';
 
procedure initPermutation(var inData: array of Byte);
var
  newData: array[0..7] of Byte;
  i: Integer;
begin
  FillChar(newData, 8, 0);
  for i := 0 to 63 do
    if (inData[BitIP[i] shr 3] and (1 shl (7- (BitIP[i] and $07)))) <> 0 then
      newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
  for i := 0 to 7 do inData[i] := newData[i];
end;
 
procedure conversePermutation(var inData: array of Byte);
var
  newData: array[0..7] of Byte;
  i: Integer;
begin
  FillChar(newData, 8, 0);
  for i := 0 to 63 do
    if (inData[BitCP[i] shr 3] and (1 shl (7-(BitCP[i] and $07)))) <> 0 then
      newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
  for i := 0 to 7 do inData[i] := newData[i];
end;
 
procedure expand(inData: array of Byte; var outData: array of Byte);
var
  i: Integer;
begin
  FillChar(outData, 6, 0);
  for i := 0 to 47 do
    if (inData[BitExp[i] shr 3] and (1 shl (7-(BitExp[i] and $07)))) <> 0 then
      outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;
 
procedure permutation(var inData: array of Byte);
var
  newData: array[0..3] of Byte;
  i: Integer;
begin
  FillChar(newData, 4, 0);
  for i := 0 to 31 do
    if (inData[BitPM[i] shr 3] and (1 shl (7-(BitPM[i] and $07)))) <> 0 then
      newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
  for i := 0 to 3 do inData[i] := newData[i];
end;
 
function si(s,inByte: Byte): Byte;
var
  c: Byte;
begin
  c := (inByte and $20) or ((inByte and $1e) shr 1) or
    ((inByte and $01) shl 4);
  Result := (sBox[s][c] and $0f);
end;
 
procedure permutationChoose1(inData: array of Byte;
  var outData: array of Byte);
var
  i: Integer;
begin
  FillChar(outData, 7, 0);
  for i := 0 to 55 do
    if (inData[BitPMC1[i] shr 3] and (1 shl (7-(BitPMC1[i] and $07)))) <> 0 then
      outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;
 
procedure permutationChoose2(inData: array of Byte;
  var outData: array of Byte);
var
  i: Integer;
begin
  FillChar(outData, 6, 0);
  for i := 0 to 47 do
    if (inData[BitPMC2[i] shr 3] and (1 shl (7-(BitPMC2[i] and $07)))) <> 0 then
      outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;
 
 
procedure cycleMove(var inData: array of Byte; bitMove: Byte);
var
  i: Integer;
begin
  for i := 0 to bitMove - 1 do
  begin
    inData[0] := (inData[0] shl 1) or (inData[1] shr 7);
    inData[1] := (inData[1] shl 1) or (inData[2] shr 7);
    inData[2] := (inData[2] shl 1) or (inData[3] shr 7);
    inData[3] := (inData[3] shl 1) or ((inData[0] and $10) shr 4);
    inData[0] := (inData[0] and $0f);
  end;
end;
 
procedure makeKey(inKey: array of Byte; var outKey: array of TKeyByte);
const
  bitDisplace: array[0..15] of Byte =
    ( 1,1,2,2, 2,2,2,2, 1,2,2,2, 2,2,2,1 );
var
  outData56: array[0..6] of Byte;
  key28l: array[0..3] of Byte;
  key28r: array[0..3] of Byte;
  key56o: array[0..6] of Byte;
  i: Integer;
begin
  permutationChoose1(inKey, outData56);
 
  key28l[0] := outData56[0] shr 4;
  key28l[1] := (outData56[0] shl 4) or (outData56[1] shr 4);
  key28l[2] := (outData56[1] shl 4) or (outData56[2] shr 4);
  key28l[3] := (outData56[2] shl 4) or (outData56[3] shr 4);
  key28r[0] := outData56[3] and $0f;
  key28r[1] := outData56[4];
  key28r[2] := outData56[5];
  key28r[3] := outData56[6];
 
  for i := 0 to 15 do
  begin
    cycleMove(key28l, bitDisplace[i]);
    cycleMove(key28r, bitDisplace[i]);
    key56o[0] := (key28l[0] shl 4) or (key28l[1] shr 4);
    key56o[1] := (key28l[1] shl 4) or (key28l[2] shr 4);
    key56o[2] := (key28l[2] shl 4) or (key28l[3] shr 4);
    key56o[3] := (key28l[3] shl 4) or (key28r[0]);
    key56o[4] := key28r[1];
    key56o[5] := key28r[2];
    key56o[6] := key28r[3];
    permutationChoose2(key56o, outKey[i]);
  end;
end;
 
procedure encry(inData, subKey: array of Byte;
   var outData: array of Byte);
var
  outBuf: array[0..5] of Byte;
  buf: array[0..7] of Byte;
  i: Integer;
begin
  expand(inData, outBuf);
  for i := 0 to 5 do outBuf[i] := outBuf[i] xor subKey[i];
          // outBuf       xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
  buf[0] := outBuf[0] shr 2;          //xxxxxx -> 2
  buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4); // 4 <- xx xxxx -> 4
  buf[2] := ((outBuf[1] and $0f) shl 2) or (outBuf[2] shr 6); //        2 <- xxxx xx -> 6
  buf[3] := outBuf[2] and $3f;          //          xxxxxx
  buf[4] := outBuf[3] shr 2;          //          xxxxxx
  buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4); //          xx xxxx
  buf[6] := ((outBuf[4] and $0f) shl 2) or (outBuf[5] shr 6); //          xxxx xx
  buf[7] := outBuf[5] and $3f;          //          xxxxxx
  for i := 0 to 7 do buf[i] := si(i, buf[i]);
  for i := 0 to 3 do outBuf[i] := (buf[i*2] shl 4) or buf[i*2+1];
  permutation(outBuf);
  for i := 0 to 3 do outData[i] := outBuf[i];
end;
 
procedure desData(desMode: TDesMode;
  inData: array of Byte; var outData: array of Byte);
// inData, outData 都为8Bytes,否则出错
var
  i, j: Integer;
  temp, buf: array[0..3] of Byte;
begin
  for i := 0 to 7 do outData[i] := inData[i];
  initPermutation(outData);
  if desMode = dmEncry then
  begin
    for i := 0 to 15 do
    begin
      for j := 0 to 3 do temp[j] := outData[j];          //temp = Ln
      for j := 0 to 3 do outData[j] := outData[j + 4];         //Ln+1 = Rn
      encry(outData, subKey[i], buf);          //Rn ==Kn==> buf
      for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j];  //Rn+1 = Ln^buf
    end;
 
    for j := 0 to 3 do temp[j] := outData[j + 4];
    for j := 0 to 3 do outData[j + 4] := outData[j];
    for j := 0 to 3 do outData[j] := temp[j];
  end
  else if desMode = dmDecry then
  begin
    for i := 15 downto 0 do
    begin
      for j := 0 to 3 do temp[j] := outData[j];
      for j := 0 to 3 do outData[j] := outData[j + 4];
      encry(outData, subKey[i], buf);
      for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j];
    end;
    for j := 0 to 3 do temp[j] := outData[j + 4];
    for j := 0 to 3 do outData[j + 4] := outData[j];
    for j := 0 to 3 do outData[j] := temp[j];
  end;
  conversePermutation(outData);
end;
 
//////////
 
function EncryStr(Str, Key: AnsiString): AnsiString;
var
  StrByte, OutByte, KeyByte: array[0..7] of Byte;
  StrResult,StrResult1: AnsiString;
  I, J: Integer;
  strLength:integer;
begin
  if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
    raise Exception.Create('Error: the last char is NULL char.');
  if Length(Key) < 8 then
    while Length(Key) < 8 do Key := Key + AnsiChar(0);
 
  strLength:=Length(Str) mod 8;
 
  //while Length(Str) mod 8 <> 0 do Str := Str + AnsiChar(0);
  if Length(Str) mod 8 = 0 then
  begin
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
    Str := Str + AnsiChar(8);
  end
  else
  begin
 
  while Length(Str) mod 8 <> 0 do
  begin
    if strLength=1 then
      Str := Str + AnsiChar(7)
    else if strLength=2 then
      Str := Str + AnsiChar(6)
    else if strLength=3 then
      Str := Str + AnsiChar(5)
    else if strLength=4 then
      Str := Str + AnsiChar(4)
    else if strLength=5 then
      Str := Str + AnsiChar(3)
    else if strLength=6 then
      Str := Str + AnsiChar(2)
    else
      Str := Str + AnsiChar(1)
    //Str := Str + AnsiChar(8 - strLength);
 
  end;
  end;
 
  for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  makeKey(keyByte, subKey);
 
  StrResult := '';
 
  for I := 0 to Length(Str) div 8 - 1 do
  begin
    for J := 0 to 7 do
      StrByte[J] := Ord(Str[I * 8 + J + 1]);
    desData(dmEncry, StrByte, OutByte);
    for J := 0 to 7 do
    begin
      StrResult1 := AnsiChar(OutByte[J]);
      StrResult := StrResult + AnsiChar(OutByte[J]);
    end;
  end;
 
  Result := StrResult;
end;
 
function DecryStr(Str, Key: AnsiString): AnsiString;
var
  StrByte, OutByte, KeyByte: array[0..7] of Byte;
  StrResult: AnsiString;
  I, J: Integer;
begin
  if Length(Key) < 8 then
    while Length(Key) < 8 do Key := Key + AnsiChar(0);
 
  for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
  makeKey(keyByte, subKey);
 
  StrResult := '';
 
  for I := 0 to Length(Str) div 8 - 1 do
  begin
    for J := 0 to 7 do StrByte[J] := Ord(Str[I * 8 + J + 1]);
    desData(dmDecry, StrByte, OutByte);
    for J := 0 to 7 do
      StrResult := StrResult + AnsiChar(OutByte[J]);
  end;
  while (Length(StrResult) > 0) and ((Ord(StrResult[Length(StrResult)]) = 8) or (Ord(StrResult[Length(StrResult)]) = 7) or (Ord(StrResult[Length(StrResult)]) = 6) or (Ord(StrResult[Length(StrResult)]) = 5) or (Ord(StrResult[Length(StrResult)]) = 4) or (Ord(StrResult[Length(StrResult)]) = 3) or (Ord(StrResult[Length(StrResult)]) = 2) or (Ord(StrResult[Length(StrResult)]) = 1) or (Ord(StrResult[Length(StrResult)]) = 0)) do
  begin
    Delete(StrResult, Length(StrResult), 1);
  end;
  Result := StrResult;
end;
 
//////////
 
function EncryStrHex(Str, Key: AnsiString): AnsiString;
var
  StrResult, TempResult, Temp: AnsiString;
  I: Integer;
begin
  TempResult := EncryStr(Str, Key);
  StrResult := '';
  for I := 0 to Length(TempResult) - 1 do
  begin
    Temp := Format('%x', [Ord(TempResult[I + 1])]);
    if Length(Temp) = 1 then Temp := '0' + Temp;
    StrResult := StrResult + Temp;
  end;
  Result := StrResult;
end;
 
function DecryStrHex(StrHex, Key: AnsiString): AnsiString;
  function HexToInt(Hex: AnsiString): Integer;
  var
    I, Res: Integer;
    ch: AnsiChar;
  begin
    Res := 0;
    for I := 0 to Length(Hex) - 1 do
    begin
      ch := Hex[I + 1];
      if (ch >= '0') and (ch <= '9') then
        Res := Res * 16 + Ord(ch) - Ord('0')
      else if (ch >= 'A') and (ch <= 'F') then
        Res := Res * 16 + Ord(ch) - Ord('A') + 10
      else if (ch >= 'a') and (ch <= 'f') then
        Res := Res * 16 + Ord(ch) - Ord('a') + 10
      else raise Exception.Create('Error: not a Hex String');
    end;
    Result := Res;
  end;
 
var
  Str, Temp: AnsiString;
  I: Integer;
begin
  Str := '';
  for I := 0 to Length(StrHex) div 2 - 1 do
  begin
    Temp := Copy(StrHex, I * 2 + 1, 2);
    Str := Str + AnsiChar(HexToInt(Temp));
  end;
  Result := DecryStr(Str, Key);
end;

end.

只能加出是java的一半字符
----------------------------------------------
-
作者:
男 bamboocaep (bamboocaep) ★☆☆☆☆ -
普通会员
2019/3/11 13:08:33
7楼: 输入的字符串需要知道编码。ansi的还是utf8的还是别的
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/3/11 16:01:10
8楼: 你的java DES加密算法是标准算法。你的问题是因为你没有对字符串进行填充就直接拿去加密了。你的字符串‘a123456a长度是8,加密后字符串长度也是8,换成hex字符串,长度当然就是16.

你的java DES加密算法要求填充方式为PKCS5Padding, 这就要求你对加密前的字符串在后面进行填充。如果字符串长度刚好是8的整数倍,就在最后面填8个数值8,否则如果缺N个字符才到8的倍数,则在最后面填N个数值N,如下面所示:
'1' -> '1'#7#7#7#7#7#7#7
'12' -> '12'#6#6#6#6#6#6
'123' -> '123'#5#5#5#5
'1234' -> '1234'#4#4#4
'12345' -> '12345'#3#3
'123456' -> '123456'#2#2
'1234567' -> '1234567'#1
'12345678' -> '12345678'#8#8#8#8#8#8#8#8

相应的,解密过程中,解密后的字符串也要手动剔除填充上去的数值。

你输入了‘a123456a’,刚好是8个字符,java在加密前会在字符串的后面加上8个数值8,这时字符串的长度是16,加密后的字符串长度也是16,转换成hex字符串后字符就是32了。
----------------------------------------------
-
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/11 23:32:49
9楼: UTF-8
----------------------------------------------
-
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/11 23:33:16
10楼: 我试一下
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/3/12 8:36:04
11楼: 对于英文字母、数字、符号来说,utf8和ansi是一样的东西
----------------------------------------------
-
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/12 10:02:56
12楼: 请问上面java填充的是什么字符?是11111111是吧
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/12 10:09:07
13楼: 8 楼 白写了。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/12 10:15:47
14楼: 而且,我也无法验证,我的代码,是不是能解楼主的密码。
不过,算了,反正我代码 是 和 8楼一致的。这就可以了。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/3/12 10:47:55
15楼: 请问你是如何根据8楼的说明,得出要填充1111111这个结论的?
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/12 11:01:50
16楼: 拍脑袋呗。一定是当领导的料。
填充永远是 N 个 N 。N 一般最多是8,最小是1。,没有 0。
但是我喜欢 填充 N 个字节,第 N 个的值是 N,1 到 N-1的值随便 来一个。
这样就是 随机 加密结果。
当需要 填充 1 的时候,是没有随机效果的。

LockBox 的 RSA 也是这么干的,结果别人无法解密他的。

大多数的无填充模式,并不是不填充,而是 填充 0 到 N-1 个 0 。
比如 你 是 7 个,就填充一个 0。
8 个不填充
9 个填充 7 个 0。

有的算法是支持完全不填充的。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 crystalmoon (crystalmoon) ★☆☆☆☆ -
盒子活跃会员
2019/3/12 13:47:01
17楼: 既然java用的是CBC/PKCS5Padding模式,你首先得知道 他的pass和iv设置的是多少。。。
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/12 14:43:54
18楼: 连填充他都不会 IV 他就更不知道了。
很多人 iv 喜欢用 字符串,其实人家是字节数组。
密码也是。
用字符串,最好都用 ANSI 的 OEM 字符,不能用汉字等。否则 就各种错误 吧。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 liu494021458 (eedc) ★☆☆☆☆ -
普通会员
2019/3/13 7:16:24
19楼: 看来我是解不了!!!!有没那个大神给段代码!Q286888199
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/13 9:38:26
20楼: 这不是一段代码能解决的。
反正 你都不配合。我也就无法证明,我的代码能用。
既然无法证明,我就不发了。
虽然是 sf.net 上的东西。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/3/13 15:20:03
21楼: function DesEncry_CBC_PKCS5(Str, Key, IV: String): String;
var
  I, J, M: Integer;
  InData, KeyData, IVData, OutData: array[0..7] of byte ;
begin
  Result := '';

  M := 8 - (Length(Str) mod 8);
  for I := 1          to M do Str := Str + Chr(M);
  for I := Length(Key) to 7 do Key := Key + #0;
  for I := Length(IV ) to 7 do IV  := IV  + #0;

  for I := 0 to 7 do
  begin
    KeyData[I] := ord(Key[I+1]);
    IVData [I] := ord(IV [I+1]);
  end;
  MakeKey(KeyData, SubKey);

  for I := 0 to (Length(Str) div 8) - 1  do
  begin
    for J := 0 to 7 do
    begin
      InData[J] := ord(Str[I*8+J+1]) xor IVData[J];
    end;
    DesData(dmEncry, InData, OutData);
    for J := 0 to 7 do
    begin
      IVData[J] := OutData[J];
    end;
    for J := 0 to 7 do
    begin
      Result := Result + IntToHex(OutData[J], 2);
    end;
  end;
end;
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/3/13 15:39:36
22楼: 楼上的 string 没有编码。
建议用 TBytes 代替。
----------------------------------------------
(C)(P)Flying Wang
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行101.5625毫秒 RSS