Skip to content

Instantly share code, notes, and snippets.

@ultraviolet-jordan
Created August 5, 2023 03:13
Show Gist options
  • Save ultraviolet-jordan/c214e49090060cbc044d61b19f2eb05d to your computer and use it in GitHub Desktop.
Save ultraviolet-jordan/c214e49090060cbc044d61b19f2eb05d to your computer and use it in GitHub Desktop.
fun ByteBuffer.rsadec(exponent: BigInteger, modulus: BigInteger) {
val length = g1
val dec = BigInteger(gdata(length)).modPow(exponent, modulus).toByteArray()
position(0)
pdata(dec)
position(0)
}
======================================================================
fun ByteBuffer.rsadec(pem: RSAPrivateCrtKey) {
val p = pem.primeP
val q = pem.primeQ
val dP = pem.primeExponentP
val dQ = pem.primeExponentQ
val qInv = pem.crtCoefficient
val length = g1
val bytes = gdata(length)
val data = BigInteger(1, bytes)
val m1 = data.mod(p).modPow(dP, p)
val m2 = data.mod(q).modPow(dQ, q)
val h = qInv * (m1 - m2) % p
val dec = (m2 + h * q).toByteArray()
position(0)
pdata(dec)
position(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment