16 September 2016

Encode Decode Code Base64 - Mã hóa Giải mã Code Base64 Java

1: Encrypt and Decrypt in Java using Base64
Java 2016
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Lonely
 */
public class Base64 {

    private static final String ALGO = "AES";
    private static final byte[] keyValue
            = new byte[]{'T', 'h', 'e', 'B', 'e', 's', 't',
                'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};

    public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }

    public static void main(String[] args) throws Exception {

        String password = "DuongMin";
        String passwordEnc = Base64.encrypt(password);
        String passwordDec = Base64.decrypt(passwordEnc);

        System.out.println("Input Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
    }
}

run:
Input Text : DuongMin
Encrypted Text : d8gf15qLw/Wtf2PPcHkWLQ==
Decrypted Text : DuongMin
BUILD SUCCESSFUL (total time: 2 seconds)

2: Encrypt and Decrypt in Java using Base64
Java 2016
package javaapplication1;

import java.io.IOException;
import java.util.Date;
import java.util.Random;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Main {

    private static Random rand = new Random((new Date()).getTime());

    public static void main(String[] args) throws Exception {

        String st = "secrete";
        String enc = encrypt(st);

        System.out.println("Encrypted string :" + enc);
        System.out.println("Decrypted string :" + decrypt(enc));

    }

    public static String encrypt(String str) {

        BASE64Encoder encoder = new BASE64Encoder();

        byte[] salt = new byte[8];

        rand.nextBytes(salt);

        return encoder.encode(salt) + encoder.encode(str.getBytes());
    }

    public static String decrypt(String encstr) {

        if (encstr.length() > 12) {

            String cipher = encstr.substring(12);

            BASE64Decoder decoder = new BASE64Decoder();

            try {

                return new String(decoder.decodeBuffer(cipher));

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return null;
    }
}
run:
Encrypted string :SnO1NUC1NIc=c2VjcmV0ZQ==
Decrypted string :secrete
BUILD SUCCESSFUL (total time: 0 seconds)

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang