1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.security.esapi;
17
18 import org.apache.commons.codec.binary.Base64;
19 import org.owasp.esapi.Encryptor;
20 import org.owasp.esapi.crypto.CipherText;
21 import org.owasp.esapi.crypto.PlainText;
22 import org.owasp.esapi.errors.EncryptionException;
23
24 import de.smartics.properties.api.core.domain.PropertyCode;
25 import de.smartics.properties.api.core.domain.PropertyDescriptor;
26 import de.smartics.properties.api.core.domain.PropertyDescriptorMessageBean;
27 import de.smartics.properties.api.core.security.SecurityException;
28 import de.smartics.properties.api.core.security.PropertyValueSecurity;
29
30
31
32
33 public abstract class AbstractEsapiPropertyValueSecurity implements
34 PropertyValueSecurity
35 {
36
37
38
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45
46
47
48
49
50
51
52
53
54 public AbstractEsapiPropertyValueSecurity()
55 {
56 }
57
58
59
60
61
62
63
64
65
66
67
68 @Override
69 public String decrypt(final PropertyDescriptor descriptor,
70 final String encryptedValue) throws SecurityException
71 {
72 try
73 {
74 final Encryptor encryptor = provideEncryptor();
75 final byte[] bytes = Base64.decodeBase64(encryptedValue);
76 final CipherText cipherText =
77 CipherText.fromPortableSerializedBytes(bytes);
78 final PlainText plainText = encryptor.decrypt(cipherText);
79 final String plainValue = plainText.toString();
80 return plainValue;
81 }
82 catch (final EncryptionException e)
83 {
84 throw new SecurityException(new PropertyDescriptorMessageBean(
85 PropertyCode.SECURITY, e, descriptor));
86 }
87 }
88
89
90
91
92
93
94
95 protected abstract Encryptor provideEncryptor() throws EncryptionException;
96
97 @Override
98 public String encrypt(final PropertyDescriptor descriptor,
99 final String plainValue) throws SecurityException
100 {
101 try
102 {
103 final Encryptor encryptor = provideEncryptor();
104 final PlainText plainText = new PlainText(plainValue);
105 final CipherText cipherText = encryptor.encrypt(plainText);
106 final byte[] bytes = cipherText.asPortableSerializedByteArray();
107 final String encryptedValue = Base64.encodeBase64String(bytes);
108 return encryptedValue;
109 }
110 catch (final EncryptionException e)
111 {
112 throw new SecurityException(new PropertyDescriptorMessageBean(
113 PropertyCode.SECURITY, e, descriptor));
114 }
115 }
116
117
118
119 }