LightJason - AgentSpeak(L++)
EAlgorithm.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin.crypto;
25 
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.commons.lang3.tuple.Pair;
30 
31 import javax.annotation.Nonnull;
32 import javax.crypto.Cipher;
33 import javax.crypto.KeyGenerator;
34 import javax.crypto.NoSuchPaddingException;
35 import java.security.InvalidKeyException;
36 import java.security.Key;
37 import java.security.KeyPair;
38 import java.security.KeyPairGenerator;
39 import java.security.NoSuchAlgorithmException;
40 import java.util.Locale;
41 
42 
46 public enum EAlgorithm
47 {
48  AES( "AES/ECB/PKCS5Padding", "AES" ),
49  DES( "DES/ECB/PKCS5Padding", "DES" ),
50  RSA( "RSA/ECB/PKCS1Padding", "RSA" );
51 
55  private final String m_cipher;
59  private final String m_key;
60 
67  EAlgorithm( @Nonnull final String p_cipher, @Nonnull final String p_key )
68  {
69  m_cipher = p_cipher;
70  m_key = p_key;
71  }
72 
80  @Nonnull
81  public final Pair<Key, Key> generateKey() throws NoSuchAlgorithmException
82  {
83  switch ( this )
84  {
85  case AES:
86  case DES:
87  return new ImmutablePair<>( KeyGenerator.getInstance( m_key ).generateKey(), null );
88 
89  case RSA:
90  final KeyPair l_key = KeyPairGenerator.getInstance( m_key ).generateKeyPair();
91  return new ImmutablePair<>( l_key.getPublic(), l_key.getPrivate() );
92 
93  default:
94  throw new CIllegalStateException( CCommon.languagestring( this, "unknown", this ) );
95  }
96  }
97 
108  @Nonnull
109  public final Cipher getEncryptCipher( @Nonnull final Key p_key ) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException
110  {
111  final Cipher l_cipher = Cipher.getInstance( m_cipher );
112  l_cipher.init( Cipher.ENCRYPT_MODE, p_key );
113  return l_cipher;
114  }
115 
126  @Nonnull
127  public final Cipher getDecryptCipher( @Nonnull final Key p_key ) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException
128  {
129  final Cipher l_cipher = Cipher.getInstance( m_cipher );
130  l_cipher.init( Cipher.DECRYPT_MODE, p_key );
131  return l_cipher;
132  }
133 
140  @Nonnull
141  public static EAlgorithm from( @Nonnull final String p_value )
142  {
143  return EAlgorithm.valueOf( p_value.trim().toUpperCase( Locale.ROOT ) );
144  }
145 
146 }
final Cipher getDecryptCipher( @Nonnull final Key p_key)
final Cipher getEncryptCipher( @Nonnull final Key p_key)
returns encrypt cipher
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
static EAlgorithm from( @Nonnull final String p_value)
additional factory
EAlgorithm( @Nonnull final String p_cipher, @Nonnull final String p_key)
ctor
Definition: EAlgorithm.java:67
final Pair< Key, Key > generateKey()
generates a key
Definition: EAlgorithm.java:81