Java tutorial
/* * The MIT License * * Copyright (c) 2010 Fabrice Medio * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package baggage.hypertoolkit.security; import org.apache.commons.codec.binary.Base64; import javax.crypto.*; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class CipherText { private byte[] bytes; public CipherText(byte[] bytes) { this.bytes = bytes; } public CipherText(String base64) { this.bytes = Base64.decodeBase64(base64); } public String base64() { return Base64.encodeBase64URLSafeString(bytes).trim(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CipherText that = (CipherText) o; if (!Arrays.equals(bytes, that.bytes)) return false; return true; } @Override public int hashCode() { return bytes != null ? Arrays.hashCode(bytes) : 0; } public ClearText decrypt(SecretKey secretKey) throws MessageIntegrityException { try { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return new ClearText(cipher.doFinal(bytes)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (IllegalBlockSizeException e) { throw new MessageIntegrityException(); } catch (BadPaddingException e) { throw new MessageIntegrityException(); } } }