资源简介
Java版国密算法SM2、SM3、SM4源代码,包含测试案例及算法相关文档。
100%可用。
代码片段和文件信息
package com.security.cipher;
import java.math.BigInteger;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.math.ec.ECPoint;
public class Cipher {
private int ct;
private ECPoint p2;
private SM3Digest sm3keybase;
private SM3Digest sm3c3;
private byte key[];
private byte keyOff;
public Cipher() {
this.ct = 1;
this.key = new byte[32];
this.keyOff = 0;
}
private void Reset() {
this.sm3keybase = new SM3Digest();
this.sm3c3 = new SM3Digest();
byte p[] = Util.byteConvert32Bytes(p2.getX().toBigInteger());
this.sm3keybase.update(p 0 p.length);
this.sm3c3.update(p 0 p.length);
p = Util.byteConvert32Bytes(p2.getY().toBigInteger());
this.sm3keybase.update(p 0 p.length);
this.ct = 1;
NextKey();
}
private void NextKey() {
SM3Digest sm3keycur = new SM3Digest(this.sm3keybase);
sm3keycur.update((byte) (ct >> 24 & 0xff));
sm3keycur.update((byte) (ct >> 16 & 0xff));
sm3keycur.update((byte) (ct >> 8 & 0xff));
sm3keycur.update((byte) (ct & 0xff));
sm3keycur.doFinal(key 0);
this.keyOff = 0;
this.ct++;
}
public ECPoint Init_enc(SM2 sm2 ECPoint userKey) {
AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator
.generateKeyPair();
ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters) key
.getPrivate();
ECPublicKeyParameters ecpub = (ECPublicKeyParameters) key.getPublic();
BigInteger k = ecpriv.getD();
ECPoint c1 = ecpub.getQ();
this.p2 = userKey.multiply(k);
Reset();
return c1;
}
public void Encrypt(byte data[]) {
this.sm3c3.update(data 0 data.length);
for (int i = 0; i < data.length; i++) {
if (keyOff == key.length) {
NextKey();
}
data[i] ^= key[keyOff++];
}
}
public void Init_dec(BigInteger userD ECPoint c1) {
this.p2 = c1.multiply(userD);
Reset();
}
public void Decrypt(byte data[]) {
for (int i = 0; i < data.length; i++) {
if (keyOff == key.length) {
NextKey();
}
data[i] ^= key[keyOff++];
}
this.sm3c3.update(data 0 data.length);
}
public void Dofinal(byte c3[]) {
byte p[] = Util.byteConvert32Bytes(p2.getY().toBigInteger());
this.sm3c3.update(p 0 p.length);
this.sm3c3.doFinal(c3 0);
Reset();
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-05-15 16:48 cipher-sm\
文件 955 2018-05-15 16:21 cipher-sm\.classpath
文件 344 2018-04-26 18:02 cipher-sm\.gitignore
文件 561 2018-05-15 16:21 cipher-sm\.project
目录 0 2018-04-26 18:04 cipher-sm\.settings\
文件 243 2018-04-26 18:04 cipher-sm\.settings\org.eclipse.jdt.core.prefs
文件 90 2018-04-26 18:04 cipher-sm\.settings\org.eclipse.m2e.core.prefs
文件 1311 2018-04-26 18:02 cipher-sm\README.md
目录 0 2018-04-26 18:02 cipher-sm\doc\
文件 945731 2018-04-26 18:02 cipher-sm\doc\SM2椭圆曲线公钥密码算法.pdf
文件 22552 2018-04-26 18:02 cipher-sm\doc\SM2椭圆曲线公钥密码算法推荐曲线参数.pdf
文件 103677 2018-04-26 18:02 cipher-sm\doc\SM3密码杂凑算法.pdf
文件 205357 2018-04-26 18:02 cipher-sm\doc\SM4分组密码算法.pdf
文件 805 2018-05-15 16:22 cipher-sm\pom.xm
目录 0 2018-04-26 18:02 cipher-sm\src\
目录 0 2018-04-26 18:02 cipher-sm\src\main\
目录 0 2018-04-26 18:02 cipher-sm\src\main\java\
目录 0 2018-05-15 16:17 cipher-sm\src\main\java\com\
目录 0 2018-05-15 16:17 cipher-sm\src\main\java\com\security\
目录 0 2018-05-15 16:10 cipher-sm\src\main\java\com\security\cipher\
文件 2332 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\Cipher.java
文件 6259 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM2.java
文件 380 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM2Result.java
文件 5806 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM2Utils.java
文件 7702 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM3.java
文件 2601 2018-05-15 16:20 cipher-sm\src\main\java\com\security\cipher\SM3Digest.java
文件 9381 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM4.java
文件 3596 2018-05-15 16:20 cipher-sm\src\main\java\com\security\cipher\SM4Utils.java
文件 221 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\SM4_Context.java
文件 15184 2018-05-15 16:19 cipher-sm\src\main\java\com\security\cipher\Util.java
目录 0 2018-04-26 18:02 cipher-sm\src\test\
............此处省略29个文件信息
- 上一篇:Android百度地图画运动轨迹和GPS定位
- 下一篇:android电子拍卖
评论
共有 条评论