资源简介
java实现国密算法SM2,SM3,SM4算法,包含需要导的一个jar包,直接导入开发工具即可(eclipse,myeclipse等)
代码片段和文件信息
package com.mlq.sm;
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1876535 2018-04-04 21:20 bcprov-jdk16-1.46.jar
目录 0 2018-04-09 13:17 TestSM\
文件 464 2018-04-04 21:37 TestSM\.classpath
文件 382 2018-04-04 21:04 TestSM\.project
目录 0 2018-04-09 13:17 TestSM\.settings\
文件 598 2018-04-04 21:04 TestSM\.settings\org.eclipse.jdt.core.prefs
文件 1876535 2018-04-04 21:20 TestSM\bcprov-jdk16-1.46.jar
目录 0 2018-04-09 13:17 TestSM\bin\
目录 0 2018-04-09 13:17 TestSM\bin\com\
目录 0 2018-04-09 13:17 TestSM\bin\com\mlq\
目录 0 2018-04-09 13:17 TestSM\bin\com\mlq\sm\
文件 3390 2018-04-04 21:37 TestSM\bin\com\mlq\sm\Cipher.class
文件 3129 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM2.class
文件 4344 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM2Utils.class
文件 5297 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM3.class
文件 2997 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM3Digest.class
文件 8070 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM4.class
文件 4407 2018-04-04 21:53 TestSM\bin\com\mlq\sm\SM4Utils.class
文件 405 2018-04-04 21:37 TestSM\bin\com\mlq\sm\SM4_Context.class
文件 10074 2018-04-04 21:37 TestSM\bin\com\mlq\sm\Util.class
目录 0 2018-04-09 13:17 TestSM\src\
目录 0 2018-04-09 13:17 TestSM\src\com\
目录 0 2018-04-09 13:17 TestSM\src\com\mlq\
目录 0 2018-04-09 13:17 TestSM\src\com\mlq\sm\
文件 3180 2018-04-04 21:31 TestSM\src\com\mlq\sm\Cipher.java
文件 3380 2018-04-04 21:22 TestSM\src\com\mlq\sm\SM2.java
文件 4743 2018-04-04 21:23 TestSM\src\com\mlq\sm\SM2Utils.java
文件 9220 2018-04-04 21:24 TestSM\src\com\mlq\sm\SM3.java
文件 3957 2018-04-04 21:24 TestSM\src\com\mlq\sm\SM3Digest.java
文件 12787 2018-04-04 21:27 TestSM\src\com\mlq\sm\SM4.java
文件 6264 2018-04-04 21:53 TestSM\src\com\mlq\sm\SM4Utils.java
............此处省略2个文件信息
- 上一篇:flanagan.jar
- 下一篇:javaweb实训项目需求
评论
共有 条评论