十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
import java.util.Random;
成都創(chuàng)新互聯(lián)于2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元祥云做網(wǎng)站,已為上家服務(wù),為祥云各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
public class BpDeep{
public double[][] layer;//神經(jīng)網(wǎng)絡(luò)各層節(jié)點(diǎn)
public double[][] layerErr;//神經(jīng)網(wǎng)絡(luò)各節(jié)點(diǎn)誤差
public double[][][] layer_weight;//各層節(jié)點(diǎn)權(quán)重
public double[][][] layer_weight_delta;//各層節(jié)點(diǎn)權(quán)重動(dòng)量
public double mobp;//動(dòng)量系數(shù)
public double rate;//學(xué)習(xí)系數(shù)
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//隨機(jī)初始化權(quán)重
}
}
}
//逐層向前計(jì)算輸出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐層反向計(jì)算誤差并修改權(quán)重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隱含層動(dòng)量調(diào)整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隱含層權(quán)重調(diào)整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距動(dòng)量調(diào)整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距權(quán)重調(diào)整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//記錄誤差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
參考資料
/**??
*?冒泡法排序br/??
*?li比較相鄰的元素。如果第一個(gè)比第二個(gè)大,就交換他們兩個(gè)。/li??
*?li對(duì)每一對(duì)相鄰元素作同樣的工作,從開(kāi)始第一對(duì)到結(jié)尾的最后一對(duì)。在這一點(diǎn),最后的元素應(yīng)該會(huì)是最大的數(shù)。/li??
*?li針對(duì)所有的元素重復(fù)以上的步驟,除了最后一個(gè)。/li??
*?li持續(xù)每次對(duì)越來(lái)越少的元素重復(fù)上面的步驟,直到?jīng)]有任何一對(duì)數(shù)字需要比較。/li??
*???
*?@param?numbers??
*????????????需要排序的整型數(shù)組??
*/??
public?static?void?bubbleSort(int[]?numbers)?{???
int?temp;?//?記錄臨時(shí)中間值???
int?size?=?numbers.length;?//?數(shù)組大小???
for?(int?i?=?0;?i??size?-?1;?i++)?{???
for?(int?j?=?i?+?1;?j??size;?j++)?{???
if?(numbers[i]??numbers[j])?{?//?交換兩數(shù)的位置???
temp?=?numbers[i];???
numbers[i]?=?numbers[j];???
numbers[j]?=?temp;???
}???
}???
}???
}
import java.lang.reflect.*;
/*******************************************************************************
* keyBean 類實(shí)現(xiàn)了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest
* 算法。
******************************************************************************/
public class keyBean {
/*
* 下面這些S11-S44實(shí)際上是一個(gè)4*4的矩陣,在原始的C實(shí)現(xiàn)中是用#define 實(shí)現(xiàn)的, 這里把它們實(shí)現(xiàn)成為static
* final是表示了只讀,切能在同一個(gè)進(jìn)程空間內(nèi)的多個(gè) Instance間共享
*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
/*
* 下面的三個(gè)成員是keyBean計(jì)算過(guò)程中用到的3個(gè)核心數(shù)據(jù),在原始的C實(shí)現(xiàn)中 被定義到keyBean_CTX結(jié)構(gòu)中
*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb
// first)
private byte[] buffer = new byte[64]; // input buffer
/*
* digestHexStr是keyBean的唯一一個(gè)公共成員,是最新一次計(jì)算結(jié)果的 16進(jìn)制ASCII表示.
*/
public String digestHexStr;
/*
* digest,是最新一次計(jì)算結(jié)果的2進(jìn)制內(nèi)部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];
/*
* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數(shù)是你想要進(jìn)行keyBean變換的字符串
* 返回的是變換完的結(jié)果,這個(gè)結(jié)果是從公共成員digestHexStr取得的.
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf.getBytes(), inbuf.length());
keyBeanFinal();
digestHexStr = "";
for (int i = 0; i 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是keyBean這個(gè)類的標(biāo)準(zhǔn)構(gòu)造函數(shù),JavaBean要求有一個(gè)public的并且沒(méi)有參數(shù)的構(gòu)造函數(shù)
public keyBean() {
keyBeanInit();
return;
}
/* keyBeanInit是一個(gè)初始化函數(shù),初始化核心變量,裝入標(biāo)準(zhǔn)的幻數(shù) */
private void keyBeanInit() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
/*
* F, G, H ,I 是4個(gè)基本的keyBean函數(shù),在原始的keyBean的C實(shí)現(xiàn)中,由于它們是
* 簡(jiǎn)單的位運(yùn)算,可能出于效率的考慮把它們實(shí)現(xiàn)成了宏,在java中,我們把它們 實(shí)現(xiàn)成了private方法,名字保持了原來(lái)C中的。
*/
private long F(long x, long y, long z) {
return (x y) | ((~x) z);
}
private long G(long x, long y, long z) {
return (x z) | (y (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
/*
* FF,GG,HH和II將調(diào)用F,G,H,I進(jìn)行近一步變換 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a s) | ((int) a (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a s) | ((int) a (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a s) | ((int) a (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a s) | ((int) a (32 - s));
a += b;
return a;
}
/*
* keyBeanUpdate是keyBean的主計(jì)算過(guò)程,inbuf是要變換的字節(jié)串,inputlen是長(zhǎng)度,這個(gè)
* 函數(shù)由getkeyBeanofStr調(diào)用,調(diào)用之前需要調(diào)用keyBeaninit,因此把它設(shè)計(jì)成private的
*/
private void keyBeanUpdate(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] 3) 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen 3)) (inputLen 3))
count[1]++;
count[1] += (inputLen 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen = partLen) {
keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
keyBeanTransform(buffer);
for (i = partLen; i + 63 inputLen; i += 64) {
keyBeanMemcpy(block, inbuf, 0, i, 64);
keyBeanTransform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
* keyBeanFinal整理和填寫輸出結(jié)果
*/
private void keyBeanFinal() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] 3) 0x3f;
padLen = (index 56) ? (56 - index) : (120 - index);
keyBeanUpdate(PADDING, padLen);
// /* Append length (before padding) */
keyBeanUpdate(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}
/*
* keyBeanMemcpy是一個(gè)內(nèi)部使用的byte數(shù)組的塊拷貝函數(shù),從input的inpos開(kāi)始把len長(zhǎng)度的
* 字節(jié)拷貝到output的outpos位置開(kāi)始
*/
private void keyBeanMemcpy(byte[] output, byte[] input, int outpos,
int inpos, int len) {
int i;
for (i = 0; i len; i++)
output[outpos + i] = input[inpos + i];
}
/*
* keyBeanTransform是keyBean核心變換程序,有keyBeanUpdate調(diào)用,block是分塊的原始字節(jié)
*/
private void keyBeanTransform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/*
* Encode把long數(shù)組按順序拆成byte數(shù)組,因?yàn)閖ava的long類型是64bit的, 只拆低32bit,以適應(yīng)原始C實(shí)現(xiàn)的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j len; i++, j += 4) {
output[j] = (byte) (input[i] 0xffL);
output[j + 1] = (byte) ((input[i] 8) 0xffL);
output[j + 2] = (byte) ((input[i] 16) 0xffL);
output[j + 3] = (byte) ((input[i] 24) 0xffL);
}
}
/*
* Decode把byte數(shù)組按順序合成成long數(shù)組,因?yàn)閖ava的long類型是64bit的,
* 只合成低32bit,高32bit清零,以適應(yīng)原始C實(shí)現(xiàn)的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) 8)
| (b2iu(input[j + 2]) 16) | (b2iu(input[j + 3]) 24);
return;
}
/*
* b2iu是我寫的一個(gè)把byte按照不考慮正負(fù)號(hào)的原則的”升位”程序,因?yàn)閖ava沒(méi)有unsigned運(yùn)算
*/
public static long b2iu(byte b) {
return b 0 ? b 0x7F + 128 : b;
}
/*
* byteHEX(),用來(lái)把一個(gè)byte類型的數(shù)轉(zhuǎn)換成十六進(jìn)制的ASCII表示,
* 因?yàn)閖ava中的byte的toString無(wú)法實(shí)現(xiàn)這一點(diǎn),我們又沒(méi)有C語(yǔ)言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib 4) 0X0F];
ob[1] = Digit[ib 0X0F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
keyBean m = new keyBean();
if (Array.getLength(args) == 0) { // 如果沒(méi)有參數(shù),執(zhí)行標(biāo)準(zhǔn)的Test Suite
System.out.println("keyBean Test suite:");
System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));
System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));
System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));
System.out.println("keyBean(\"message digest\"):"
+ m.getkeyBeanofStr("message digest"));
System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"
+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));
System.out
.println("keyBean(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"
+ m
.getkeyBeanofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));
}
}
【方案1】
package ECDSA;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class Ecdsa {
private static String src = "hello berber" ;
public static void main(String []args){
? jdkECDSA();
}
public static void jdkECDSA(){
? // 1.初始化密鑰
? try{
? ? ? KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
? ? ? keyPairGenerator.initialize(256);
? ? ? KeyPair keyPair = keyPairGenerator.generateKeyPair() ;
? ? ? ECPublicKey ecPublicKey = (ECPublicKey)keyPair.getPublic() ;
? ? ? ECPrivateKey ecPrivateKey = (ECPrivateKey)keyPair.getPrivate() ;
? ? ? // 執(zhí)行簽名
? ? ? PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
? ? ? KeyFactory keyFactory = KeyFactory.getInstance("EC") ;
? ? ? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec) ;
? ? ? Signature signature = Signature.getInstance("SHA1withECDSA");
? ? ? signature.initSign(privateKey);
? ? ? signature.update(src.getBytes());
? ? ? byte []arr = signature.sign();
? ? ? System.out.println("jdk ecdsa sign :"+ HexBin.encode(arr));
? ? ? // 驗(yàn)證簽名
? ? ? X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
? ? ? keyFactory = KeyFactory.getInstance("EC");
? ? ? PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
? ? ? signature = Signature.getInstance("SHA1withECDSA");
? ? ? signature.initVerify(publicKey);
? ? ? signature.update(src.getBytes());
? ? ? boolean bool = signature.verify(arr);
? ? ? System.out.println("jdk ecdsa verify:"+bool);
? }catch(Exception e){
? }
}
}
Java數(shù)字簽名——ECDSA算法
【方案2】
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
new MyTest().getSign();
}
void getSign() {
// Get the instance of the Key Generator with "EC" algorithm
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
g.initialize(kpgparams);
KeyPair pair = g.generateKeyPair();
// Instance of signature class with SHA256withECDSA algorithm
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initSign(pair.getPrivate());
System.out.println("Private Keys is::" + pair.getPrivate());
System.out.println("Public Keys is::" + pair.getPublic());
String msg = "text ecdsa with sha256";//getSHA256(msg)
ecdsaSign.update((msg + pair.getPrivate().toString())
.getBytes("UTF-8"));
byte[] signature = ecdsaSign.sign();
System.out.println("Signature is::"
+ new BigInteger(1, signature).toString(16));
// Validation
ecdsaSign.initVerify(pair.getPublic());
ecdsaSign.update(signature);
if (ecdsaSign.verify(signature))
System.out.println("valid");
else
System.out.println("invalid!!!!");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}}
java – 使用secp256r1曲線和SHA256算法生
怎么驗(yàn)證生成的Ecdsa簽名是正確的呢,可以看下這篇文章:RSA,ECC,Ecdsa,國(guó)密SM2的簽名,驗(yàn)簽,加密
java常見(jiàn)的排序分為:
1 插入類排序
主要就是對(duì)于一個(gè)已經(jīng)有序的序列中,插入一個(gè)新的記錄。它包括:直接插入排序,折半插入排序和希爾排序
2 交換類排序
這類排序的核心就是每次比較都要“交換”,在每一趟排序都會(huì)兩兩發(fā)生一系列的“交換”排序,但是每一趟排序都會(huì)讓一個(gè)記錄排序到它的最終位置上。它包括:起泡排序,快速排序
3 選擇類排序
每一趟排序都從一系列數(shù)據(jù)中選擇一個(gè)最大或最小的記錄,將它放置到第一個(gè)或最后一個(gè)為位置交換,只有在選擇后才交換,比起交換類排序,減少了交換記錄的時(shí)間。屬于它的排序:簡(jiǎn)單選擇排序,堆排序
4 歸并類排序
將兩個(gè)或兩個(gè)以上的有序序列合并成一個(gè)新的序列
5 基數(shù)排序
主要基于多個(gè)關(guān)鍵字排序的。
下面針對(duì)上面所述的算法,講解一些常用的java代碼寫的算法
二 插入類排序之直接插入排序
直接插入排序,一般對(duì)于已經(jīng)有序的隊(duì)列排序效果好。
基本思想:每趟將一個(gè)待排序的關(guān)鍵字按照大小插入到已經(jīng)排序好的位置上。
算法思路,從后往前先找到要插入的位置,如果小于則就交換,將元素向后移動(dòng),將要插入數(shù)據(jù)插入該位置即可。時(shí)間復(fù)雜度為O(n2),空間復(fù)雜度為O(1)
package sort.algorithm;
public class DirectInsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
int temp, j;
for (int i = 1; i data.length; i++) {
temp = data[i];
j = i - 1;
// 每次比較都是對(duì)于已經(jīng)有序的
while (j = 0 data[j] temp) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = temp;
}
// 輸出排序好的數(shù)據(jù)
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
三 插入類排序之折半插入排序(二分法排序)
條件:在一個(gè)已經(jīng)有序的隊(duì)列中,插入一個(gè)新的元素
折半插入排序記錄的比較次數(shù)與初始序列無(wú)關(guān)
思想:折半插入就是首先將隊(duì)列中取最小位置low和最大位置high,然后算出中間位置mid
將中間位置mid與待插入的數(shù)據(jù)data進(jìn)行比較,
如果mid大于data,則就表示插入的數(shù)據(jù)在mid的左邊,high=mid-1;
如果mid小于data,則就表示插入的數(shù)據(jù)在mid的右邊,low=mid+1
最后整體進(jìn)行右移操作。
時(shí)間復(fù)雜度O(n2),空間復(fù)雜度O(1)
package sort.algorithm;
//折半插入排序
public class HalfInsertSort {
public static void main(String[] args) {
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20 };
// 存放臨時(shí)要插入的元素?cái)?shù)據(jù)
int temp;
int low, mid, high;
for (int i = 1; i data.length; i++) {
temp = data[i];
// 在待插入排序的序號(hào)之前進(jìn)行折半插入
low = 0;
high = i - 1;
while (low = high) {
mid = (low + high) / 2;
if (temp data[mid])
high = mid - 1;
else
// low=high的時(shí)候也就是找到了要插入的位置,
// 此時(shí)進(jìn)入循環(huán)中,將low加1,則就是要插入的位置了
low = mid + 1;
}
// 找到了要插入的位置,從該位置一直到插入數(shù)據(jù)的位置之間數(shù)據(jù)向后移動(dòng)
for (int j = i; j = low + 1; j--)
data[j] = data[j - 1];
// low已經(jīng)代表了要插入的位置了
data[low] = temp;
}
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
四 插入類排序之希爾排序
希爾排序,也叫縮小增量排序,目的就是盡可能的減少交換次數(shù),每一個(gè)組內(nèi)最后都是有序的。
將待續(xù)按照某一種規(guī)則分為幾個(gè)子序列,不斷縮小規(guī)則,最后用一個(gè)直接插入排序合成
空間復(fù)雜度為O(1),時(shí)間復(fù)雜度為O(nlog2n)
算法先將要排序的一組數(shù)按某個(gè)增量d(n/2,n為要排序數(shù)的個(gè)數(shù))分成若干組,每組中記錄的下標(biāo)相差d.對(duì)每組中全部元素進(jìn)行直接插入排序,然后再用一個(gè)較小的增量(d/2)對(duì)它進(jìn)行分組,在每組中再進(jìn)行直接插入排序。當(dāng)增量減到1時(shí),進(jìn)行直接插入排序后,排序完成。
package sort.algorithm;
public class ShellSort {
public static void main(String[] args) {
int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };
double d1 = a.length;
int temp = 0;
while (true)
{
//利用這個(gè)在將組內(nèi)倍數(shù)減小
//這里依次為5,3,2,1
d1 = Math.ceil(d1 / 2);
//d為增量每個(gè)分組之間索引的增量
int d = (int) d1;
//每個(gè)分組內(nèi)部排序
for (int x = 0; x d; x++)
{
//組內(nèi)利用直接插入排序
for (int i = x + d; i a.length; i += d) {
int j = i - d;
temp = a[i];
for (; j = 0 temp a[j]; j -= d) {
a[j + d] = a[j];
}
a[j + d] = temp;
}
}
if (d == 1)
break;
}
for (int i = 0; i a.length; i++)
System.out.print(a[i]+" ");
}
}
五 交換類排序之冒泡排序
交換類排序核心就是每次比較都要進(jìn)行交換
冒泡排序:是一種交換排序
每一趟比較相鄰的元素,較若大小不同則就會(huì)發(fā)生交換,每一趟排序都能將一個(gè)元素放到它最終的位置!每一趟就進(jìn)行比較。
時(shí)間復(fù)雜度O(n2),空間復(fù)雜度O(1)
package sort.algorithm;
//冒泡排序:是一種交換排序
public class BubbleSort {
// 按照遞增順序排序
public static void main(String[] args) {
// TODO Auto-generated method stub
int data[] = { 2, 6, 10, 3, 9, 80, 1, 16, 27, 20, 13, 100, 37, 16 };
int temp = 0;
// 排序的比較趟數(shù),每一趟都會(huì)將剩余最大數(shù)放在最后面
for (int i = 0; i data.length - 1; i++) {
// 每一趟從開(kāi)始進(jìn)行比較,將該元素與其余的元素進(jìn)行比較
for (int j = 0; j data.length - 1; j++) {
if (data[j] data[j + 1]) {
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
for (int i = 0; i data.length; i++)
System.out.print(data[i] + " ");
}
}