private void button2_Click(object sender, EventArgs e)
{
// string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost:4451/TEST/Default.aspx");
string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost/release/Default.aspx");
MessageBox.Show(a);
}
public static string MyUploader(string strFileToUpload, string strUrl)
{
string strFileFormName = "file";
Uri oUri = new Uri(strUrl);
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
// The trailing boundary string
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
// The post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(strFileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(strFileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
// The WebRequest
HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
oWebrequest.Method = "POST";
// This is important, otherwise the whole file will be read to memory anyway...
oWebrequest.AllowWriteStreamBuffering = false;
// Get a FileStream and set the final properties of the WebRequest
FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
oWebrequest.ContentLength = length;
Stream oRequestStream = oWebrequest.GetRequestStream();
// Write the post header
oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Stream the file contents in small pieces (4096 bytes, max).
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
int bytesRead = 0;
while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
oRequestStream.Write(buffer, 0, bytesRead);
oFileStream.Close();
// Add the trailing boundary
oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse oWResponse = oWebrequest.GetResponse();
Stream s = oWResponse.GetResponseStream();
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadToEnd();
// Clean up
oFileStream.Close();
oRequestStream.Close();
s.Close();
sr.Close();
return sReturnString;
}
|
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import org.bouncycastle.util.encoders.Base64;
import com.dfzxca.easyca.cas.certservice.cipher.Algorithm;
import com.dfzxca.easyca.cas.certservice.cipher.DataProcess;
import com.dfzxca.easyca.cas.certservice.cipher.KeyResource;
import com.dfzxca.easyca.cas.certservice.cipher.PbPvKey;
import com.dfzxca.easyca.cas.certservice.digitalenvelope.EnvelopeZip;
/**
* 签名验签类
* @author 黄通隆
*
*/
public class SignValidate {
public static void main(String[] args) {
String aa = "是EB18还是EA啊是EB18还是EA啊";
byte[] bb = aa.getBytes();
File file = new File("D:\\证书\\keystore");
FileInputStream fis;
try {
fis = new FileInputStream(file);
int size = fis.available();
byte pfx[] = new byte[size];
fis.read(pfx);
byte[] cc = certSignJksJKS("MD5","JKS",pfx,"changeit",aa);
System.out.println(cc.toString());
EnvelopeZip zip = new EnvelopeZip();
zip.unZip(cc);
boolean flg = signedDataVerifyJKS("MD5","JKS",pfx,"changeit",cc);
System.out.println(flg);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用jks/pfx密钥库文件进行签名
* @param arithmetic 摘要算法(MD5/SHA1)
* @param type 文件类型(JKS/PFX)
* @param keyfile 密钥文件
* @param Passwd 密码
* @param data 签名数据
* @return 签名ZIP包
*/
public static byte[] certSignJksJKS(String arithmetic,String type,byte[] keyfile, String Passwd,String data) {
X509Certificate cert = null;
String ref = null;
byte[] qmzip = null;
PrivateKey sedpvk = null;
String signdata = "error, please check jks file and password!";
KeyResource kc = new KeyResource();
PbPvKey pbpvk = new PbPvKey();
byte[] bytedata = data.getBytes();
try{
if(type.equals("JKS")){
//取得私钥
KeyStore keystore = kc.getKeystoreJks(keyfile, Passwd);
sedpvk = pbpvk.getPrivateKey(keystore , Passwd);
}else if(type.equals("PFX")){
KeyStore keystore = kc.getKeystorePfx(keyfile, Passwd);
sedpvk = pbpvk.getPrivateKey(keystore , Passwd);
}
//取得摘要
byte t[] = null;
if(arithmetic.equals("SHA1")){
DataProcess pss = DataProcess.getInstance(Algorithm.SHA1);
t = pss.digest(data.getBytes());
}else if(arithmetic.equals("MD5")){
DataProcess pss = DataProcess.getInstance(Algorithm.MD5);
t = pss.digest(data.getBytes());
}
byte[] zy = null;
zy = Base64.encode(t);
//摘要加密
ref = new String(Base64.encode(EncryptParse.decryptPreKeyRSA(sedpvk, zy)));
//把签名后的数据和原文写入文件
String qmTemp = "d:\\qm.txt";// 路径
String yTemp = "d:\\y.txt";// 路径
FileOutputStream qmfos = new FileOutputStream(qmTemp);
PrintWriter qmpw = new PrintWriter(qmfos);
// 写入
qmpw.write(ref);
qmpw.flush();
FileOutputStream yfos = new FileOutputStream(yTemp);
PrintWriter ypw = new PrintWriter(yfos);
// 写入
ypw.write(data);
ypw.flush();
//close
qmfos.close();
qmpw.close();
yfos.close();
ypw.close();
//打包
EnvelopeZip zip = new EnvelopeZip();
String[] zippah = {qmTemp,yTemp};
qmzip = zip.zip(zippah);
//删除文件
File fzip = new File(qmTemp);
if(fzip.isFile()){
fzip.delete();
}
File fzip1 = new File(yTemp);
if(fzip1.isFile()){
fzip1.delete();
}
} catch (Exception e) {
System.out.println("jks sign error!");
}
return qmzip;
}
/**
* 验签
* @param arithmetic 摘要算法(MD5/SHA1)
* @param type 文件类型(JKS/PFX)
* @param keyfile 密钥文件
* @param Passwd 密码
* @param zipdata ZIP包
* @return
*/
public static boolean signedDataVerifyJKS(String arithmetic,String type,byte[] keyfile, String Passwd,byte[] zipdata)
{
boolean verify=false;
try {
PublicKey pbk = null;
KeyResource kc = new KeyResource();
PbPvKey pbpvk = new PbPvKey();
Certificate sedpbk = null;
//解压ZIP包信息
EnvelopeZip zip = new EnvelopeZip();
zip.unZip(zipdata);
// 文件路径
File qmfile = new File("d:\\qm.txt");
File yfile = new File("d:\\y.txt");
// 将文件读入输入流
FileInputStream qmfis = new FileInputStream(qmfile);
InputStreamReader qmisr = new InputStreamReader(qmfis);
BufferedReader qmbr = new BufferedReader(qmisr);
//签名内容
StringBuffer qmbuf = new StringBuffer();
String temp = "";
for (int j = 1; (temp = qmbr.readLine()) != null; j++) {
qmbuf = qmbuf.append(temp);
}
FileInputStream yfis = new FileInputStream(yfile);
InputStreamReader yisr = new InputStreamReader(yfis);
BufferedReader ybr = new BufferedReader(yisr);
//原文内容
StringBuffer ybuf = new StringBuffer();
String ytemp = "";
for (int j = 1; (ytemp = ybr.readLine()) != null; j++) {
ybuf = ybuf.append(ytemp);
}
qmfis.close();
qmisr.close();
qmbr.close();
yfis.close();
yisr.close();
ybr.close();
//对原文进行摘要
byte t[] = null;
if(arithmetic.equals("SHA1")){
DataProcess pss = DataProcess.getInstance(Algorithm.SHA1);
t = pss.digest(ybuf.toString().getBytes());
}else if(arithmetic.equals("MD5")){
DataProcess pss = DataProcess.getInstance(Algorithm.MD5);
t = pss.digest(ybuf.toString().getBytes());
}
byte[] zy = null;
zy = Base64.encode(t);
//公钥
try {
if(type.equals("JKS")){
KeyStore keystore = kc.getKeystoreJks(keyfile, Passwd);
sedpbk = pbpvk.getPublicKey(keystore);
}else if(type.equals("PFX")){
KeyStore keystore = kc.getKeystorePfx(keyfile, Passwd);
sedpbk = pbpvk.getPublicKey(keystore);
}
pbk = sedpbk.getPublicKey();
} catch (Exception e) {
e.printStackTrace();
}
//公钥解密
byte[] de=EncryptParse.parsePubKeyRSA(pbk, Base64.decode(qmbuf.toString().getBytes()));
//比较2个摘要数据
if(new String(de).equals(new String(zy)))
{
verify=true;
}
//删除文件
File fzip = new File("d:\\qm.txt");
if(fzip.isFile()){
fzip.delete();
}
File fzip1 = new File("d:\\y.txt");
if(fzip1.isFile()){
fzip1.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return verify;
}
}
|
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CRLException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.dfzxca.easyca.cas.certservice.cipher.KeyResource;
import com.dfzxca.easyca.cas.certservice.cipher.PbPvKey;
/**
* 加密解密类
* @author HTL
*
*/
public class EncryptParse {
public static void main(String[] args) throws FileNotFoundException,
CertificateException, CRLException, IOException {
// Timer timer = new Timer();
try {
String aa = "是EB18还是EA啊是EB18还是EA啊";
byte[] bb = aa.getBytes();
File file = new File("D:\\keystore");
FileInputStream fis;
try {
fis = new FileInputStream(file);
int size = fis.available();
byte pfx[] = new byte[size];
fis.read(pfx);
byte[] jm = decryptRSA("JKS",pfx, "changeit", bb);
System.out.println(new String(jm));
String mm = parseRSA("JKS",pfx, "changeit", jm);
System.out.println(mm);
} catch (Exception e) {
e.printStackTrace();
}
//byte[] jm = decryptRSA("JKS","D:\\keystore", "changeit", bb);
//System.out.println(new String(jm));
//String mm = parseRSA("JKS","D:\\keystore", "changeit", jm);
//System.out.println(mm);
//byte[] cc = JavaDecryptP12RSA("D:\\Payment Mgr.p12","12345678",aa);
//System.out.println(new String(cc));
//String dd = JavaParseP12RSA("D:\\Payment Mgr.p12","12345678",cc);
//System.out.println(dd);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* JKS/PFX加密RSA
* @param type 文件类型(JKS/PFX)
* @param keyfile 密钥文件
* @param Passwd 文件密码
* @param data 要加密数据
* @return
*/
public static byte[] decryptRSA(String type,byte[] keyfile, String Passwd,byte[] data) {
PrivateKey sedpvk = null;
byte[] cipherdata = null;
KeyResource kc = new KeyResource();
PbPvKey pbpvk = new PbPvKey();
try {
if(type.equals("JKS")){
//取得私钥
KeyStore keystore = kc.getKeystoreJks(keyfile, Passwd);
sedpvk = pbpvk.getPrivateKey(keystore , Passwd);
}else if(type.equals("PFX")){
KeyStore keystore = kc.getKeystorePfx(keyfile, Passwd);
sedpvk = pbpvk.getPrivateKey(keystore , Passwd);
}
Cipher cp2= Cipher.getInstance("RSA" , new BouncyCastleProvider());
cp2.init(Cipher.ENCRYPT_MODE, sedpvk);
int blocksize = cp2.getBlockSize();
int outputsize = cp2.getOutputSize(data.length);
int lengthleft = data.length % blocksize;
int blocknum = lengthleft == 0 ? data.length / blocksize : data.length / blocksize + 1;
cipherdata = new byte [blocknum * outputsize];
for(int i = 0 ; i < blocknum ; i++)
{
if(data.length - blocksize * i > blocksize)
cp2.doFinal(data , i*blocksize , blocksize , cipherdata , i*outputsize);
else
cp2.doFinal(data , i*blocksize , data.length - i *blocksize , cipherdata , i*outputsize);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("jks sign error!");
}
return cipherdata;
}
/**
* JKS/PFX解密RSA
* @param type 文件类型(JKS/PFX)
* @param keyfile 密钥文件
* @param Passwd 文件密码
* @param data 已加密数据
* @return
*/
public static String parseRSA(String type,byte[] keyfile, String Passwd, byte[] data) {
byte[] bkey =null;
Certificate sedpbk = null;
PublicKey pbk = null;
KeyResource kc = new KeyResource();
PbPvKey pbpvk = new PbPvKey();
try {
try {
//取公钥
if(type.equals("JKS")){
KeyStore keystore = kc.getKeystoreJks(keyfile, Passwd);
sedpbk = pbpvk.getPublicKey(keystore);
}else if(type.equals("PFX")){
KeyStore keystore = kc.getKeystorePfx(keyfile, Passwd);
sedpbk = pbpvk.getPublicKey(keystore);
}
pbk = sedpbk.getPublicKey();
Cipher cp = Cipher.getInstance("RSA" , new BouncyCastleProvider());
cp.init(Cipher.DECRYPT_MODE, pbk);
int blocksize = cp.getBlockSize();
int outputsize = cp.getOutputSize(data.length);
int blocknum = data.length / blocksize;
int i = 0;
bkey = new byte[outputsize * blocknum];
while(data.length - i*blocksize > 0)
{
cp.doFinal(data , i*blocksize , blocksize , bkey , i*outputsize);
i++;
}
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(new String(bkey));
} catch (Exception e) {
e.printStackTrace();
System.out.println("jks sign error!");
}
return new String(bkey);
}
/**
* * 公钥解密RSA
*
* @param pk
* 解密的公钥
* @param raw
* 已经加密的数据
* @return 解密后的明文
* @throws Exception
*/
public static byte[] parsePubKeyRSA(PublicKey pk, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(cipher.DECRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
int j = 0;
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * 公钥加密 *
*
* @param key
* 加密的密钥 *
* @param data
* 待加密的明文数据 *
* @return 加密后的数据 *
* @throws Exception
*/
public static byte[] decryptPubKeyRSA(PublicKey pk, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();// 获得加密块大小,如:加密前数据为128个byte,而key_size=1024
// 加密块大小为127
// byte,加密后为128个byte;因此共有2个加密块,第一个127
// byte第二个为1个byte
int outputSize = cipher.getOutputSize(data.length);// 获得加密块加密后块大小
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i
* blockSize, raw, i * outputSize);
// 这里面doUpdate方法不可用,查看源代码后发现每次doUpdate后并没有什么实际动作除了把byte[]放到
// ByteArrayOutputStream中,而最后doFinal的时候才将所有的byte[]进行加密,可是到了此时加密块大小很可能已经超出了
// OutputSize所以只好用dofinal方法。
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * 私钥加密 *
*
* @param key
* 加密的密钥 *
* @param data
* 待加密的明文数据 *
* @return 加密后的数据 *
* @throws Exception
*/
public static byte[] decryptPreKeyRSA(PrivateKey pk, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();// 获得加密块大小,如:加密前数据为128个byte,而key_size=1024
// 加密块大小为127
// byte,加密后为128个byte;因此共有2个加密块,第一个127
// byte第二个为1个byte
int outputSize = cipher.getOutputSize(data.length);// 获得加密块加密后块大小
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i
* blockSize, raw, i * outputSize);
// 这里面doUpdate方法不可用,查看源代码后发现每次doUpdate后并没有什么实际动作除了把byte[]放到
// ByteArrayOutputStream中,而最后doFinal的时候才将所有的byte[]进行加密,可是到了此时加密块大小很可能已经超出了
// OutputSize所以只好用dofinal方法。
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
|
private enum SHGFI
{
SHGFI_ICON = 0x000000100, // get icon
SHGFI_DISPLAYNAME = 0x000000200, // get display name
SHGFI_TYPENAME = 0x000000400, // get type name
SHGFI_ATTRIBUTES = 0x000000800, // get attributes
SHGFI_ICONLOCATION = 0x000001000, // get icon location
SHGFI_EXETYPE = 0x000002000, // return exe type
SHGFI_SYSICONINDEX = 0x000004000, // get system icon index
SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon
SHGFI_SELECTED = 0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes
SHGFI_LARGEICON = 0x000000000, // get large icon
SHGFI_SMALLICON = 0x000000001, // get small icon
SHGFI_OPENICON = 0x000000002, // get open icon
SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon
SHGFI_PIDL = 0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x000000010 // use passed dwFileAttribute
}
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = " "; szTypeName = " ";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("Shell32.dll ")]
private static extern IntPtr SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
//取文件关联图标
public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.SHGFI_ICON | SHGFI.SHGFI_SMALLICON;//16*16小图标
else
flags = SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON;//32*32大图标
SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
public void GetFolderInfo(string path)
{
DirectoryInfo dir = Directory.CreateDirectory(path);
listView1.BeginUpdate();
listView1.Clear();
foreach (DirectoryInfo item in dir.GetDirectories())
{
listView1.Items.Add(item.Name, "folder");
}
foreach (FileInfo item in dir.GetFiles())
{
imageList1.Images.Add(item.Name, GetIcon(item.FullName, false));
listView1.Items.Add(item.Name, item.Name);
}
listView1.EndUpdate();
toolStripStatusLabel1.Text = listView1.Items.Count.ToString();
}
|
#tp{
border-radius: 10px;
border:0px;
box-shadow: 3px 3px 10px #999;
padding-left:10px;
padding-right:10px;
padding-top:5px;
font-family:微软雅黑;
height:200px;
width:400px;
background:-webkit-gradient(
linear,
left bottom,
left top,
color-stop(25%, rgb(235,197,101)),
color-stop(75%, rgb(240,161,24))
);
}
::-webkit-scrollbar
{
width: 10px;
height: 6px;
}
::-webkit-scrollbar-track-piece
{
background-color: rgb(235,197,101);
-webkit-border-radius: 6px;
}
::-webkit-scrollbar-thumb:vertical
{
height: 5px;
background-color: rgb(240,161,24);
-webkit-border-radius: 6px;
}
::-webkit-scrollbar-thumb:horizontal
{
width: 5px;
background-color: #CCCCCC;
-webkit-border-radius: 6px;
}
|