[ maxer @ 27.01.2011. 17:13 ] @
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).
[ MMX @ 30.01.2011. 14:07 ] @
Zašto bi uopšte koristio bilo koji algoritam kada možeš da koristiš SSL socket?
[ maxer @ 31.01.2011. 11:01 ] @
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.
[ dejanet @ 31.01.2011. 11:14 ] @
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)..
[ maxer @ 01.02.2011. 08:45 ] @
Radi. Hvala na odgovoru. Pozdrav