From 7b730e4d0a747974e93fedc6ce4ea06c80b67b6c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 29 Sep 2014 16:43:51 +0200 Subject: Change data type of auth challenge from string to byte array, add message verifier that will use private/public keypair directly --- .../openslx/encryption/AsymEncryptionHandler.java | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/main/java/org/openslx/encryption/AsymEncryptionHandler.java (limited to 'src/main/java/org/openslx/encryption') diff --git a/src/main/java/org/openslx/encryption/AsymEncryptionHandler.java b/src/main/java/org/openslx/encryption/AsymEncryptionHandler.java new file mode 100644 index 0000000..98109f2 --- /dev/null +++ b/src/main/java/org/openslx/encryption/AsymEncryptionHandler.java @@ -0,0 +1,89 @@ +package org.openslx.encryption; + +import java.security.InvalidKeyException; +import java.security.InvalidParameterException; +import java.security.Key; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; + +import org.apache.log4j.Logger; + +public class AsymEncryptionHandler +{ + private static final Logger LOG = Logger.getLogger( AsymEncryptionHandler.class ); + + private final Key key; + + /** + * Create a handler. + */ + public AsymEncryptionHandler( Key key ) + { + this.key = key; + } + + /** + * Encrypt given plain text message with the key this class was + * instantiated with. + * + * @param cleartext a clear text message + * @return The encrypted message + */ + public byte[] encryptMessage( byte[] cleartext ) + { + try { + Cipher cipher = Cipher.getInstance( "RSA" ); + cipher.init( Cipher.ENCRYPT_MODE, key ); + return cipher.doFinal( cleartext ); + } catch ( NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e ) { + LOG.warn( "Cannot encrypt message", e ); + } + return null; + } + + /** + * Verify an encrypted message, where we know the plain text. + * + * @param encryptedMessage + * @param expectedCleartext + * @return true if the message matches the expected plain text after decrypting + */ + public boolean verifyMessage( byte[] encryptedMessage, byte[] expectedCleartext ) + { + try { + Cipher cipher = Cipher.getInstance( "RSA" ); + cipher.init( Cipher.DECRYPT_MODE, key ); + byte[] result = cipher.doFinal( encryptedMessage ); + return Arrays.equals( expectedCleartext, result ); + } catch ( NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e ) { + LOG.warn( "Cannot verify message", e ); + } + return false; + } + + /** + * Generate a fresh RSA key pair. + * + * @param bits length of key + * @return key pair, or null on error + */ + public static KeyPair generateKeyPair( int bits ) + { + try { + KeyPairGenerator kpg = KeyPairGenerator.getInstance( "RSA" ); + kpg.initialize( bits ); + return kpg.genKeyPair(); + } catch ( NoSuchAlgorithmException | InvalidParameterException e ) { + LOG.warn( "Cannot generate RSA Keypair", e ); + return null; + } + } + +} -- cgit v1.2.3-55-g7522