Example usage for org.apache.shiro.crypto AesCipherService AesCipherService

List of usage examples for org.apache.shiro.crypto AesCipherService AesCipherService

Introduction

In this page you can find the example usage for org.apache.shiro.crypto AesCipherService AesCipherService.

Prototype

public AesCipherService() 

Source Link

Document

Creates a new CipherService instance using the AES cipher algorithm with the following important cipher default attributes:
Attribute Value
#setKeySize keySize 128 bits
#setBlockSize blockSize 128 bits (required for AES
#setMode mode OperationMode#GCM GCM *
#setPaddingScheme paddingScheme PaddingScheme#PKCS5 PKCS5
#setInitializationVectorSize(int) initializationVectorSize 128 bits
#setGenerateInitializationVectors(boolean) generateInitializationVectors true **

* The OperationMode#GCM GCM operation mode is used instead of the JDK default ECB to ensure strong encryption.

Usage

From source file:br.com.criativasoft.opendevice.restapi.auth.AesRuntimeCipher.java

License:Open Source License

public AesRuntimeCipher() {
    cipher = new AesCipherService();
    key = cipher.generateNewKey().getEncoded();
}

From source file:cn.powerdash.libsystem.common.security.util.CryptoUtil.java

License:Open Source License

public static String encrypt(String plainText, String key) {
    AesCipherService aes = new AesCipherService();
    byte[] keyBytes = CodecSupport.toBytes(key);
    ByteSource encryptedBytes = aes.encrypt(CodecSupport.toBytes(plainText), keyBytes);
    return encryptedBytes.toString();
}

From source file:cn.powerdash.libsystem.common.security.util.CryptoUtil.java

License:Open Source License

public static String decrypt(String cipherText, String key) {
    AesCipherService aesCs = new AesCipherService();
    byte[] keyBytes = CodecSupport.toBytes(key);

    ByteSource decryptedBytes = aesCs.decrypt(Base64.decode(CodecSupport.toBytes(cipherText)), keyBytes);
    return CodecSupport.toString(decryptedBytes.getBytes());
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

private AesCipherService aesCipherService(final int keySize) {
    final AesCipherService aes = new AesCipherService();
    aes.setKeySize(keySize);//w w w .ja  va  2  s  . com
    return aes;
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

/**
 * Test of getEncryptionKeys method, of class EncryptionServiceImpl.
 *//*  w  w  w .j av a2  s  .c o  m*/
@Test
public void testGetEncryptionKeys() {
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    assertThat(service.getEncryptionKeys().size(), is(1));
    assertThat(service.getEncryptionKeys().contains("a"), is(true));
    assertThat(service.getEncryptionKeys().contains("b"), is(false));
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

@Test
public void testDefaultEncryptDecrypt() throws InvalidProtocolBufferException {
    final String METHOD = "testDefaultEncryptDecrypt";
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    byte[] encrypted = service.encrypt(appInstanceBytes, "a");
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("uncrypted length = %d", appInstanceBytes.length));
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("encrypted length = %d", encrypted.length));

    final byte[] decrypted = service.decrypt(encrypted, "a");
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("decrypted length = %d", appInstanceBytes.length));
    final RunRightFastApplicationInstance appInstance2 = RunRightFastApplicationInstance.parseFrom(decrypted);
    assertThat(appInstance2, is(equalTo(appInstance)));
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

@Test
public void testBasicEncryptionDecryption() throws InvalidProtocolBufferException {
    final String METHOD = "testBasicEncryptionDecryption";
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    final Encryption encryption = service.encryption("a");
    final Decryption decryption = service.decryption("a");
    byte[] encrypted = encryption.apply(appInstanceBytes);
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("uncrypted length = %d", appInstanceBytes.length));
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("encrypted length = %d", encrypted.length));

    final byte[] decrypted = decryption.apply(encrypted);
    log.logp(Level.INFO, getClass().getName(), METHOD,
            String.format("decrypted length = %d", appInstanceBytes.length));
    final RunRightFastApplicationInstance appInstance2 = RunRightFastApplicationInstance.parseFrom(decrypted);
    assertThat(appInstance2, is(equalTo(appInstance)));
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

@Test(expected = UnknownSecretKeyException.class)
public void testInvalidSecretKey_cipherFunctions() {
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    service.cipherFunctions("b");
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

@Test(expected = UnknownSecretKeyException.class)
public void testInvalidSecretKey_encrypt() {
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    service.encrypt("data".getBytes(), "b");
}

From source file:co.runrightfast.core.crypto.impl.EncryptionServiceImplTest.java

License:Apache License

@Test(expected = UnknownSecretKeyException.class)
public void testInvalidSecretKey_encryption() {
    final AesCipherService aes = new AesCipherService();
    final Map<String, Key> keys = ImmutableMap.<String, Key>builder().put("a", aes.generateNewKey()).build();
    final EncryptionService service = new EncryptionServiceImpl(aes, keys);
    service.encryption("b");
}