Java tutorial
/* * Copyright (C) 2017 FormKiQ Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.formkiq.core.service.crypto; import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.Base64; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import com.fasterxml.jackson.core.JsonProcessingException; import com.formkiq.core.form.JSONService; import com.formkiq.core.service.dto.LicenseToken; import com.formkiq.core.util.Strings; /** * Implementation of handle Secure Tokens Service. * */ public class SecureTokenServiceImpl implements SecureTokenService { /** Logger. */ private static final Logger LOG = Logger.getLogger(SecureTokenServiceImpl.class.getName()); /** Default Key Size. */ public static final int DEFAULT_KEYSIZE = 4096; /** {@link JSONService}. */ @Autowired private JSONService jsonService; @Override public String createLicenseToken(final RSAPrivateKey privateKey, final LicenseToken license) throws GeneralSecurityException, JsonProcessingException { if (license.getIssuedat() == null) { throw new InvalidLicenseException("Invalid License: Issue Date required"); } String text = this.jsonService.writeValueAsString(license); return encryptToken(privateKey, text); } @Override public LicenseToken decodeLicenseToken(final PublicKey publicKey, final String encryptedToken) throws InvalidLicenseException { try { String decrypted = decryptToken(publicKey, encryptedToken); LicenseToken license = this.jsonService.readValue(decrypted, LicenseToken.class); return license; } catch (Exception e) { LOG.log(Level.WARNING, "Invalid License", e); throw new InvalidLicenseException("Invalid License"); } } @Override public String decryptToken(final PublicKey publicKey, final String encryptedToken) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] bs = cipher.doFinal(Base64.getDecoder().decode(encryptedToken)); return Strings.toString(bs); } @Override public String encryptToken(final PrivateKey privateKey, final String token) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = Strings.getBytes(token); return Base64.getEncoder().encodeToString(cipher.doFinal(bytes)); } @Override public Pair<RSAPublicKey, RSAPrivateKey> initializeToken(final int keysize) throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(keysize); KeyPair kp = kpg.genKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate(); return Pair.of(publicKey, privateKey); } }