????????在下面的代碼中,把DSA的公鑰和私鑰保存在文件中,要使用該代碼你需要先得到DSA算法的公鑰和私鑰才行。其實下面的代碼就是JProbe 6.0.2 不完整分析的注冊機,本打算在看雪論壇混片精華,分析到后來才發現注冊文件在二進制代碼中有多處驗證,這不是我的強項,再加上年末事情較多,所以只好放棄。
public final class Codecs
{
??? private Codecs()
??? {
??? }
??? public static final byte[] base64Encode(byte abyte0[])
??? {
??????? if(abyte0 == null)
??????????? return null;
??????? byte abyte1[] = new byte[((abyte0.length + 2) / 3) * 4];
??????? int i = 0;
??????? int j = 0;
??????? for(; i < abyte0.length - 2; i += 3)
??????? {
??????????? abyte1[j++] = Base64EncMap[abyte0[i] >>> 2 & 0x3f];
??????????? abyte1[j++] = Base64EncMap[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
??????????? abyte1[j++] = Base64EncMap[abyte0[i + 2] >>> 6 & 3 | abyte0[i + 1] << 2 & 0x3f];
??????????? abyte1[j++] = Base64EncMap[abyte0[i + 2] & 0x3f];
??????? }
??????? if(i < abyte0.length)
??????? {
??????????? abyte1[j++] = Base64EncMap[abyte0[i] >>> 2 & 0x3f];
??????????? if(i < abyte0.length - 1)
??????????? {
??????????????? abyte1[j++] = Base64EncMap[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
??????????????? abyte1[j++] = Base64EncMap[abyte0[i + 1] << 2 & 0x3f];
??????????? } else
??????????? {
??????????????? abyte1[j++] = Base64EncMap[abyte0[i] << 4 & 0x3f];
??????????? }
??????? }
??????? for(; j < abyte1.length; j++)
??????????? abyte1[j] = 61;
??????? return abyte1;
??? }
??? public static final byte[] base64Decode(byte abyte0[])
??? {
??????? if(abyte0 == null)
??????????? return null;
??????? int i;
??????? for(i = abyte0.length; abyte0[i - 1] == 61; i--);
??????? byte abyte1[] = new byte[i - abyte0.length / 4];
??????? for(int j = 0; j < abyte0.length; j++)
??????????? abyte0[j] = Base64DecMap[abyte0[j]];
??????? int k = 0;
??????? int l;
??????? for(l = 0; l < abyte1.length - 2; l += 3)
??????? {
??????????? abyte1[l] = (byte)(abyte0[k] << 2 & 0xff | abyte0[k + 1] >>> 4 & 3);
??????????? abyte1[l + 1] = (byte)(abyte0[k + 1] << 4 & 0xff | abyte0[k + 2] >>> 2 & 0xf);
??????????? abyte1[l + 2] = (byte)(abyte0[k + 2] << 6 & 0xff | abyte0[k + 3] & 0x3f);
??????????? k += 4;
??????? }
??????? if(l < abyte1.length)
??????????? abyte1[l] = (byte)(abyte0[k] << 2 & 0xff | abyte0[k + 1] >>> 4 & 3);
??????? if(++l < abyte1.length)
??????????? abyte1[l] = (byte)(abyte0[k + 1] << 4 & 0xff | abyte0[k + 2] >>> 2 & 0xf);
??????? return abyte1;
??? }
??? private static byte Base64EncMap[];
??? private static byte Base64DecMap[];
??? static
??? {
??????? byte abyte0[] = {
??????????? 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
??????????? 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
??????????? 85, 86, 87, 88, 89, 90, 97, 98, 99, 100,
??????????? 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
??????????? 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
??????????? 121, 122, 48, 49, 50, 51, 52, 53, 54, 55,
??????????? 56, 57, 43, 47
??????? };
??????? Base64EncMap = abyte0;
??????? Base64DecMap = new byte[128];
??????? for(int i = 0; i < Base64EncMap.length; i++)
??????????? Base64DecMap[Base64EncMap[i]] = (byte)i;
??? }
}
import java.io.UnsupportedEncodingException;
public final class SignableBlock
{
??? private SignableBlock()
??? {
??? }
??? public static byte[] createSignableBlock(String as[])
??????? throws UnsupportedEncodingException
??? {
??????? StringBuffer stringbuffer = new StringBuffer(256);
??????? for(int i = 0; i < as.length; i++)
??????????? stringbuffer.append(as[i]);
??????? return stringbuffer.toString().getBytes("UTF-8");
??? }
}
import java.io.UnsupportedEncodingException;
import java.security.*;
final class ValidateSignature
{
??? private ValidateSignature()
??? {
??? }
???
??? public static void main(String[] args)
??? {
??? ?String[] as = {"qq:8117892","m.tkk7.com/galaxyp","DSA數字簽名"};
??? ?String s = "MCwCFFSpNp/n4Mq24FDXyVkk/kr815yHAhQO0TLslIJOqUes4OFn0ARvFaAVOw==";
??? ?try{
??? ??System.out.println(validateSignature(as,s));
??? ?}catch(Exception e){}
??? }
??? private static boolean validate(byte abyte0[], byte abyte1[])
??? {
??????? Signature signature = null;
??????? boolean flag = false;
??????? try
??????? {
??????? ?java.io.ObjectInputStream in=new java.io.ObjectInputStream(new java.io.FileInputStream("myprikey.dat"));
??????????? PrivateKey prikey=(PrivateKey)in.readObject();
??????????? in.close();
???????????
??????????? java.io.ObjectInputStream in2=new java.io.ObjectInputStream(new java.io.FileInputStream("mypubkey.dat"));
??????????? PublicKey pubkey=(PublicKey)in2.readObject();
??????????? in2.close();
???????????
??????????? signature = Signature.getInstance("SHA/DSA");
???????????
??????????? signature.initSign(prikey);
??????????? signature.update(abyte0);
??????????? byte[] bt = signature.sign();
??????????? String encode =new String(Codecs.base64Encode(bt),"UTF-8");
??????????? System.out.println(encode);
???????????
??????????? signature.initVerify(pubkey);
??????????? signature.update(abyte0);
???????????
??????????? flag = signature.verify(abyte1);
??????? }
??????? catch(Exception e)
??????? {?
??????????? return false;
??????? }
??????? return flag;
??? }
??? private static boolean validateSignatureBytes(String as[], byte abyte0[])
??????? throws UnsupportedEncodingException
??? {
??????? byte abyte1[] = SignableBlock.createSignableBlock(as);
??????? return validate(abyte1, abyte0);
??? }
??? public static boolean validateSignature(String as[], String s)
??????? throws UnsupportedEncodingException
??? {
??????? byte abyte0[];
??????? try
??????? {
??????????? abyte0 = Codecs.base64Decode(s.getBytes("UTF-8"));
??????? }
??????? catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
??????? {
??????????? return false;
??????? }
??????? return validateSignatureBytes(as, abyte0);
??? }
}