Jave Common - Library - Encrypt a Password

Submitted by code_admin on Fri, 07/20/2018 - 16:12
  1. import javax.crypto.SecretKey;
  2.  
  3. import org.apache.commons.codec.binary.Base64;
  4.  
  5. import metcarob.com.common.security.EncryptReturn;
  6. import metcarob.com.common.security.SecurityUtilities;
  7. import metcarob.com.common.security.saltPot;
  8.  
  9.  
  10. public class Main {
  11.  
  12.     /**
  13.      * @param args
  14.      */
  15.     public static void main(String[] args) {
  16.         try {
  17.         byte[] salt = {
  18.                 (byte)0x42, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  19.                 (byte)0x43, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  20.                 (byte)0x44, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  21.                 (byte)0x45, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  22.                 (byte)0x46, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  23.                 (byte)0x47, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  24.                 (byte)0x48, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  25.                 (byte)0x49, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  26.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  27.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  28.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  29.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  30.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  31.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  32.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  33.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  34.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  35.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  36.                 (byte)0x41, (byte)0xBB, (byte)0xF7, (byte)0xC8, (byte)0x06, (byte)0xF9, (byte)0x42, (byte)0xD8,
  37.                 (byte)0x21, (byte)0x13, (byte)0x74, (byte)0x34, (byte)0x94, (byte)0x7B, (byte)0x70, (byte)0x99
  38.             };         
  39.         //IV:Qbv3yAb5QthBu/fIBvlC2A==
  40.         //IV:Qbv3yAb5QthBu/fIBvlC2A==
  41.         //IV:Rrv3yAb5QthHu/fIBvlC2A==
  42.  
  43.                
  44.         saltPot m_salt = new saltPot(salt);
  45.         m_salt.allocateSalt(32); //Salt for pass gen
  46.         m_salt.allocateSalt(16); //IC for const IC Encrypt
  47.        
  48.         String m_PrivateReqPass = "fdfsfdsdf334SDSD";
  49.         String m_PrivateReqMsg = "Test Message To Encrypt";
  50.  
  51.         String cipherText;
  52.        
  53.         {
  54.             String enc = "VERIFY" + m_PrivateReqPass;
  55.             SecretKey key = SecurityUtilities.generateKeyFromPassword(m_PrivateReqPass.toCharArray(), m_salt.getSalt(1));
  56.             EncryptReturn er = SecurityUtilities.encryptNoIVSecretKey(key, m_PrivateReqMsg.getBytes(), m_salt.getSalt(2));
  57.             cipherText = new String(Base64.encodeBase64(er.getCipherText()));
  58.         }
  59.        
  60.         System.out.println("BASE64 Cypher=" + cipherText);
  61.        
  62.         {
  63.             SecretKey key = SecurityUtilities.generateKeyFromPassword(m_PrivateReqPass.toCharArray(), m_salt.getSalt(1));
  64.             byte[] cypBYT = Base64.decodeBase64(cipherText);
  65.             String ret = new String(SecurityUtilities.decryptNoIV(key, cypBYT, m_salt.getSalt(2)));
  66.             System.out.println("Ret:" + ret);
  67.         }
  68.        
  69.         } catch (Exception e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.  
  74. }

This creates a saltpot and allocates the first 32 bytes. Here i have indicated where I use it in code

TODO

RJM Article Type
Work Notes