Criptografia...

Delphi

14/04/2009

Olá pessoal, estou finalizando um software em Delphi 7, e preciso disponibilizar arquivos texto (.txt) que são atualizados via web. Esses arquivos são dados técnicos e somente podem ser acessados pelo software em Delphi. Acho que a saída sería criptografar esses arquivos...Alguém sabe de algum sistema de criptografia para arquivos texto que podería ser criptografado e descriptografado pelo meu software em Delphi 7?

Valeu

Arakan


Arakan

Arakan

Curtidas 0

Respostas

Lehapan

Lehapan

14/04/2009

Caro arakan,

você pode utilizar a metodologia de ´Blaise de Vigenère´.

espero ter ajudado.


GOSTEI 0
Arakan

Arakan

14/04/2009

valeu pela dica...procurei no google e encontrei informações mas nada de concreto em como montar o algorítmo, você tem algum material?

Arakan


GOSTEI 0
Lehapan

Lehapan

14/04/2009

da uma olhada nesta unit e veja se te resolve

unit Criptografia; { Baseado na metodologia de "Blaise de Vigenère" }

interface

Uses
  SysUtils, Dialogs;

type
  TMatriz = array of array of string;
  TVetor = array of string;

function  Ret_Criptografia(strOrigem, Tipo: string; Com_Simbolos: Boolean = True): string;

implementation

var
  Linhas, Colunas: integer;

function  Criptografa(strOrigem, strTabNormal, strTabInvertida, Chave: string;
  matTabela: TMatriz): string; forward;
function  Descriptografa(strOrigem, strTabNormal, strTabInvertida, Chave: string;
  matTabela: TMatriz): string; forward;
procedure PreencherMatriz(var matTabela: TMatriz; VetTabela: TVetor); forward;
function  Ret_Coluna(matTabela: TMatriz; Caracter: string): integer; forward;
function  Ret_Linha(matTabela: TMatriz; Caracter: string; Col: integer): integer; forward;


function Ret_Criptografia(strOrigem, Tipo: string; Com_Simbolos: Boolean = True): string;
var
  strTabNormal, strTabInvertida, Chave: string;
  matTabela: TMatriz;
  vetTabela: TVetor;
  x, y: integer;
begin
  Result := ´´;

  strTabNormal := ´ABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789´;
  if  Com_Simbolos then
      strTabNormal := ´Ç~´´!@#$¬^&*()-_=+\|,<.>/?;:´ + Chr(39) + ´[]{}´ +
        Chr(11) + ´ ´ + strTabNormal
  ;

  strTabInvertida := ´´;
  for x := Length(strTabNormal) downto 1 do
  begin
    strTabInvertida := strTabInvertida + strTabNormal[x];
  end;

  Chave := ´AQUI_VOCE_COLOCA_A_SUA_PALAVRA_SECRETA´;

  Linhas := 0;
  Colunas := 0;

  strOrigem := UpperCase( strOrigem );

  SetLength( matTabela, 0 );
  SetLength( vetTabela, 0);

  { Gera tabela de "Blaise de Vigenère" }
  for x := 1 to Length(strTabInvertida) do
  begin
    for y := x to Length(strTabInvertida) do
    begin
      Colunas := Length( vetTabela ) + 1;
      SetLength( vetTabela, Colunas );
      vetTabela[Colunas-1] := Copy( strTabInvertida, y, 1 );
    end;

    for y := 1 to x - 1 do
    begin
      Colunas := Length( vetTabela ) + 1;
      SetLength( vetTabela, Colunas );
      vetTabela[Colunas-1] := Copy( strTabInvertida, y, 1 );
    end;

    PreencherMatriz( matTabela, vetTabela );
    SetLength( vetTabela, 0 );
  end;

  { Tipo:
      C -> Criptografa;
      D -> Descriptografa }
  Tipo := UpperCase( Tipo );
  if  Tipo = ´C´ then
      Result := Criptografa( strOrigem, strTabNormal, strTabInvertida, Chave, matTabela )
  else
    if  Tipo = ´D´  then
        Result := Descriptografa( strOrigem, strTabNormal, strTabInvertida, Chave, matTabela )
    ;
  ;
end;

procedure PreencherMatriz(var matTabela: TMatriz; VetTabela: TVetor);
var
  x: integer;
begin
  SetLength( matTabela, Linhas + 1, Colunas );

  for x := 0 to Colunas - 1 do
  begin
    matTabela[Linhas,x] := VetTabela[x];
  end;

  Inc( Linhas );
end;

function Criptografa(strOrigem, strTabNormal, strTabInvertida, Chave: string;
  matTabela: TMatriz): string;
var
  strInvertida, strChaveada: string;
  Caracter, CaracterChaveado: string;
  x, Posicao, Col, Lin: integer;
begin
  Result := ´´;

  strInvertida := ´´;
  strChaveada := ´´;

  { Inverte a Senha de acordo com ´strTabInvertida´.
    Se o caracter não existir em ´strTabNormal´, coloca o caracter original }
  for x := 1 to Length(strOrigem) do
  begin
    Caracter := Copy( strOrigem, x, 1 );

    Posicao := Pos( Caracter, strTabNormal );

    if  Posicao > 0 then
        strInvertida := strInvertida + Copy( strTabInvertida, Posicao, 1 )
    else
        strInvertida := strInvertida + Caracter
    ;
  end;

  { - Cria a string criptografada com o mesmo tamanho de ´cStrInvertida´.
    - Repete os caracteres da chave para cada caracter que esteja contido em
      ´cStrInvertida´ -> que é a senha com os caracteres trocados pelos
      caracteres de ´strTabInvertida´, e que existam na tabela de Vigenere
      (matTabela), que foi gerada a partir de ´strTabInvertida´ }
  Posicao := 0;

  for x := 1 to Length(strInvertida) do
  begin
    Caracter := Copy( strInvertida, x, 1 );

    { Se o caracter existir em ´strTabInvertida´, é colocado um caracter da
      Chave, senão, coloca o próprio caracter }
    if  Pos( Caracter, strTabInvertida ) = 0 then
        strChaveada := strChaveada + Caracter
    else
      begin
        Inc( Posicao );
        if  Posicao > Length(Chave) then
            Posicao := 1
        ;
        strChaveada := strChaveada + Copy( Chave, Posicao, 1 );
      end;
   ;
  end;

  { Criptografa cada caracter da mensagem, juntamente com seu caracter
    correspondente da chave, conforme a tabela de Vigenere (matTabela) }
  for x := 1 to Length(strInvertida) do
  begin
    Caracter := Copy( strInvertida, x, 1 );
    CaracterChaveado := Copy( strChaveada, x, 1 );

    { Se o caracter existir em ´strTabInvertida´, é feita a pesquisa na tabela
      de Vigenere (matTabela) para encontrar outro caracter, senão coloca o
      próprio caracter }
    if  Pos( Caracter, strTabInvertida ) = 0 then
        Result := Result + Caracter
    else
      begin
        Lin := Ret_Linha( matTabela, CaracterChaveado, 0 );
        Col := Ret_Coluna( matTabela, Caracter );

        Result := Result + matTabela[Lin, Col];
      end
    ;
  end;
end;

function Ret_Coluna(matTabela: TMatriz; Caracter: string): integer;
var
  Col: integer;
begin
  Result := 0;

  Col := 0;
  while Col <= (Colunas - 1) do
  begin
    if  matTabela[0,Col] = Caracter then
      begin
        Result := Col;
        Exit;
      end
    ;

    Inc( Col );
  end;
end;

function  Ret_Linha(matTabela: TMatriz; Caracter: string; Col: integer): integer;
var
  Lin: integer;
begin
  Result := 0;

  Lin := 0;
  while Lin <= (Linhas - 1) do
  begin
    if  matTabela[Col,Lin] = Caracter then
      begin
        Result := Lin;
        Exit;
      end
    ;

    Inc( Lin );
  end;
end;

function Descriptografa(strOrigem, strTabNormal, strTabInvertida, Chave: string; matTabela: TMatriz): string;
var
  strChaveada, strOrig: string;
  Caracter, CaracterOrigem: string;
  x, Posicao, Col, Lin: integer;
begin
  Result := ´´;
  strChaveada := ´´;
  strOrig := ´´;

  { - Cria a string criptografada com o mesmo tamanho de ´strOrigem´.
    - Repete os caracteres da chave para cada caracter que esteja contido em
      ´strOrigem´ -> que é a senha criptografada }
  Posicao := 0;
  for x := 1 to Length(strOrigem) do
  begin
    Caracter := Copy( strOrigem, x, 1 );

    { Se o caracter existir em ´strTabInvertida´, coloca um caracter da Chave,
      senão, coloca o próprio caracter }
    if  Pos( Caracter, strTabInvertida ) = 0 then
        strChaveada := strChaveada + Caracter
    else
      begin
        Inc( Posicao );
        if  Posicao > Length(Chave) then
            Posicao := 1
        ;
        strChaveada := strChaveada + Copy( Chave, Posicao, 1 );
      end
    ;

  end;

  { Descriptografa cada caracter da mensagem criptografada, juntamente com seu
    caracter correspondente da Chave, conforme a tabela de Vigenere (matTabela) }
  for x := 1 to Length(strOrigem) do
  begin
    Caracter  := Copy( strChaveada, x, 1 );
    CaracterOrigem := Copy( strOrigem, x, 1 );

    { Se ´CaracterOrigem´ da senha, existir em ´strTabInvertida´, pesquisa na
      tabela de Vigenere, para encontrar outro caracter, senão, coloca o
      próprio caracter }
    if  Pos( CaracterOrigem, strTabInvertida ) = 0 then
        strOrig := strOrig + CaracterOrigem
    else
      begin
        Col := Ret_Coluna( matTabela, Caracter );
        Lin := Ret_Linha( matTabela, CaracterOrigem, Col );
        strOrig := strOrig + matTabela[Lin, 0];
      end
    ;
  end;

  { Reverte a string (já aplicada a tabela de Vigenere) de acordo com
    ´strTabNormal´, se o caracter não existir em ´strTabInvertida´, coloca o
    caracter original }
  for x := 1 to Length(strOrig) do
  begin
    Caracter := Copy( strOrig, x, 1 );
    Posicao := Pos( Caracter, strTabInvertida );
    if  Posicao > 0  then
        Result := Result + Copy( strTabNormal, Posicao, 1 )
    else
        Result := Result + Caracter
    
  end;
end;

end.


qualquer coisa é só perguntar.


GOSTEI 0
Arakan

Arakan

14/04/2009

Valeu pelo código, era exatamente isso que precisava...

Perfeito

valeu mesmo

Arakan


GOSTEI 0
POSTAR