Example #1
0
 /**
  * 加密
  *
  * @param content 需要加密的内容
  * @param password 加密密码
  * @return
  */
 public static byte[] encryptAES(String content, String password) {
   try {
     KeyGenerator kgen = KeyGenerator.getInstance("AES");
     kgen.init(128, new SecureRandom(password.getBytes()));
     SecretKey secretKey = kgen.generateKey();
     byte[] enCodeFormat = secretKey.getEncoded();
     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
     Cipher cipher = Cipher.getInstance("AES"); // 创建密码器
     byte[] byteContent = content.getBytes("utf-8");
     cipher.init(Cipher.ENCRYPT_MODE, key); // 初始化
     byte[] result = cipher.doFinal(byteContent);
     return result; // 加密
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   }
   return null;
 }
Example #2
0
  public static String decryptIt(String value) {
    try {
      DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey key = keyFactory.generateSecret(keySpec);

      byte[] encrypedPwdBytes = Base64.decode(value, Base64.DEFAULT);
      // cipher is not thread safe
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));

      String decrypedValue = new String(decrypedValueBytes);
      Log.d(TAG, "Decrypted: " + value + " -> " + decrypedValue);
      return decrypedValue;

    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (InvalidKeySpecException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    }
    return value;
  }
Example #3
0
  private static byte[] doAes(int mode, byte[] input, String password) {
    try {
      KeyGenerator keygen = KeyGenerator.getInstance(AES_ALGORITHM_NAME);
      keygen.init(128, new SecureRandom(password.getBytes()));
      SecretKey secretKey = keygen.generateKey();

      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES_ALGORITHM_NAME);

      Cipher cipher = Cipher.getInstance(AES_ALGORITHM_NAME);
      cipher.init(Cipher.DECRYPT_MODE, key);

      return cipher.doFinal(input);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    return null;
  }
 private static Cipher func_75886_a(int p_75886_0_, String p_75886_1_, Key p_75886_2_) {
   try {
     Cipher cipher = Cipher.getInstance(p_75886_1_);
     cipher.init(p_75886_0_, p_75886_2_);
     return cipher;
   } catch (InvalidKeyException invalidkeyexception) {
     invalidkeyexception.printStackTrace();
   } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
     nosuchalgorithmexception.printStackTrace();
   } catch (NoSuchPaddingException nosuchpaddingexception) {
     nosuchpaddingexception.printStackTrace();
   }
   System.err.println("Cipher creation failed!");
   return null;
 }
 public static byte[] encryptData(Key key, byte[] toEncrypt) {
   try {
     Cipher cipher = Cipher.getInstance(key.getAlgorithm());
     cipher.init(Cipher.ENCRYPT_MODE, key);
     return cipher.doFinal(toEncrypt);
   } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();
   } catch (NoSuchPaddingException e) {
     e.printStackTrace();
   } catch (InvalidKeyException e) {
     e.printStackTrace();
   } catch (BadPaddingException e) {
     e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
     e.printStackTrace();
   }
   return new byte[0];
 }
Example #6
0
  public Object transformMessage(MuleMessage message, String outputEncoding)
      throws TransformerException {

    Arrays.fill(ivBytes, (byte) 0x00);
    iv = new IvParameterSpec(ivBytes);

    Object derivedKey = message.getInvocationProperty("derivedKey");
    if (null == derivedKey) {
      keyValue = Base64.decodeBase64(DEFAULT_KEY.getBytes());
    } else {
      keyValue = Base64.decodeBase64(derivedKey.toString().getBytes());
    }
    SecretKeySpec skeySpec = new SecretKeySpec(keyValue, "AES");

    Cipher cipher = null;
    try {
      cipher = Cipher.getInstance(ALGO);
    } catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
    } catch (NoSuchPaddingException e1) {
      e1.printStackTrace();
    }

    try {
      cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    byte[] original = null;
    try {
      // Object queryParm1 = message.getPayload().toString());	//"2zU3YSkvbamjrlE7WgBaKA==");
      // Object queryParm1 = message.getInvocationProperty("1");

      // HashMap queryParms = (HashMap)message.getInboundProperty("http.query.params");
      // Object queryParm1 = queryParms.get("1");

      Object queryParm1 = message.getInvocationProperty("encryptedPatientId");

      if (queryParm1 != null) {
        byte[] encString = Base64.decodeBase64(queryParm1.toString().getBytes());
        original = cipher.doFinal(encString);

        String clearText = "";
        try {
          clearText = new String(original, UNICODE_FORMAT).trim();
          // message.setInvocationProperty("patientId", clearText);
          message.setOutboundProperty("patientId", clearText);

        } catch (UnsupportedEncodingException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }

    } catch (IllegalBlockSizeException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (BadPaddingException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    return message;
  }