Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by ghita ( 6 years ago )
package com.cofedistrict.helpers.scan
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.json.JSONException
import org.json.JSONObject
import java.io.UnsupportedEncodingException
import java.security.InvalidKeyException
import java.security.NoSuchAlgorithmException
import java.security.Security
import javax.crypto.*
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
object AESEncryption {
const val AES_KEY = "YNYp!hrBMG5+hYqQ"
const val IV_KEY = "X8rt9YYtASNVbw2X"
fun decryptCodeFromVoucherQRCodeScan(strToDecrypt: String?): String? {
if (strToDecrypt.isNullOrEmpty()) {
return null
}
Security.addProvider(BouncyCastleProvider())
var keyBytes: ByteArray
try {
keyBytes = AES_KEY.toByteArray(charset("UTF8"))
val skey = SecretKeySpec(keyBytes, "AES")
val input = org.bouncycastle.util.encoders.Base64
.decode(strToDecrypt?.trim { it <= ' ' }?.toByteArray(charset("UTF8")))
synchronized(Cipher::class.java) {
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, skey, IvParameterSpec(IV_KEY.toByteArray()))
val plainText = ByteArray(cipher.getOutputSize(input.size))
var ptLength = cipher.update(input, 0, input.size, plainText, 0)
ptLength += cipher.doFinal(plainText, ptLength)
val decryptedString = String(plainText)
return try {
if (JSONObject(decryptedString).get("type").toString() == "COUPON" || JSONObject(decryptedString).get("type").toString() == "GIFT_CARD") {
JSONObject(decryptedString).get("code").toString()
} else {
null
}
} catch (e: JSONException) {
null
}
}
} catch (uee: UnsupportedEncodingException) {
uee.printStackTrace()
} catch (ibse: IllegalBlockSizeException) {
ibse.printStackTrace()
} catch (bpe: BadPaddingException) {
bpe.printStackTrace()
} catch (ike: InvalidKeyException) {
ike.printStackTrace()
} catch (nspe: NoSuchPaddingException) {
nspe.printStackTrace()
} catch (nsae: NoSuchAlgorithmException) {
nsae.printStackTrace()
} catch (e: ShortBufferException) {
e.printStackTrace()
}
return null
}
}
Revise this Paste