diff options
Diffstat (limited to 'pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java')
-rw-r--r-- | pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java b/pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java index f93bd079..58160a97 100644 --- a/pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java +++ b/pg/src/main/java/org/bouncycastle/openpgp/operator/PublicKeyKeyEncryptionMethodGenerator.java @@ -1,8 +1,10 @@ package org.bouncycastle.openpgp.operator; +import java.io.IOException; import java.math.BigInteger; import org.bouncycastle.bcpg.ContainedPacket; +import org.bouncycastle.bcpg.MPInteger; import org.bouncycastle.bcpg.PublicKeyEncSessionPacket; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKey; @@ -19,56 +21,74 @@ public abstract class PublicKeyKeyEncryptionMethodGenerator switch (pubKey.getAlgorithm()) { - case PGPPublicKey.RSA_ENCRYPT: - case PGPPublicKey.RSA_GENERAL: - break; - case PGPPublicKey.ELGAMAL_ENCRYPT: - case PGPPublicKey.ELGAMAL_GENERAL: - break; - case PGPPublicKey.ECDH: - break; - case PGPPublicKey.DSA: - throw new IllegalArgumentException("Can't use DSA for encryption."); - case PGPPublicKey.ECDSA: - throw new IllegalArgumentException("Can't use ECDSA for encryption."); - default: - throw new IllegalArgumentException("unknown asymmetric algorithm: " + pubKey.getAlgorithm()); + case PGPPublicKey.RSA_ENCRYPT: + case PGPPublicKey.RSA_GENERAL: + break; + case PGPPublicKey.ELGAMAL_ENCRYPT: + case PGPPublicKey.ELGAMAL_GENERAL: + break; + case PGPPublicKey.ECDH: + break; + case PGPPublicKey.DSA: + throw new IllegalArgumentException("Can't use DSA for encryption."); + case PGPPublicKey.ECDSA: + throw new IllegalArgumentException("Can't use ECDSA for encryption."); + default: + throw new IllegalArgumentException("unknown asymmetric algorithm: " + pubKey.getAlgorithm()); } } - public BigInteger[] processSessionInfo( + public byte[][] processSessionInfo( byte[] encryptedSessionInfo) throws PGPException { - BigInteger[] data; + byte[][] data; switch (pubKey.getAlgorithm()) { - case PGPPublicKey.RSA_ENCRYPT: - case PGPPublicKey.RSA_GENERAL: - data = new BigInteger[1]; + case PGPPublicKey.RSA_ENCRYPT: + case PGPPublicKey.RSA_GENERAL: + data = new byte[1][]; - data[0] = new BigInteger(1, encryptedSessionInfo); - break; - case PGPPublicKey.ELGAMAL_ENCRYPT: - case PGPPublicKey.ELGAMAL_GENERAL: - byte[] b1 = new byte[encryptedSessionInfo.length / 2]; - byte[] b2 = new byte[encryptedSessionInfo.length / 2]; + data[0] = convertToEncodedMPI(encryptedSessionInfo); + break; + case PGPPublicKey.ELGAMAL_ENCRYPT: + case PGPPublicKey.ELGAMAL_GENERAL: + byte[] b1 = new byte[encryptedSessionInfo.length / 2]; + byte[] b2 = new byte[encryptedSessionInfo.length / 2]; - System.arraycopy(encryptedSessionInfo, 0, b1, 0, b1.length); - System.arraycopy(encryptedSessionInfo, b1.length, b2, 0, b2.length); + System.arraycopy(encryptedSessionInfo, 0, b1, 0, b1.length); + System.arraycopy(encryptedSessionInfo, b1.length, b2, 0, b2.length); - data = new BigInteger[2]; - data[0] = new BigInteger(1, b1); - data[1] = new BigInteger(1, b2); - break; - default: - throw new PGPException("unknown asymmetric algorithm: " + pubKey.getAlgorithm()); + data = new byte[2][]; + data[0] = convertToEncodedMPI(b1); + data[1] = convertToEncodedMPI(b2); + break; + case PGPPublicKey.ECDH: + data = new byte[1][]; + + data[0] = encryptedSessionInfo; + break; + default: + throw new PGPException("unknown asymmetric algorithm: " + pubKey.getAlgorithm()); } return data; } + private byte[] convertToEncodedMPI(byte[] encryptedSessionInfo) + throws PGPException + { + try + { + return new MPInteger(new BigInteger(1, encryptedSessionInfo)).getEncoded(); + } + catch (IOException e) + { + throw new PGPException("Invalid MPI encoding: " + e.getMessage(), e); + } + } + public ContainedPacket generate(int encAlgorithm, byte[] sessionInfo) throws PGPException { |