熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用JAVA數字證書做數字簽名認證

2022-06-13   來源: Java核心技術 

  

  keytool genkey keystore chinajavaworldkeystore alias chinajavaworld

  這個命令用來產生一個密匙庫執行完畢後會在當前操作目錄中產生一個chinajavaworldkeystore的文件在執行命令的時候還有提示你輸入密匙庫的密碼要記住後面還要用到

  

  keytool export keystore chinajavaworldkeystore

  alias chinajavaworld file chinajavaworldcer

  這個命令用來產生簽名時所要用的證書

  在JAVA裡操作將Cer內容改為BASE編碼

  //從密鑰庫中讀取CA證書

  String storepass = ;

  //前面設置的密碼

  FileInputStream in = new FileInputStream(e:\\license\\a\\chinajavaworldkeystore); KeyStore ks = KeyStoregetInstance(JKS); ksload(in storepasstoCharArray());

  //獲取證書 javasecuritycertCertificate c = ksgetCertificate(chinajavaworld);

  //BASE編碼 Systemoutprintln(StringUtilsencodeBase(cgetEncoded()));

  //將chinajavaworldcer內容改為這裡輸出的內容

  開始產生(測試)簽名

  Signature signature;

  try {

  InputStream streamCert = new javaioFileInputStream( e:\\license\\a\\testcer);

  CertificateFactory factory = CertificateFactorygetInstance(X);

  Certificate cert = factorygenerateCertificate(streamCert);

  Systemoutprintln(StringUtilsencodeBase(certgetEncoded()));

  signature = SignaturegetInstance(SHAwithDSA);

  signatureinitVerify(certgetPublicKey());

  //要簽名的指紋內容

  String sss = Welcome to wwThe java world for you forever;

  //獲取CA證書私鑰

  PrivateKey priKey=(PrivateKey)ksgetKey(teststorepasstoCharArray());

  Systemoutprintln(priKey:+StringUtilsencodeHex(priKeygetEncoded()));

  //用私鑰簽名

  sig = SignaturegetInstance(SHAwithDSA);

  siginitSign(priKey);

  ByteArrayOutputStream streamRaw = new ByteArrayOutputStream();

  DataOutputStream streamSig = new DataOutputStream(streamRaw);

  streamSigwriteUTF(sss); sigupdate(streamRawtoByteArray());

  String signatureS = StringUtilsencodeHex(sigsign());

  Systemoutprintln(signature: +signatureS);

  //用公鑰做驗證測試

  Systemoutprintln(pubKey:+StringUtilsencodeHex(certgetPublicKey()getEncoded()));

  ByteArrayOutputStream streamRaw = new ByteArrayOutputStream();

  DataOutputStream streamSig = new DataOutputStream(streamRaw);

  streamSigwriteUTF(sss); signatureupdate(streamRawtoByteArray());

  Systemoutprintln(verify: +signatureverify(StringUtilsdecodeHex(signatureS)));

  } catch(Exception e)

  {

  Systemoutprintln(e);

  }

  接下來你就可以把chinajavaworldcer和簽名放在你的產品目錄裡了認證的時候讀取cer證書中的公鑰對簽名內容進行認證就可以了

  附

  public static String encodeBase(byte data[]) {

  boolean lineSep = false;

  int sLen = data == null ? : datalength;

  (sLen == ) return new String();

  int eLen = (sLen / ) * ;

  int cCnt = (sLen ) / + << ;

  int dLen = cCnt + (lineSep ? (cCnt ) / << : );

  char dArr[] = new char[dLen];

  int s = ;

  int d = ;

  int cc = ;

  do {

  if(s >= eLen)

  break;

  int i = (data[s++] & xff) << | (data[s++] & xff) << | data[s++] & xff;

  dArr[d++] = CA[i >>> & xf];

  dArr[d++] = CA[i >>> & xf];

  dArr[d++] = CA[i >>> & xf];

  dArr[d++] = CA[i & xf];

  if(lineSep && ++cc == && d < dLen )

  { dArr[d++] = \r;

  dArr[d++] = \n;

  cc = ;

  }

  } while(true);

  int left = sLen eLen;

  if(left > )

  { int i = (data[eLen] & xff) << | (left != ? : (data[sLen ] & xff) << );

  dArr[dLen ] = CA[i >> ]; dArr[dLen ] = CA[i >>> & xf];

  dArr[dLen ] = left != ? = : CA[i & xf]; dArr[dLen ] = =;

  } return new String(dArr);

  }

  public static final String encodeHex(byte bytes[])

  { StringBuffer buf = new StringBuffer(byteslength * );

  for(int i = ; i < byteslength; i++) {

  if((bytes[i] & xff) < ) bufappend();

  bufappend(LongtoString(bytes[i] & xff ));

  }   return buftoString();

  }

  public static final byte[] decodeHex(String hex) {

  char chars[] = hextoCharArray();

  byte bytes[] = new byte[charslength / ];

  int byteCount = ;

  for(int i = ; i < charslength; i += ) {

  int newByte = ;

  newByte |= hexCharToByte(chars[i]);

  newByte <<= ;

  newByte |= hexCharToByte(chars[i + ]);

  bytes[byteCount] = (byte)newByte; byteCount++; }   return bytes; }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26624.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.