pveselov

crypt example

Aug 24th, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.IvParameterSpec;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.security.MessageDigest;
  5. import java.security.SecureRandom;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8. import java.util.Base64;
  9.  
  10. public class Bunnies {
  11.  
  12.     public static final long msecsIn24Hours = 86400000L;
  13.  
  14.     public static final int CS_256_SHA_AES_CTR = 1;
  15.  
  16.     public static SecureRandom sRand = new SecureRandom();
  17.     static {
  18.         new Timer("re-seed", true).scheduleAtFixedRate(new TimerTask(){
  19.             @Override
  20.             public void run() {
  21.                 sRand = new SecureRandom();
  22.             }
  23.         }, msecsIn24Hours, msecsIn24Hours);
  24.     }
  25.  
  26.     public static String decrypt(byte [] rawInput, byte [] srpKey, int cipherSuite) {
  27.  
  28.         if (cipherSuite != CS_256_SHA_AES_CTR) {
  29.             throw new IllegalArgumentException("Unsupported cipher suite : " + cipherSuite);
  30.         }
  31.  
  32.         try {
  33.  
  34.             MessageDigest md = MessageDigest.getInstance("SHA-256");
  35.             // $TODO: note that aesKey can be pre-calculated before, and
  36.             // provided as parameter to this method, to avoid unnecessary hashing
  37.             byte[] aesKey = md.digest(srpKey);
  38.             Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
  39.  
  40.             int blkSize = cipher.getBlockSize();
  41.  
  42.             if (rawInput.length <= blkSize) {
  43.                 throw new IllegalArgumentException("Too little data");
  44.             }
  45.  
  46.             cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey, "AES"),
  47.                     new IvParameterSpec(rawInput, 0, blkSize));
  48.  
  49.             return new String(cipher.doFinal(rawInput, blkSize, rawInput.length - blkSize), "UTF-8");
  50.  
  51.         } catch (RuntimeException e) {
  52.             throw e;
  53.         } catch (Exception e) {
  54.             throw new RuntimeException(e);
  55.         }
  56.  
  57.     }
  58.  
  59.  
  60.     public static byte [] encrypt(Object rawInput, byte [] srpKey, int cipherSuite) {
  61.  
  62.         if (cipherSuite != CS_256_SHA_AES_CTR) {
  63.             throw new IllegalArgumentException("Unsupported cipher suite : " + cipherSuite);
  64.         }
  65.  
  66.         try {
  67.  
  68.             MessageDigest md = MessageDigest.getInstance("SHA-256");
  69.             // $TODO: note that aesKey can be pre-calculated before, and
  70.             // provided as parameter to this method, to avoid unnecessary hashing
  71.             byte[] aesKey = md.digest(srpKey);
  72.             // aesKey = new byte[]{0x2b,0x7e,0x15,0x16,0x28,(byte)0xae,(byte)0xd2,(byte)0xa6,(byte)0xab,(byte)0xf7,0x15,(byte)0x88,0x9,(byte)0xcf,0x4f,0x3c};
  73.             Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
  74.  
  75.             int blkSize = cipher.getBlockSize();
  76.  
  77.             byte [] iv = new byte[blkSize];
  78.             // iv = new byte[]{-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1};
  79.             sRand.nextBytes(iv);
  80.  
  81.             cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey, "AES"),
  82.                     new IvParameterSpec(iv));
  83.  
  84.             byte [] rawBytes;
  85.            if (rawInput instanceof String) {
  86.                rawBytes = ((String)rawInput).getBytes();
  87.            } else {
  88.                rawBytes = (byte[])rawInput;
  89.            }
  90.             byte [] bits = cipher.doFinal(rawBytes);
  91.             byte [] total = new byte[iv.length + bits.length];
  92.  
  93.             System.arraycopy(iv, 0, total, 0, blkSize);
  94.             System.arraycopy(bits, 0, total, blkSize, bits.length);
  95.             return total;
  96.  
  97.         } catch (RuntimeException e) {
  98.             throw e;
  99.         } catch (Exception e) {
  100.             throw new RuntimeException(e);
  101.         }
  102.  
  103.     }
  104.    
  105.     public static void main(String a[]) {
  106.  
  107.         byte [] srpKey = new byte[]{1,2,3,4};
  108.         if ("d".equals(a[0])) {
  109.             byte [] input = Base64.getDecoder().decode(a[1]);
  110.             System.out.println(decrypt(input, srpKey, CS_256_SHA_AES_CTR));
  111.         } else if ("e".equals(a[0])) {
  112.             System.out.println(Base64.getEncoder().encodeToString(encrypt(a[1], srpKey, CS_256_SHA_AES_CTR)));
  113.         } else if ("s".equals(a[0])) {
  114.             System.out.println(Base64.getEncoder().encodeToString(encrypt(new byte[]{0x6b,(byte)0xc1,(byte)0xbe,(byte)0xe2,0x2e,0x40,(byte)0x9f,(byte)0x96,(byte)0xe9,0x3d,0x7e,0x11,0x73,(byte)0x93,0x17,0x2a}, srpKey, CS_256_SHA_AES_CTR)));
  115.         }
  116.    
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment