Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.security.MessageDigest;
- import java.security.SecureRandom;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.Base64;
- public class Bunnies {
- public static final long msecsIn24Hours = 86400000L;
- public static final int CS_256_SHA_AES_CTR = 1;
- public static SecureRandom sRand = new SecureRandom();
- static {
- new Timer("re-seed", true).scheduleAtFixedRate(new TimerTask(){
- @Override
- public void run() {
- sRand = new SecureRandom();
- }
- }, msecsIn24Hours, msecsIn24Hours);
- }
- public static String decrypt(byte [] rawInput, byte [] srpKey, int cipherSuite) {
- if (cipherSuite != CS_256_SHA_AES_CTR) {
- throw new IllegalArgumentException("Unsupported cipher suite : " + cipherSuite);
- }
- try {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- // $TODO: note that aesKey can be pre-calculated before, and
- // provided as parameter to this method, to avoid unnecessary hashing
- byte[] aesKey = md.digest(srpKey);
- Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
- int blkSize = cipher.getBlockSize();
- if (rawInput.length <= blkSize) {
- throw new IllegalArgumentException("Too little data");
- }
- cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey, "AES"),
- new IvParameterSpec(rawInput, 0, blkSize));
- return new String(cipher.doFinal(rawInput, blkSize, rawInput.length - blkSize), "UTF-8");
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public static byte [] encrypt(Object rawInput, byte [] srpKey, int cipherSuite) {
- if (cipherSuite != CS_256_SHA_AES_CTR) {
- throw new IllegalArgumentException("Unsupported cipher suite : " + cipherSuite);
- }
- try {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- // $TODO: note that aesKey can be pre-calculated before, and
- // provided as parameter to this method, to avoid unnecessary hashing
- byte[] aesKey = md.digest(srpKey);
- // 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};
- Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
- int blkSize = cipher.getBlockSize();
- byte [] iv = new byte[blkSize];
- // iv = new byte[]{-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1};
- sRand.nextBytes(iv);
- cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey, "AES"),
- new IvParameterSpec(iv));
- byte [] rawBytes;
- if (rawInput instanceof String) {
- rawBytes = ((String)rawInput).getBytes();
- } else {
- rawBytes = (byte[])rawInput;
- }
- byte [] bits = cipher.doFinal(rawBytes);
- byte [] total = new byte[iv.length + bits.length];
- System.arraycopy(iv, 0, total, 0, blkSize);
- System.arraycopy(bits, 0, total, blkSize, bits.length);
- return total;
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public static void main(String a[]) {
- byte [] srpKey = new byte[]{1,2,3,4};
- if ("d".equals(a[0])) {
- byte [] input = Base64.getDecoder().decode(a[1]);
- System.out.println(decrypt(input, srpKey, CS_256_SHA_AES_CTR));
- } else if ("e".equals(a[0])) {
- System.out.println(Base64.getEncoder().encodeToString(encrypt(a[1], srpKey, CS_256_SHA_AES_CTR)));
- } else if ("s".equals(a[0])) {
- 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)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment