' Gambas class file
' Classe creata per criptare e decriptare per mezzo di chiave piccole stringhe
' By Claudio Bernardini
' claudio69@infinito.it
PUBLIC FUNCTION Cod(testo AS String, chiave AS String) AS String
DIM aKey AS NEW Integer[]
DIM aTxt AS NEW Integer[]
DIM a, b AS Integer
DIM iCodice AS Integer
DIM sCodice AS String
DIM bCodP AS String
'converte chiave in array di numeri
FOR a = 1 TO Len(chiave)
aKey.Add(Asc(Mid$(chiave, a, 1)))
NEXT
'converte il testo in array di numeri
FOR a = 1 TO Len(testo)
aTxt.Add(Asc(Mid$(testo, a, 1)))
NEXT
'crea codice
FOR a = 0 TO Len(testo) - 1
FOR b = 0 TO Len(chiave) - 1
bCodP = (aTxt[a] XOR aKey[b])
IF Len(bCodP) < 2 THEN
sCodice &= "0" & bCodP
ELSE
sCodice &= bCodP
ENDIF
NEXT
NEXT
RETURN sCodice
END
PUBLIC FUNCTION Decod(codice AS String, chiave AS String) AS String
DIM sCod AS NEW Integer[]
DIM aKey AS NEW Integer[]
DIM a, b AS Integer
DIM sTxt AS String
'converte chiave in array di numeri
FOR a = 0 TO (Len(codice) / 2) - 1
b = b + 1
IF b > Len(chiave) THEN
b = 1
END IF
aKey.Add(Asc(Mid$(chiave, b, 1)))
NEXT
'riempie array con coppie di valori della chiave
FOR a = 1 TO Len(codice) STEP 2
sCod.Add(Mid$(codice, a, 2))
NEXT
'decodifica codice
b = 1
FOR a = 0 TO (Len(codice) / 2) - 1
DEC b
IF b = 0 THEN
sTxt &= Chr$(sCod[a] XOR aKey[a])
b = Len(chiave)
ENDIF
NEXT
RETURN sTxt
END
La metto quì, magari può servire a qualcuno,Bravo. Bene. In questo Forum così ci si comporta: in modo solidale. :coder:
Salve; siccome girando per internet non ho trovato niente che potesse servirmi, mi sono messo alla tastiera ed è uscito questo:Codice: [Seleziona]' Gambas class file
' Classe creata per criptare e decriptare per mezzo di chiave piccole stringhe
' By Claudio Bernardini
' claudio69@infinito.it
PUBLIC FUNCTION Cod(testo AS String, chiave AS String) AS String
DIM aKey AS NEW Integer[]
DIM aTxt AS NEW Integer[]
DIM a, b AS Integer
DIM iCodice AS Integer
DIM sCodice AS String
DIM bCodP AS String
'converte chiave in array di numeri
FOR a = 1 TO Len(chiave)
aKey.Add(Asc(Mid$(chiave, a, 1)))
NEXT
'converte il testo in array di numeri
FOR a = 1 TO Len(testo)
aTxt.Add(Asc(Mid$(testo, a, 1)))
NEXT
'crea codice
FOR a = 0 TO Len(testo) - 1
FOR b = 0 TO Len(chiave) - 1
bCodP = (aTxt[a] XOR aKey[b])
IF Len(bCodP) < 2 THEN
sCodice &= "0" & bCodP
ELSE
sCodice &= bCodP
ENDIF
NEXT
NEXT
RETURN sCodice
END
PUBLIC FUNCTION Decod(codice AS String, chiave AS String) AS String
DIM sCod AS NEW Integer[]
DIM aKey AS NEW Integer[]
DIM a, b AS Integer
DIM sTxt AS String
'converte chiave in array di numeri
FOR a = 0 TO (Len(codice) / 2) - 1
b = b + 1
IF b > Len(chiave) THEN
b = 1
END IF
aKey.Add(Asc(Mid$(chiave, b, 1)))
NEXT
'riempie array con coppie di valori della chiave
FOR a = 1 TO Len(codice) STEP 2
sCod.Add(Mid$(codice, a, 2))
NEXT
'decodifica codice
b = 1
FOR a = 0 TO (Len(codice) / 2) - 1
DEC b
IF b = 0 THEN
sTxt &= Chr$(sCod[a] XOR aKey[a])
b = Len(chiave)
ENDIF
NEXT
RETURN sTxt
END
Questa funzione serve per criptare e decriptare stringhe per mezzo di una chiave.
La metto quì, magari può servire a qualcuno, oppure qualcuno potrebbe provarla e consigliarmi qualche buona modifica.
Grazie.
bisognerebbe capir ecosa devi realizzare,
solitamente si cripta una password in modo tale che non possa essere decriptata, per questo gambas fornisce il componente gb.crypt (che devi abilitare)
con la funzione :
passcriptata=crypt.md5(password)
la stringa che ottieni la salvi sul file di testo.
poi se devi usare la pass come accesso al tuo programma , fai digitare la password dall' utente e verifichi che sia uguale a quella cryptata con
Crypt.Check(password_da_verificare, stringa con password criptata)
che ti ritorna true se la pass è valida e false se non lo è
per criptare in modo reversibile devi trovare un programma esterno , visto che gambas non lo fa