· 6 years ago · Jul 01, 2019, 11:42 PM
1private static final String CIPHER_NAME = "DESede";
2private static final String ALGORITHM = "MD5";
3private static final String SECRET_KEY = "p3tr1c0r";
4public static String decrypt(String encryptedText) {
5 String base64EncryptedString;
6 try{
7 byte[] message = Base64.decodeBase64(encryptedText.getBytes(StandardCharsets.UTF_8.name()));
8 MessageDigest md = MessageDigest.getInstance(ALGORITHM);
9 byte[] digestOfPassword = md.digest(SECRET_KEY.getBytes(StandardCharsets.UTF_8.name()));
10 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
11 SecretKey key = new SecretKeySpec(keyBytes, CIPHER_NAME);
12
13 Cipher decipher = Cipher.getInstance(CIPHER_NAME);
14 decipher.init(Cipher.DECRYPT_MODE, key);
15
16 byte[] plainText = decipher.doFinal(message);
17
18 base64EncryptedString = new String(plainText, StandardCharsets.UTF_8.name());
19}catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException ex){
20 log.warn("Exception while trying to decrypt, using encryptedText ");
21 base64EncryptedString = encryptedText;
22 }
23return encryptedText;
24}
25
26let str = "61880868013"
27let buf: [UInt8] = Array(str.utf8)
28
29func MD5(string: String) -> Data {
30 let messageData = string.data(using:.utf8)!
31 var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH))
32
33_ = digestData.withUnsafeMutableBytes {digestBytes in
34 messageData.withUnsafeBytes {messageBytes in
35 CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes)
36 }
37 }
38return digestData
39}