import java.lang.reflect.*;
成都創新互聯10多年企業網站制作服務;為您提供網站建設,網站制作,網頁設計及高端網站定制服務,企業網站制作及推廣,對酒店設計等多個領域擁有多年的網站制作經驗的網站建設公司。
/*******************************************************************************
* keyBean 類實現了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest
* 算法。
******************************************************************************/
public class keyBean {
/*
* 下面這些S11-S44實際上是一個4*4的矩陣,在原始的C實現中是用#define 實現的, 這里把它們實現成為static
* final是表示了只讀,切能在同一個進程空間內的多個 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 };
/*
* 下面的三個成員是keyBean計算過程中用到的3個核心數據,在原始的C實現中 被定義到keyBean_CTX結構中
*/
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的唯一一個公共成員,是最新一次計算結果的 16進制ASCII表示.
*/
public String digestHexStr;
/*
* digest,是最新一次計算結果的2進制內部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];
/*
* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數是你想要進行keyBean變換的字符串
* 返回的是變換完的結果,這個結果是從公共成員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這個類的標準構造函數,JavaBean要求有一個public的并且沒有參數的構造函數
public keyBean() {
keyBeanInit();
return;
}
/* keyBeanInit是一個初始化函數,初始化核心變量,裝入標準的幻數 */
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個基本的keyBean函數,在原始的keyBean的C實現中,由于它們是
* 簡單的位運算,可能出于效率的考慮把它們實現成了宏,在java中,我們把它們 實現成了private方法,名字保持了原來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將調用F,G,H,I進行近一步變換 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的主計算過程,inbuf是要變換的字節串,inputlen是長度,這個
* 函數由getkeyBeanofStr調用,調用之前需要調用keyBeaninit,因此把它設計成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整理和填寫輸出結果
*/
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是一個內部使用的byte數組的塊拷貝函數,從input的inpos開始把len長度的
* 字節拷貝到output的outpos位置開始
*/
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調用,block是分塊的原始字節
*/
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數組按順序拆成byte數組,因為java的long類型是64bit的, 只拆低32bit,以適應原始C實現的用途
*/
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數組按順序合成成long數組,因為java的long類型是64bit的,
* 只合成低32bit,高32bit清零,以適應原始C實現的用途
*/
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是我寫的一個把byte按照不考慮正負號的原則的”升位”程序,因為java沒有unsigned運算
*/
public static long b2iu(byte b) {
return b 0 ? b 0x7F + 128 : b;
}
/*
* byteHEX(),用來把一個byte類型的數轉換成十六進制的ASCII表示,
* 因為java中的byte的toString無法實現這一點,我們又沒有C語言中的 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) { // 如果沒有參數,執行標準的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]));
}
}
java常見的排序分為:
1 插入類排序
主要就是對于一個已經有序的序列中,插入一個新的記錄。它包括:直接插入排序,折半插入排序和希爾排序
2 交換類排序
這類排序的核心就是每次比較都要“交換”,在每一趟排序都會兩兩發生一系列的“交換”排序,但是每一趟排序都會讓一個記錄排序到它的最終位置上。它包括:起泡排序,快速排序
3 選擇類排序
每一趟排序都從一系列數據中選擇一個最大或最小的記錄,將它放置到第一個或最后一個為位置交換,只有在選擇后才交換,比起交換類排序,減少了交換記錄的時間。屬于它的排序:簡單選擇排序,堆排序
4 歸并類排序
將兩個或兩個以上的有序序列合并成一個新的序列
5 基數排序
主要基于多個關鍵字排序的。
下面針對上面所述的算法,講解一些常用的java代碼寫的算法
二 插入類排序之直接插入排序
直接插入排序,一般對于已經有序的隊列排序效果好。
基本思想:每趟將一個待排序的關鍵字按照大小插入到已經排序好的位置上。
算法思路,從后往前先找到要插入的位置,如果小于則就交換,將元素向后移動,將要插入數據插入該位置即可。時間復雜度為O(n2),空間復雜度為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;
// 每次比較都是對于已經有序的
while (j = 0 data[j] temp) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = temp;
}
// 輸出排序好的數據
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
三 插入類排序之折半插入排序(二分法排序)
條件:在一個已經有序的隊列中,插入一個新的元素
折半插入排序記錄的比較次數與初始序列無關
思想:折半插入就是首先將隊列中取最小位置low和最大位置high,然后算出中間位置mid
將中間位置mid與待插入的數據data進行比較,
如果mid大于data,則就表示插入的數據在mid的左邊,high=mid-1;
如果mid小于data,則就表示插入的數據在mid的右邊,low=mid+1
最后整體進行右移操作。
時間復雜度O(n2),空間復雜度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 };
// 存放臨時要插入的元素數據
int temp;
int low, mid, high;
for (int i = 1; i data.length; i++) {
temp = data[i];
// 在待插入排序的序號之前進行折半插入
low = 0;
high = i - 1;
while (low = high) {
mid = (low + high) / 2;
if (temp data[mid])
high = mid - 1;
else
// low=high的時候也就是找到了要插入的位置,
// 此時進入循環中,將low加1,則就是要插入的位置了
low = mid + 1;
}
// 找到了要插入的位置,從該位置一直到插入數據的位置之間數據向后移動
for (int j = i; j = low + 1; j--)
data[j] = data[j - 1];
// low已經代表了要插入的位置了
data[low] = temp;
}
for (int k = 0; k data.length; k++) {
System.out.print(data[k] + " ");
}
}
}
四 插入類排序之希爾排序
希爾排序,也叫縮小增量排序,目的就是盡可能的減少交換次數,每一個組內最后都是有序的。
將待續按照某一種規則分為幾個子序列,不斷縮小規則,最后用一個直接插入排序合成
空間復雜度為O(1),時間復雜度為O(nlog2n)
算法先將要排序的一組數按某個增量d(n/2,n為要排序數的個數)分成若干組,每組中記錄的下標相差d.對每組中全部元素進行直接插入排序,然后再用一個較小的增量(d/2)對它進行分組,在每組中再進行直接插入排序。當增量減到1時,進行直接插入排序后,排序完成。
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)
{
//利用這個在將組內倍數減小
//這里依次為5,3,2,1
d1 = Math.ceil(d1 / 2);
//d為增量每個分組之間索引的增量
int d = (int) d1;
//每個分組內部排序
for (int x = 0; x d; x++)
{
//組內利用直接插入排序
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]+" ");
}
}
五 交換類排序之冒泡排序
交換類排序核心就是每次比較都要進行交換
冒泡排序:是一種交換排序
每一趟比較相鄰的元素,較若大小不同則就會發生交換,每一趟排序都能將一個元素放到它最終的位置!每一趟就進行比較。
時間復雜度O(n2),空間復雜度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;
// 排序的比較趟數,每一趟都會將剩余最大數放在最后面
for (int i = 0; i data.length - 1; i++) {
// 每一趟從開始進行比較,將該元素與其余的元素進行比較
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] + " ");
}
}
【方案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() ;
? ? ? // 執行簽名
? ? ? 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));
? ? ? // 驗證簽名
? ? ? 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數字簽名——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算法生
怎么驗證生成的Ecdsa簽名是正確的呢,可以看下這篇文章:RSA,ECC,Ecdsa,國密SM2的簽名,驗簽,加密
/**??
*?冒泡法排序br/??
*?li比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。/li??
*?li對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最后一對。在這一點,最后的元素應該會是最大的數。/li??
*?li針對所有的元素重復以上的步驟,除了最后一個。/li??
*?li持續每次對越來越少的元素重復上面的步驟,直到沒有任何一對數字需要比較。/li??
*???
*?@param?numbers??
*????????????需要排序的整型數組??
*/??
public?static?void?bubbleSort(int[]?numbers)?{???
int?temp;?//?記錄臨時中間值???
int?size?=?numbers.length;?//?數組大小???
for?(int?i?=?0;?i??size?-?1;?i++)?{???
for?(int?j?=?i?+?1;?j??size;?j++)?{???
if?(numbers[i]??numbers[j])?{?//?交換兩數的位置???
temp?=?numbers[i];???
numbers[i]?=?numbers[j];???
numbers[j]?=?temp;???
}???
}???
}???
}
import java.util.Random;
public class BpDeep{
public double[][] layer;//神經網絡各層節點
public double[][] layerErr;//神經網絡各節點誤差
public double[][][] layer_weight;//各層節點權重
public double[][][] layer_weight_delta;//各層節點權重動量
public double mobp;//動量系數
public double rate;//學習系數
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();//隨機初始化權重
}
}
}
//逐層向前計算輸出
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];
}
//逐層反向計算誤差并修改權重
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];//隱含層動量調整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隱含層權重調整
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];//截距動量調整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距權重調整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//記錄誤差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
參考資料
當前文章:java常用算法代碼,Java基本算法
文章出自:http://vcdvsql.cn/article38/heopsp.html
成都網站建設公司_創新互聯,為您提供網站導航、企業網站制作、App開發、網站內鏈、軟件開發、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯