Página 1 de 1

Sistema de criptografia utilizando numeros aleatorios

Enviado: 08 Out 2012 13:20
por bencz
Olá (:
Estava este fim de semana, sem fazer nada, e, desenvolvi um sistema de criptografia, utilizando numeros aleatorios (:
Bom, vou colocar aqui o código, para quem queira utilizar (:

Código: Selecionar todos

#include "hbclass.ch"

function start
   local tempGet   
   local test := EnCrypt():New()
   
   ? "Palavra criptografada: Ola Mundo!!"
   test:SetTextToEncrypt("Ola Mundo!!")
   test:EnCrypt()
   ? "Ola Mundo!! : criptografado:  " + (tempGet := test:GetEncryptedText())
   
   
   ? "Descriptografacao do Ola Mundo!! -> " + tempGet
   test:SetTextToDecrypt(tempGet)
   test:Decrypt()
   ? test:GetDecryptedText()
return nil

class EnCrypt
private:
   data cEncryptedText   init ""
   data cDecryptedText   init ""
   
   data cTextToEncrypt   init ""
   data cTextToDecrypt   init ""
   
public:
   method new() constructor
   method EnCrypt()
   method Decrypt()
   
   method SetTextToEncrypt( cText ) inline( ::cTextToEncrypt := cText )
   method SetTextToDecrypt( cText ) inline( ::cTextToDecrypt := cText )
   
   inline method GetEncryptedText()
      return ::cEncryptedText
   endmethod
   
   inline method GetDecryptedText()
      return ::cDecryptedText
   endmethod
endclass

method new() class encrypt ; return self

method EnCrypt() class encrypt
   local i
   local cRet := ""
   
   local nRand1
   local nRand2
   
   for i:=1 to len(::cTextToEncrypt)
      nRand1 := HB_RandomInt() % 2
      nRand2 := HB_RandomInt() % 15
      
      cRet += Chr(AscPos(::cTextToEncrypt[i]) + nRand2)
      
      if nRand1 == 0
         cRet += Chr(97)
         cRet += Chr((48+nRand2))
      else
         cRet += Chr(48)
         cRet += Chr((97+nRand2))
      endif
   next   
   ::cEncryptedText := cRet  
return nil

method Decrypt() class encrypt
   local i
   local nAux := 0
   local cRet := ""
   
   for i:=1 to len(::cTextToDecrypt)
      nAux := 0
      
      if AscPos(::cTextToDecrypt[i+1]) == 97
         nAux := AscPos(::cTextToDecrypt[i+2]) - 48
         cRet += Chr(AscPos(::cTextToDecrypt[i]) - nAux)
      endif
      
      if AscPos(::cTextToDecrypt[i+1]) == 48
         nAux := AscPos(::cTextToDecrypt[i+2]) - 97
         cRet += Chr(AscPos(::cTextToDecrypt[i]) - nAux)
      endif
      
      i+=2
   next     
      
   ::cDecryptedText := cRet
return nil
(:

Sistema de criptografia utilizando numeros aleatorios

Enviado: 11 Out 2012 20:04
por alxsts
Olá!

Muito bom e simples.

Grato por compartilhar.

Sistema de criptografia utilizando numeros aleatorios

Enviado: 12 Out 2012 02:11
por bencz
muito obrigado (:

quando eu criei, fiz alguns testes tipo: salvei 50000 criptografias da palavra "Ola Mundo!!" em um arquivo, e nao tinha uma igual a outra...
e na descriptografia, ele descriptografava certinho (: