日期:2014-05-16 浏览次数:20501 次
import java.io.IOException; import java.security.InvalidKeyException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * Servlet implementation class PersonShowServlet */ public class PersonShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; private String signatureAlgorithm = "SHA1withRSA"; /** * @see HttpServlet#HttpServlet() */ public PersonShowServlet() { super(); // TODO Auto-generated constructor stub } //在这里后台做签名与js相比较 public static void main(String[] args) throws ServletException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { PersonShowServlet pss = new PersonShowServlet(); byte[] bytes = "Hello 1".getBytes("UTF-8");//pss.digest("Hello 1"); byte[] signedBytes = pss.doSign(bytes, CertificateUtil.loadPrivateKey("E:/tmp/pki-crt/mark.jks")); String signedB64 = new BASE64Encoder().encode(signedBytes); System.out.println(signedB64); Resource resource = new ClassPathResource("/com/apusic/portal/ldap/demo/spring-ldap.xml"); BeanFactory factory = new XmlBeanFactory(resource); UserDao userDao = (UserDao) factory.getBean("userDao"); String dn = "cn=mark"; Person person = userDao.getPersonByDn(dn); PublicKey publicKey = CertificateUtil.loadCertificate(person).getPublicKey(); boolean result = pss.doVerify(bytes, publicKey, new BASE64Decoder().decodeBuffer(signedB64)); System.out.println(result); } //在这里校验js生成的签名 /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(Ht