Ir al contenido


Foto

[RESUELTO] Alguien sabe si este algoritmo en delphi existe en java


  • Por favor identifícate para responder
6 respuestas en este tema

#1 axesys

axesys

    Advanced Member

  • Moderadores
  • PipPipPip
  • 640 mensajes
  • LocationLos Mochis

Escrito 30 junio 2010 - 12:37

Pues eso si alguien se le hace conocido el código de este algoritmo hecho en delphi como para encontrar uno igual para java


delphi
  1. unit uEncrypt;
  2.  
  3. interface
  4.  
  5. function Decrypt(const S: AnsiString; Key: Word): AnsiString;
  6. function Encrypt(const S: AnsiString; Key: Word): AnsiString;
  7.  
  8. implementation
  9.  
  10. const
  11.   C1      = 52845;
  12.   C2      = 22719;
  13.  
  14. function Decode(const S: ansistring): ansistring;
  15. const
  16.   {$IFDEF DELPHI12}
  17.   Map: array[Ansichar] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53,
  20.     54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
  21.     3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  22.     20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30,
  23.     31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  24.     46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31.     0);
  32.   {$ELSE}
  33.   Map: array[char] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53,
  36.     54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
  37.     3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  38.     20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30,
  39.     31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  40.     46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47.     0);
  48.   {$ENDIF}
  49. var
  50.   I: longint;
  51. begin
  52.   case Length(S) of
  53.     2:
  54.     begin
  55.       I := Map[S[1]] + (Map[S[2]] shl 6);
  56.       SetLength(Result, 1);
  57.       Move(I, Result[1], Length(Result));
  58.     end;
  59.     3:
  60.     begin
  61.       I := Map[S[1]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12);
  62.       SetLength(Result, 2);
  63.       Move(I, Result[1], Length(Result));
  64.     end;
  65.     4:
  66.     begin
  67.       I := Map[S[1]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12) + (Map[S[4]] shl 18);
  68.       SetLength(Result, 3);
  69.       Move(I, Result[1], Length(Result));
  70.     end
  71.   end;
  72. end;
  73.  
  74. function Encode(const S: ansistring): ansistring;
  75. const
  76.   Map: array[0..63] of char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  77. var
  78.   I: longint;
  79. begin
  80.   I := 0;
  81.   Move(S[1], I, Length(S));
  82.   case Length(S) of
  83.     1:
  84.       Result := Map[I mod 64] + Map[(I shr 6) mod 64];
  85.     2:
  86.       Result := Map[I mod 64] + Map[(I shr 6) mod 64] + Map[(I shr 12) mod 64];
  87.     3:
  88.       Result := Map[I mod 64] + Map[(I shr 6) mod 64] + Map[(I shr 12) mod 64] + Map[(I shr 18) mod 64];
  89.   end;
  90. end;
  91.  
  92. function InternalDecrypt(const S: ansistring; Key: Word): ansistring;
  93. var
  94.   I:    Word;
  95.   Seed: int64;
  96. begin
  97.   Result := S;
  98.   Seed  := Key;
  99.   for I := 1 to Length(Result) do
  100.   begin
  101.     {$IFDEF DELPHI12}
  102.       Result[I] := Ansichar(byte(Result[I]) xor (Seed shr 8));
  103.     {$ELSE}
  104.       Result[I] := char(byte(Result[I]) xor (Seed shr 8));
  105.     {$ENDIF}
  106.     Seed      := (byte(S[I]) + Seed) * Word(C1) + Word(C2);
  107.   end;
  108. end;
  109.  
  110. function PreProcess(const S: ansistring): ansistring;
  111. var
  112.   SS: ansistring;
  113. begin
  114.   SS    := S;
  115.   Result := '';
  116.   while SS <> '' do
  117.   begin
  118.     Result := Result + Decode(Copy(SS, 1, 4));
  119.     Delete(SS, 1, 4);
  120.   end;
  121. end;
  122.  
  123. function Decrypt(const S: ansistring; Key: Word): ansistring;
  124. begin
  125.   Result := InternalDecrypt(PreProcess(S), Key);
  126. end;
  127.  
  128. function PostProcess(const S: ansistring): ansistring;
  129. var
  130.   SS: ansistring;
  131. begin
  132.   SS    := S;
  133.   Result := '';
  134.   while SS <> '' do
  135.   begin
  136.     Result := Result + Encode(Copy(SS, 1, 3));
  137.     Delete(SS, 1, 3);
  138.   end;
  139. end;
  140.  
  141. function InternalEncrypt(const S: ansistring; Key: Word): ansistring;
  142. var
  143.   I:    Word;
  144.   Seed: int64;
  145. begin
  146.   Result := S;
  147.   Seed  := Key;
  148.   for I := 1 to Length(Result) do
  149.   begin
  150.     {$IFDEF DELPHI12}
  151.       Result[I] := Ansichar(byte(Result[I]) xor (Seed shr 8));
  152.     {$ELSE}
  153.       Result[I] := char(byte(Result[I]) xor (Seed shr 8));
  154.     {$ENDIF}
  155.     Seed      := (byte(Result[I]) + Seed) * Word(C1) + Word(C2);
  156.   end;
  157. end;
  158.  
  159. function Encrypt(const S: ansistring; Key: Word): ansistring;
  160. begin
  161.   Result := PostProcess(InternalEncrypt(S, Key));
  162. end;
  163.  
  164. {**************************************************************}
  165. // Example:
  166. {**************************************************************}
  167.  
  168. procedure TForm1.Button1Click(Sender: TObject);
  169. const
  170. my_key = 33189;
  171. var
  172.   sEncrypted, sDecrypted :AnsiString;
  173. begin
  174.   // Encrypt a string
  175.   sEncrypted := Encrypt('this is a sample text to encrypt...abcd 123 {}[]?=)=(',my_key);
  176.   // Show encrypted string
  177.   ShowMessage(sEncrypted);
  178.   // Decrypt the string
  179.   sDecrypted := Decrypt(sEncrypted,my_key);
  180.   // Show decrypted string
  181.   ShowMessage(sDecrypted);
  182. end;


  • 0

#2 axesys

axesys

    Advanced Member

  • Moderadores
  • PipPipPip
  • 640 mensajes
  • LocationLos Mochis

Escrito 17 julio 2010 - 03:23

Por si a alguien se le llega a ofrecer aqui les comparto la parte de encriptar del algoritmo delphi hecha en java


java
  1. import java.nio.ByteBuffer;
  2.  
  3. public class Encrypt {
  4.  
  5.     private final int C1 = 52845;
  6.     private final int C2 = 22719;
  7.  
  8.     public String encrypt(final String s, int key) {
  9.         return postProcess(internalEncrypt(s, key));
  10.     }
  11.  
  12.     private byte[] internalEncrypt(final String s, int key) {
  13.         char[] result = s.toCharArray();
  14.         byte[] bytes = new byte[result.length];
  15.         long seed = key;
  16.         for (int i = 0; i < result.length; i++) {
  17.             long l = (byte) result[i] ^ (seed >> 8L);
  18.             int bite = (byte) l & 0xFF;
  19.             System.out.println(bite);
  20.             bytes[i] = (byte) bite;
  21.             seed = (bite + seed) * C1 + C2;
  22.         }
  23.         return bytes;
  24.  
  25.     }
  26.  
  27.     private String postProcess(final byte[] b) {
  28.         String result = "";
  29.         for (int i = 0; i < b.length; i += 3) {
  30.             if ((i + 3) < b.length) {
  31.                 ByteBuffer bb = ByteBuffer.allocate(3);
  32.                 for (int j = 0; j < 3; j++) {
  33.                     bb.put(b[i + j]);
  34.                 }
  35.                 result += encode(bb.array());
  36.             } else {
  37.                 ByteBuffer bb = ByteBuffer.allocate(b.length - i);
  38.                 for (int j = 0; j < b.length - i; j++) {
  39.                     bb.put(b[i + j]);
  40.                 }
  41.                 result += encode(bb.array());
  42.             }
  43.  
  44.         }
  45.         return result;
  46.     }
  47.  
  48.     private String encode(final byte[] b) {
  49.         char map[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  50.             'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  51.             'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  52.             'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
  53.         int i = byteArrayToInt(b);
  54.         StringBuffer result = new StringBuffer("");
  55.         switch (b.length) {
  56.             case 1:
  57.                 result.append(map[i % 64]).append(map[(i >> 6) % 64]);
  58.                 break;
  59.             case 2:
  60.                 result.append(map[i % 64]).append(map[(i >> 6) % 64]).append(map[(i >> 12) % 64]);
  61.                 break;
  62.             case 3:
  63.                 result.append(map[i % 64]).append(map[(i >> 6) % 64]).append(map[(i >> 12) % 64]).append(map[(i >> 18) % 64]);
  64.                 break;
  65.         }
  66.         return result.toString();
  67.     }
  68.  
  69.     private int byteArrayToInt(byte[] b) {
  70.         ByteBuffer bb = ByteBuffer.allocate(4);
  71.         for (int i = 0; i < b.length; i++) {
  72.             System.out.println(b[i] & 0xff);
  73.         }
  74.         byte cero = 0;
  75.         bb.put(cero);
  76.         if (b.length == 3) {
  77.             bb.put(b[2]);
  78.             bb.put(b[1]);
  79.         } else if (b.length == 2) {
  80.             bb.put(cero);
  81.             bb.put(b[1]);
  82.         } else if (b.length == 1) {
  83.             bb.put(cero);
  84.             bb.put(cero);
  85.         }
  86.         bb.put(b[0]);
  87.         return bb.getInt(0);
  88.     }
  89.    
  90. }


  • 0

#3 felipe

felipe

    Advanced Member

  • Administrador
  • 3.283 mensajes
  • LocationColombia

Escrito 19 julio 2010 - 07:42

Parece hasta mas corto.


Saludos!
  • 0

#4 enecumene

enecumene

    Webmaster

  • Administrador
  • 7.419 mensajes
  • LocationRepública Dominicana

Escrito 19 julio 2010 - 08:31

Me tomé la molestia de modificar tu post amigo axesys, ya que en el foro existe la etiqueta para java ;)
  • 0

#5 axesys

axesys

    Advanced Member

  • Moderadores
  • PipPipPip
  • 640 mensajes
  • LocationLos Mochis

Escrito 19 julio 2010 - 03:57

Parece hasta mas corto.


Saludos!


Lo que pasa es que no esta completo solo esta la parte de encriptar todavia falta para desencriptar

Me tomé la molestia de modificar tu post amigo axesys, ya que en el foro existe la etiqueta para java ;)


Ah que bien como no vi el iconito en la barra yo pense que no se podia pero que bueno que si gracias por el tip enecumene


Saludos
  • 0

#6 egostar

egostar

    missing my father, I love my mother.

  • Administrador
  • 14.448 mensajes
  • LocationMéxico

Escrito 19 julio 2010 - 05:02

Y también existe uno que no nos gusta utilizar :D :D :D

[off-topic]
Es que no nos gusta desvirtuar hilos :D :D :D
[/off-topic]


[nobbc]
[off-topic]
Es que no nos gusta desvirtuar hilos :D :D :D
[/off-topic]
[/nobbc]


Salud OS
  • 0

#7 axesys

axesys

    Advanced Member

  • Moderadores
  • PipPipPip
  • 640 mensajes
  • LocationLos Mochis

Escrito 19 julio 2010 - 06:49

[off-topic]
AAAAhhhhhhhhhhhhh si es cierto  :D :D :D
[/off-topic]


Saludos
  • 0




IP.Board spam blocked by CleanTalk.