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; }
/** * 加密 * * @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; }
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; }
/** * 加密字节数组 * * @param arrB 需加密的字节数组 * @return 加密后的字节数组 * @throws Exception */ public byte[] encrypt(byte[] arrB) { try { return encryptCipher.doFinal(arrB); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return arrB; }
private static byte[] func_75885_a(int p_75885_0_, Key p_75885_1_, byte p_75885_2_[]) { try { return func_75886_a(p_75885_0_, p_75885_1_.getAlgorithm(), p_75885_1_).doFinal(p_75885_2_); } catch (IllegalBlockSizeException illegalblocksizeexception) { illegalblocksizeexception.printStackTrace(); } catch (BadPaddingException badpaddingexception) { badpaddingexception.printStackTrace(); } System.err.println("Cipher data 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]; }
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; }