import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Encrypt {
private static final String Algorithm = "DESede";
private static final byte[] sourceKey = {24个字符串};//密钥
private static final Key key = new SecretKeySpec(sourceKey, Algorithm);
private String charset = "GBK";
/**
* 加密
*
* @param source
* 明文
* @return 密文
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public String encrypt(String source) throws InvalidKeyException,
UnsupportedEncodingException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException {
String result = new String();
byte[] tran = null;
byte[] target = source.getBytes(charset);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
tran = cipher.doFinal(target);
result = new BASE64Encoder().encode(tran);
return result;
}
/**
* 解密
*
* @param source
* 密文
* @return 明文
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws UnsupportedEncodingException
* @throws IOException
*/
public String decrypt(String source) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException, IOException {
String result = new String();
byte[] tran = null;
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.DECRYPT_MODE, key);// 使用私钥解密
tran = cipher.doFinal(new BASE64Decoder().decodeBuffer(source));
result = new String(tran, charset);
return result;
}
/**
* 获得字符集
*
* @return
*/
public String getCharset() {
return charset;
}
/**
* 设置字符集 默认“GBK”
*
* @return
*/
public void setCharset(String charset) {
this.charset = charset;
}
}
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.qrcode.EncodeHintType;
import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel;
/**
* 生成二维码
* @author
*
*/
public class QRCodeGeneration {
/**
* 生成二维码图片
* @param toEncryptStr 要加密的信息
* @return Image
* @throws Exception
*/
public static Image generateQR(JSONArray toEncryptStr) throws Exception
{
//设置QR二维码参数
Map<EncodeHintType,Object> hints=new HashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
Encrypt encoder=new Encrypt();
//加密
String ming=toEncryptStr.toString();//明文
// System.out.println("ming="+ming);
String mi=encoder.encrypt(ming);//密文
// System.out.println("mi="+mi);
// System.out.println("解密:"+encoder.decrypt(mi));
//生成二位图
BarcodeQRCode qr=new BarcodeQRCode(mi,150,150,hints);//此处是用密文生成的二维码,因此扫描出来的信息也是密文
Image image = qr.getImage();
return image;//返回二维码
}
}
//此处是生成PDF的处理方法
<%@page language = "java" errorPage = "/public/errorpage.jsp"
import="
java.io.*,com.lowagie.text.pdf.PdfCell,com.itextpdf.awt.geom.Rectangle,com.itextpdf.text.Element,
net.sf.json.JSONArray,java.util.HashMap,java.util.Map,com.itextpdf.text.pdf.PdfPCell,
java.util.Date,java.text.DateFormat,java.text.SimpleDateFormat,com.itextpdf.text.Paragraph,
com.itextpdf.text.Font,test.QRCodeGeneration,com.itextpdf.text.Image,com.itextpdf.text.PageSize,
com.itextpdf.text.Document,com.itextpdf.text.pdf.BaseFont,com.itextpdf.text.pdf.PdfWriter;"
%>
String code ="";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//打印时间 打印人
Document doc = new Document(PageSize.A4, 90, 90, 100, 120);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ServletOutputStream output =null;
try{
//定义输出位置并把文档对象装入输出对象中
PdfWriter.getInstance(doc, ba);
// 打开文档对象
doc.open();
/* 使用中文字体 */
BaseFont songFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font song = new Font(songFont, 11, Font.NORMAL);
Font songBOLD = new Font(songFont, 15, Font.BOLD);
Font songBIG = new Font(songFont, 15, Font.NORMAL);
Paragraph title1 = new Paragraph("大标题", songBOLD);
Paragraph title2 = new Paragraph("换行大标题", songBOLD);
title = "二维码生成数据";
title1.setAlignment(title1.ALIGN_CENTER);
title2.setAlignment(title2.ALIGN_CENTER);
doc.add(title1);
doc.add(title2);
doc.add(new Paragraph("\n\n", song));
Paragraph projectNoPara = new Paragraph("PDF显示编号:"+projectNo + "\n\n", song);
Paragraph projectNamePara=new Paragraph("PDF显示名称:"+projectName+"\n\n",song);
Map<String,String> content=new HashMap();
content.put("title", title);
content.put("projectNo",projectNo);
content.put("projectName",projectName);
JSONArray jsonArray =JSONArray.fromObject(content);
Image qrCodeImage=QRCodeGeneration.generateQR(jsonArray);
qrCodeImage.scaleToFit(160,160);
//qrCodeImage.setAlignment(qrCodeImage.ALIGN_RIGHT);
//doc.add(qrCodeImage);
com.itextpdf.text.pdf.PdfPTable table=new com.itextpdf.text.pdf.PdfPTable(2);//表格 2列
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);//默认单元格无边框
PdfPCell cell1=new PdfPCell(projectNoPara);//项目编号
cell1.setColspan(2);
cell1.setBorder(PdfCell.NO_BORDER);
table.addCell(cell1);
PdfPCell cell2=new PdfPCell(projectNamePara);//项目名称
cell2.setColspan(2);
cell2.setBorder(PdfCell.NO_BORDER);
table.addCell(cell2);
PdfPCell cell11=new PdfPCell(qrCodeImage);//二维码
cell11.setColspan(2);
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
cell11.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell11);
table.setWidthPercentage(100);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
//cell1.setFixedHeight(fixedHeight); //行高
//cell1.setNoWrap(true);//默认值失效
//cell1.setMinimumHeight(minimumHeight);//行高不得小于该值
//table.setExtendLastRow(true);// 表格最后一行填充到页面的底部
//Element.ALIGN_MIDDLE;//垂直居中
doc.add(table);
doc.close();
response.reset();
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment; filename=download.pdf");
response.setContentLength(ba.size());
output = response.getOutputStream();
ba.writeTo(output);
output.flush();
output.close();
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}catch(Exception e){
e.printStackTrace();
}