Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2013-12-31 10:24:00 +0400
committerPeter Dettman <peter.dettman@bouncycastle.org>2013-12-31 10:24:00 +0400
commitb4c624ba84dd7f0352ad47fa489882c91e38d0eb (patch)
tree0d432a0d2580ef43c257c62526f4613680845dd2 /core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java
parent10d7ae1da2ada9f66b60bc98f2d5c76a9b780b19 (diff)
Factor out method generateKey
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java')
-rwxr-xr-xcore/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java45
1 files changed, 18 insertions, 27 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java b/core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java
index 8c1a1724..42fc2353 100755
--- a/core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java
+++ b/core/src/main/java/org/bouncycastle/crypto/kems/RSAKeyEncapsulation.java
@@ -38,7 +38,6 @@ public class RSAKeyEncapsulation
this.rnd = rnd;
}
-
/**
* Initialise the RSA-KEM.
*
@@ -51,12 +50,9 @@ public class RSAKeyEncapsulation
{
throw new IllegalArgumentException("RSA key required");
}
- else
- {
- this.key = (RSAKeyParameters)key;
- }
- }
+ this.key = (RSAKeyParameters)key;
+ }
/**
* Generate and encapsulate a random session key.
@@ -79,25 +75,15 @@ public class RSAKeyEncapsulation
// Generate the ephemeral random and encode it
BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd);
- byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
// Encrypt the random and encode it
BigInteger c = r.modPow(e, n);
byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c);
System.arraycopy(C, 0, out, outOff, C.length);
-
- // Initialise the KDF
- kdf.init(new KDFParameters(R, null));
-
- // Generate the secret key
- byte[] K = new byte[keyLen];
- kdf.generateBytes(K, 0, K.length);
-
- return new KeyParameter(K);
+ return generateKey(n, r, keyLen);
}
-
/**
* Generate and encapsulate a random session key.
*
@@ -110,7 +96,6 @@ public class RSAKeyEncapsulation
return encrypt(out, 0, keyLen);
}
-
/**
* Decrypt an encapsulated session key.
*
@@ -138,16 +123,8 @@ public class RSAKeyEncapsulation
// Decrypt the ephemeral random and encode it
BigInteger r = c.modPow(d, n);
- byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
- // Initialise the KDF
- kdf.init(new KDFParameters(R, null));
-
- // Generate the secret key
- byte[] K = new byte[keyLen];
- kdf.generateBytes(K, 0, K.length);
-
- return new KeyParameter(K);
+ return generateKey(n, r, keyLen);
}
/**
@@ -161,4 +138,18 @@ public class RSAKeyEncapsulation
{
return decrypt(in, 0, in.length, keyLen);
}
+
+ protected KeyParameter generateKey(BigInteger n, BigInteger r, int keyLen)
+ {
+ byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
+
+ // Initialise the KDF
+ kdf.init(new KDFParameters(R, null));
+
+ // Generate the secret key
+ byte[] K = new byte[keyLen];
+ kdf.generateBytes(K, 0, K.length);
+
+ return new KeyParameter(K);
+ }
}