Add length parameter to Rsa.generateKeyPair()

bzip2
Graham 5 years ago
parent 6326aa93db
commit 2fa55b3ce1
  1. 6
      common/src/main/java/dev/openrs2/common/crypto/Rsa.kt
  2. 2
      common/src/main/java/dev/openrs2/common/crypto/RsaKeyProvider.kt
  3. 2
      common/src/test/java/dev/openrs2/common/crypto/RsaTest.kt

@ -59,14 +59,14 @@ object Rsa {
* The maximum output length of RSA encryption is the key size plus one, so * The maximum output length of RSA encryption is the key size plus one, so
* the maximum key size supported by the client is 126 bytes - or 1008 bits. * the maximum key size supported by the client is 126 bytes - or 1008 bits.
*/ */
private const val KEY_LENGTH = 1008 const val KEY_LENGTH = 1008
// 1 in 2^80 // 1 in 2^80
private const val CERTAINTY = 80 private const val CERTAINTY = 80
fun generateKeyPair(): Pair<RSAKeyParameters, RSAPrivateCrtKeyParameters> { fun generateKeyPair(length: Int): Pair<RSAKeyParameters, RSAPrivateCrtKeyParameters> {
val generator = RSAKeyPairGenerator() val generator = RSAKeyPairGenerator()
generator.init(RSAKeyGenerationParameters(F4, secureRandom, KEY_LENGTH, CERTAINTY)) generator.init(RSAKeyGenerationParameters(F4, secureRandom, length, CERTAINTY))
val keyPair = generator.generateKeyPair() val keyPair = generator.generateKeyPair()
return Pair(keyPair.public as RSAKeyParameters, keyPair.private as RSAPrivateCrtKeyParameters) return Pair(keyPair.public as RSAKeyParameters, keyPair.private as RSAPrivateCrtKeyParameters)

@ -10,7 +10,7 @@ class RsaKeyProvider : Provider<RSAPrivateCrtKeyParameters> {
return if (Files.exists(PRIVATE_PATH)) { return if (Files.exists(PRIVATE_PATH)) {
Rsa.readPrivateKey(PRIVATE_PATH) Rsa.readPrivateKey(PRIVATE_PATH)
} else { } else {
val (public, private) = Rsa.generateKeyPair() val (public, private) = Rsa.generateKeyPair(Rsa.KEY_LENGTH)
Rsa.writePublicKey(PUBLIC_PATH, public) Rsa.writePublicKey(PUBLIC_PATH, public)
Rsa.writePrivateKey(PRIVATE_PATH, private) Rsa.writePrivateKey(PRIVATE_PATH, private)
private private

@ -46,7 +46,7 @@ object RsaTest {
@Test @Test
fun testGenerateKeyPair() { fun testGenerateKeyPair() {
val (public, private) = Rsa.generateKeyPair() val (public, private) = Rsa.generateKeyPair(Rsa.KEY_LENGTH)
val expectedPlaintext = BigInteger("1337") val expectedPlaintext = BigInteger("1337")
val ciphertext = Rsa.encrypt(expectedPlaintext, public) val ciphertext = Rsa.encrypt(expectedPlaintext, public)

Loading…
Cancel
Save