Add ByteBuf extension methods for RSA encryption and decryption

bzip2
Graham 5 years ago
parent e64dbe1a0e
commit 6b247cb0e1
  1. 4
      common/pom.xml
  2. 26
      common/src/main/java/dev/openrs2/common/crypto/Rsa.kt
  3. 47
      common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt
  4. 5
      pom.xml

@ -23,6 +23,10 @@
<groupId>com.michael-bull.kotlin-inline-logger</groupId>
<artifactId>kotlin-inline-logger-jvm</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>

@ -1,5 +1,7 @@
package dev.openrs2.common.crypto
import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled
import org.bouncycastle.asn1.DERNull
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo
@ -22,6 +24,30 @@ import java.math.BigInteger
import java.nio.file.Files
import java.nio.file.Path
private fun ByteBuf.toBigInteger(): BigInteger {
val bytes: ByteArray
if (hasArray() && arrayOffset() == 0 && readerIndex() == 0 && readableBytes() == array().size) {
bytes = array()
} else {
bytes = ByteArray(readableBytes())
getBytes(readerIndex(), bytes)
}
return BigInteger(bytes)
}
private fun BigInteger.toByteBuf(): ByteBuf {
return Unpooled.wrappedBuffer(toByteArray())
}
fun ByteBuf.rsaEncrypt(key: RSAKeyParameters): ByteBuf {
return Rsa.encrypt(toBigInteger(), key).toByteBuf()
}
fun ByteBuf.rsaDecrypt(key: RSAKeyParameters): ByteBuf {
return Rsa.decrypt(toBigInteger(), key).toByteBuf()
}
object Rsa {
private const val PUBLIC_KEY = "PUBLIC KEY"
private const val PRIVATE_KEY = "PRIVATE KEY"

@ -1,5 +1,6 @@
package dev.openrs2.common.crypto
import io.netty.buffer.Unpooled
import org.bouncycastle.crypto.params.RSAKeyParameters
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters
import org.bouncycastle.util.Properties
@ -54,6 +55,52 @@ object RsaTest {
assertEquals(BigInteger("65"), ciphertext)
}
@Test
fun testEncryptByteBuf() {
// from https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Example
val public = allowUnsafeMod { RSAKeyParameters(false, BigInteger("3233"), BigInteger("17")) }
val plaintext = Unpooled.wrappedBuffer(byteArrayOf(65))
try {
val ciphertext = plaintext.rsaEncrypt(public)
try {
val expectedCiphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte()))
try {
assertEquals(expectedCiphertext, ciphertext)
} finally {
expectedCiphertext.release()
}
} finally {
ciphertext.release()
}
} finally {
plaintext.release()
}
}
@Test
fun testDecryptByteBuf() {
// from https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Example
val private = allowUnsafeMod { RSAKeyParameters(true, BigInteger("3233"), BigInteger("413")) }
val ciphertext = Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte()))
try {
val plaintext = ciphertext.rsaDecrypt(private)
try {
val expectedPlaintext = Unpooled.wrappedBuffer(byteArrayOf(65))
try {
assertEquals(expectedPlaintext, plaintext)
} finally {
expectedPlaintext.release()
}
} finally {
plaintext.release()
}
} finally {
ciphertext.release()
}
}
private fun <T> allowUnsafeMod(f: () -> T): T {
Properties.setThreadOverride(ALLOW_UNSAFE_MOD, true)
try {

@ -83,6 +83,11 @@
<artifactId>pack200</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.1.44.Final</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>

Loading…
Cancel
Save