com.iterzp.momo.service.impl.RSAServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.iterzp.momo.service.impl.RSAServiceImpl.java

Source

/*
 * Copyright 2005-2013 iterzp.com. All rights reserved.
 * Support: http://www.iterzp.com
 * License: http://www.iterzp.com/license
 */
package com.iterzp.momo.service.impl;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.iterzp.momo.service.RSAService;
import com.iterzp.momo.utils.RSAUtils;

/**
 * Service - RSA
 * 
 * @author yd14 Team
 * @version 3.0
 */
@Service("rsaServiceImpl")
public class RSAServiceImpl implements RSAService {

    /** "?"??? */
    private static final String PRIVATE_KEY_ATTRIBUTE_NAME = "privateKey";

    @Override
    @Transactional(readOnly = true)
    public RSAPublicKey generateKey(HttpServletRequest request) {
        Assert.notNull(request);
        KeyPair keyPair = RSAUtils.generateKeyPair();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        HttpSession session = request.getSession();
        session.setAttribute(PRIVATE_KEY_ATTRIBUTE_NAME, privateKey);
        return publicKey;
    }

    @Override
    @Transactional(readOnly = true)
    public void removePrivateKey(HttpServletRequest request) {
        Assert.notNull(request);
        HttpSession session = request.getSession();
        session.removeAttribute(PRIVATE_KEY_ATTRIBUTE_NAME);
    }

    @Override
    @Transactional(readOnly = true)
    public String decryptParameter(String name, HttpServletRequest request) {
        Assert.notNull(request);
        if (name != null) {
            HttpSession session = request.getSession();
            RSAPrivateKey privateKey = (RSAPrivateKey) session.getAttribute(PRIVATE_KEY_ATTRIBUTE_NAME);
            String parameter = request.getParameter(name);
            if (privateKey != null && StringUtils.isNotEmpty(parameter)) {
                return RSAUtils.decrypt(privateKey, parameter);
            }
        }
        return null;
    }

}