ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

Encryption / Decryption


  • Please log in to reply
Encryption / Decryption

#1

marcshahine
marcshahine
    Offline
    0
    Rep
    4
    Likes

    New Member

  • PipPip
Posts: 17
Threads: 1
Joined: Feb 26, 2020
Credits: 0

Four years registered
#1

THIS WILL USE SALT ENCRYPTION METHOD.

 

1rst file: Protector.java

import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
 
public class Protector {
 
    private static final String ALGORITHM = "AES";
    private static final int ITERATIONS = 5;
    private static final byte[] keyValue =
            new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S',
                    'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
 
    public static String encrypt(String value, String salt) 
                                            throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
 
        String valueToEnc = null;
        String eValue = value;
        for (int i = 0; i < ITERATIONS; i++) {
            valueToEnc = salt + eValue;
            byte[] encValue = c.doFinal(valueToEnc.getBytes());
            eValue = new BASE64Encoder().encode(encValue);
        }
        return eValue;
    }
 
    public static String decrypt(String value, String salt) 
                                            throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
 
        String dValue = null;
        String valueToDecrypt = value;
        for (int i = 0; i < ITERATIONS; i++) {
            byte[] decordedValue 
                    = new BASE64Decoder().decodeBuffer(valueToDecrypt);
            byte[] decValue = c.doFinal(decordedValue);
            dValue = new String(decValue).substring(salt.length());
            valueToDecrypt = dValue;
        }
        return dValue;
    }
 
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        // SecretKeyFactory keyFactory 
        //                  = SecretKeyFactory.getInstance(ALGORITHM);
        // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
        return key;
    }
}

 

2nd file: TestProtector.java (The one to compile and run)

public class TestProtector {
 
    public static void main(String[] args) throws Exception {
        String password = "";
        String salt = "";
        String passwordEnc = Protector.encrypt(password, salt);
        String passwordDec = Protector.decrypt(passwordEnc, salt);
 
        System.out.println("Salt Text : " + salt);
        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted : " + passwordEnc);
        System.out.println("Decrypted : " + passwordDec);
    }
}

 


  • 3

#2

Blocus
Blocus
    Offline
    0
    Rep
    16
    Likes

    Advanced Member

Posts: 107
Threads: 6
Joined: Feb 27, 2020
Credits: 0

Four years registered
#2

good work !


  • 0

#3

Mohamed139
Mohamed139
    Offline
    0
    Rep
    19
    Likes

    Junkie

Posts: 348
Threads: 10
Joined: Feb 26, 2020
Credits: 0

Four years registered
#3

Broo

Thank U


  • 0

#4

hosh01234
hosh01234
    Offline
    0
    Rep
    0
    Likes

    Addicted

  • PipPipPipPipPip
Posts: 192
Threads: 0
Joined: Jan 28, 2020
Credits: 0

Four years registered
#4

ty


  • 0

#5

tinacg
tinacg
    Offline
    -2
    Rep
    0
    Likes

    Addicted

  • PipPipPipPipPip
Posts: 189
Threads: 0
Joined: Dec 26, 2019
Credits: 0
Four years registered
#5
Nice one.. Love your posts

  • 0

#6

Hoolahookk
Hoolahookk
    Offline
    0
    Rep
    1
    Likes

    Advanced Member

  • PipPipPipPip
Posts: 139
Threads: 1
Joined: Apr 11, 2020
Credits: 0
Four years registered
#6
Hope everything works out

  • 0

#7

Indian23
Indian23
    Offline
    0
    Rep
    0
    Likes

    New Member

Posts: 11
Threads: 0
Joined: Apr 17, 2020
Credits: 0
Four years registered
#7

ty


  • 0

#8

Sunmughan
Sunmughan
    Offline
    0
    Rep
    1
    Likes

    New Member

Posts: 17
Threads: 0
Joined: Aug 07, 2018
Credits: 0
Five years registered
#8
??????

  • 0

#9

DiabloDoggo
DiabloDoggo
    Offline
    0
    Rep
    1
    Likes

    Member

Posts: 28
Threads: 4
Joined: Jun 24, 2020
Credits: 0
Three years registered
#9

Good work man :pepoclap:


  • 0

#10

McFakie
McFakie
    Offline
    0
    Rep
    3
    Likes

    New Member

Posts: 19
Threads: 2
Joined: Oct 23, 2019
Credits: 0
Four years registered
#10
Thank you

  • 0


 Users browsing this thread: