{
"responseCode": 200,
"responseMsg": "Success",
"driverData": {
"id": "DR220929.1151.000001",
},
"token": "JHBKQWFBFJS55JZWBKEL"
}
encrypted_and_encoded_token| Parameter | Value |
|---|---|
| Cipher Algorithm | AES/CBC/PKCS5Padding |
| Encryption Algorithm | PBKDF2WithHmacSHA1 |
| Encryption Key | 61085G5GH96QW96D |
| Password | PS606AA85DRG3J93800Q9L90672IYV50 |
| Salt | LOGISTICS |
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;private String ENCRYPTION_ALGO = "PBKDF2WithHmacSHA1";
private String CYPHER_ALGO = "AES/CBC/PKCS5Padding";
private String SALT = "LOGISTICS";
private String PASSWORD = "PS606AA85DRG3J93800Q9L90672IYV50";
private String ENCRYPTION_KEY = "61085G5GH96QW96D";private SecretKeySpec generateKey() {
SecretKeyFactory factory = SecretKeyFactory.getInstance(ENCRYPTION_ALGO);
byte[] saltBytes = SALT.getBytes("UTF-8");
KeySpec spec = new PBEKeySpec(PASSWORD.toCharArray(), saltBytes, 4, 128);
SecretKey tmp = factory.generateSecret(spec);
return new SecretKeySpec(tmp.getEncoded(), "AES");
}
private static Cipher getCipher(int mode) throws Exception {
Cipher cipher = Cipher.getInstance(CYPHER_ALGO);
byte[] iv = Encryption Key.getBytes("UTF-8");
cipher.init(mode, generateKey(), new IvParameterSpec(iv));
return cipher;
}
public static String encrypt(String raw) {
Cipher cipher = getCipher(1);
byte[] encryptedVal = cipher.doFinal(raw.getBytes("UTF-8"));
String encString = Base64.getEncoder().encodeToString(encryptedVal);
return URLEncoder.encode(encString, "UTF-8");
}import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.security.spec.KeySpec
import javax.crypto.Cipher
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
import java.util.Base64private val ENCRYPTION_ALGO = "PBKDF2WithHmacSHA1"
private val CYPHER_ALGO = "AES/CBC/PKCS5Padding"
private val SALT = "LOGISTICS"
private val PASSWORD = "PS606AA85DRG3J93800Q9L90672IYV50"
private val ENCRYPTION_KEY = "61085G5GH96QW96D"private fun generateKey(): SecretKeySpec {
val saltBytes = SALT.toByteArray(Charsets.UTF_8)
val spec: KeySpec = PBEKeySpec(PASSWORD.toCharArray(), saltBytes, 4, 128)
val factory = SecretKeyFactory.getInstance(ENCRYPTION_ALGO)
val tmp = factory.generateSecret(spec)
return SecretKeySpec(tmp.encoded, "AES")
}
@Throws(Exception::class)
private fun getCipher(mode: Int): Cipher {
val cipher = Cipher.getInstance(CYPHER_ALGO)
val iv = ENCRYPTION_KEY.toByteArray(Charsets.UTF_8)
cipher.init(mode, generateKey(), IvParameterSpec(iv))
return cipher
}
fun encrypt(raw: String): String {
val cipher = getCipher(Cipher.ENCRYPT_MODE)
val encryptedVal = cipher.doFinal(raw.toByteArray(Charsets.UTF_8))
val encString = Base64.getEncoder().encodeToString(encryptedVal)
return URLEncoder.encode(encString, "UTF-8")
}import <CommonCrypto/CommonCrypto.h>private let ENCRYPTION_ALGO = "PBKDF2WithHmacSHA1"
private let CYPHER_ALGO = "AES/CBC/PKCS5Padding"
private let SALT = "LOGISTICS"
private let PASSWORD = "PS606AA85DRG3J93800Q9L90672IYV50"
private let ENCRYPTION_KEY = "61085G5GH96QW96D"private static func generateKey() -> Data? {
guard let passwordData = password.data(using: .utf8),
let saltData = salt.data(using: .utf8) else { return nil }
var derivedKey = Data(count: 16) // 128 bits = 16 bytes
let result = derivedKey.withUnsafeMutableBytes { derivedKeyBytes in
saltData.withUnsafeBytes { saltBytes in
CCKeyDerivationPBKDF(
encryptionAlgo,
password, passwordData.count,
saltBytes.bindMemory(to: UInt8.self).baseAddress!, saltData.count,
CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1),
4, // Iteration count
derivedKeyBytes.bindMemory(to: UInt8.self).baseAddress!, 16)
}
}
return result == kCCSuccess ? derivedKey : nil
}
static func encrypt(raw: String) -> String? {
guard let key = generateKey(),
let iv = encryptionKey.data(using: .utf8),
let dataToEncrypt = raw.data(using: .utf8) else { return nil }
var outLength = Int(0)
var cipherData = Data(count: dataToEncrypt.count + kCCBlockSizeAES128)
let status = cipherData.withUnsafeMutableBytes { cipherBytes in
dataToEncrypt.withUnsafeBytes { dataBytes in
iv.withUnsafeBytes { ivBytes in
key.withUnsafeBytes { keyBytes in
CCCrypt(CCOperation(kCCEncrypt),
cipherAlgo, options,
keyBytes.baseAddress, key.count,
ivBytes.baseAddress,
dataBytes.baseAddress, dataToEncrypt.count,
cipherBytes.baseAddress, cipherData.count,
&outLength)
}
}
}
}
if status == kCCSuccess {
cipherData.removeSubrange(outLength..<cipherData.count)
let base64Encoded = cipherData.base64EncodedString()
return base64Encoded.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
}
return nil
}