Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

String to string algoritam za enkripciju

[es] :: Java :: String to string algoritam za enkripciju

[ Pregleda: 1984 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

maxer
Logoriste

Član broj: 46427
Poruke: 132
*.dynamic.sbb.rs.



+1 Profil

icon String to string algoritam za enkripciju27.01.2011. u 17:13 - pre 161 meseci
Zna li neko neki prost algoritam za enkripciju i dekripciju stringa u string (npr. "blabla" => "asd821378as8asd8asd987" ), da pritom koristi neki mali prosti kljuc, i da nije previse skupa operacija. Koristio bih ovo samo za sakrivanje oblika nekih stringova a ne za cuvanje nekih previse vaznih podataka, a pritom ne bi valjalo da ta operacij bude previse skupa (to je i razlog zasto ne koristim java.security paket).
YoYo
 
Odgovor na temu

MMX
Miloš Malović
Platform engineer, Supplyframe
Beograd

SuperModerator
Član broj: 2423
Poruke: 2105
212.178.244.*

Jabber: mmx@elitesecurity.org
ICQ: 98797759
Sajt: www.mmx.rs


+11 Profil

icon Re: String to string algoritam za enkripciju30.01.2011. u 14:07 - pre 160 meseci
Zašto bi uopšte koristio bilo koji algoritam kada možeš da koristiš SSL socket?
↑ ↑ ↓ ↓ ← → ← → B A B A [select] [start]
 
Odgovor na temu

maxer
Logoriste

Član broj: 46427
Poruke: 132
*.dynamic.sbb.rs.



+1 Profil

icon Re: String to string algoritam za enkripciju31.01.2011. u 11:01 - pre 160 meseci
Pa ne znam tacno kako radi taj SSL Socket, ali predpostavljam da se preko njega salju podaci na siguran nacin. Meni to ne treba, jednostavno hocu da sakrijem izgled nekog srting koji necu nigde da saljem.
YoYo
 
Odgovor na temu

dejanet
Beograd

Član broj: 19240
Poruke: 1181



+836 Profil

icon Re: String to string algoritam za enkripciju31.01.2011. u 11:14 - pre 160 meseci
Code:
import java.io.*;
import java.net.*;
import java.security.*;
import java.security.spec.*;

import java.util.Date;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;
public class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;
    
    DesEncrypter(SecretKey key) {
        try {
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);
            
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }
    
    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);
            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
            //return getHexString(enc);
            
        } catch (javax.crypto.BadPaddingException e) {
            System.out.println(e);
        } catch (IllegalBlockSizeException e) {
            System.out.println(e);
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
            //} catch (java.io.IOException e) {
            //    System.out.println(e);
        }
        return null;
    }
    
    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
            
            // Decode using utf-8
            return new String(utf8, "UTF8");
            //return getHexString(utf8);
        } catch (javax.crypto.BadPaddingException e) {
            System.out.println(e);
        } catch (IllegalBlockSizeException e) {
            System.out.println(e);
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
        } catch (java.io.IOException e) {
            System.out.println(e);
        }
        return null;
    }
    
    public void testCrypt(){
        try {
            // Generate a temporary key. In practice, you would save this key.
            // See also e464 Encrypting with DES Using a Pass Phrase.
            SecretKey key = KeyGenerator.getInstance("DES").generateKey();
            
            // Create encrypter/decrypter class
            DesEncrypter encrypter = new DesEncrypter(key);
            
            // Encrypt
            String encrypted = encrypter.encrypt("Don't tell anybody!");
            System.out.println("encrypted="+encrypted);
            
            // Decrypt
            String decrypted = encrypter.decrypt(encrypted);
            System.out.println("decrypted="+decrypted);
            
        } catch (Exception e) {
        }
    }
}


Probaj ovaj DES crypter/decrypter, ja sam ga koristio za passworde u npr. user tabeli..
Takodje ti ne trebaju svi importi(zbog copy paste iz dela mog koda)..
 
Odgovor na temu

maxer
Logoriste

Član broj: 46427
Poruke: 132
*.dynamic.sbb.rs.



+1 Profil

icon Re: String to string algoritam za enkripciju01.02.2011. u 08:45 - pre 160 meseci
Radi. Hvala na odgovoru. Pozdrav
YoYo
 
Odgovor na temu

[es] :: Java :: String to string algoritam za enkripciju

[ Pregleda: 1984 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.