password encryption and decryption with Base 64

/*
 * 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 endecrypt;


import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/**
 *
 * @author WeiZhang
 */
public class EnDecrypt {


    /**
     * @param args the command line arguments
     */
    
    
    private static final String ALGO = "AES";
    private static final int ITERATIONS = 2;
    //18 bytes output salt with length 24 with no padding
    private static final int saltSize = 24;
    private static final byte[] keyValue = 
        new byte[] { 'T', 'o', 'e', 'B', 'e', 's', 't',
                    'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
    
    //AES algorithm
    public static String encrypt(String data) throws Exception {
        String salt = generateSalt();
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        String valueToEnc = null; 
        String eValue = data; 
        for (int i = 0; i < ITERATIONS; i++) { 
            valueToEnc = salt + eValue; 
            byte[] encValue = c.doFinal(valueToEnc.getBytes());
            eValue = Base64.getEncoder().encodeToString(encValue);
        } 
        return salt + eValue;
    }
    
    //generate random salt with 18 bytes, output has length 24 in base64 with no padding
    public static String generateSalt() {
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[18];
        random.nextBytes(bytes);
        String s = Base64.getEncoder().encodeToString(bytes);
System.out.println("generateing salt is : " + s.length() + "  " + s);        
        return s;
    }
    
    
    public static String decrypt(String encryptedData) throws Exception {
        String salt = encryptedData.substring(0, saltSize);
        
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        String dValue = null; 
        String valueToDecrypt = encryptedData.substring(saltSize); 
        for (int i = 0; i < ITERATIONS; i++) { 
            byte[] decordedValue = Base64.getDecoder().decode(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, ALGO);
        return key;
    }
    
    public static void main(String[] args) {
        // TODO code application logic here
        String password = "12536";
        String passwordEnc = null, passwordDec = null;
        
        //better choose any string with lentgh 16
        String salt = "choose a simple clear salt";
        
        String test = "cGFzc3dvcmQ=";
        
        
        try {
            passwordEnc = EnDecrypt.encrypt(password);
            passwordDec = EnDecrypt.decrypt(passwordEnc);
//            test = EnDecrypt.decrypt(test);
        } catch (Exception ex) {
            Logger.getLogger(EnDecrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
        


        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
        
//        System.out.println("decode is: " + test);
    }
    
    
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值