use of com.github.zhenwei.core.crypto.CryptoException in project LinLong-Java by zhenwei1108.
the class SM2Signer method generateSignature.
public byte[] generateSignature() throws CryptoException {
byte[] eHash = digestDoFinal();
BigInteger n = ecParams.getN();
BigInteger e = calculateE(n, eHash);
BigInteger d = ((ECPrivateKeyParameters) ecKey).getD();
BigInteger r, s;
ECMultiplier basePointMultiplier = createBasePointMultiplier();
// 5.2.1 Draft RFC: SM2 Public Key Algorithms
do // generate s
{
BigInteger k;
do // generate r
{
// A3
k = kCalculator.nextK();
// A4
ECPoint p = basePointMultiplier.multiply(ecParams.getG(), k).normalize();
// A5
r = e.add(p.getAffineXCoord().toBigInteger()).mod(n);
} while (r.equals(ZERO) || r.add(k).equals(n));
// A6
BigInteger dPlus1ModN = BigIntegers.modOddInverse(n, d.add(ONE));
s = k.subtract(r.multiply(d)).mod(n);
s = dPlus1ModN.multiply(s).mod(n);
} while (s.equals(ZERO));
// A7
try {
return encoding.encode(ecParams.getN(), r, s);
} catch (Exception ex) {
throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
}
}
use of com.github.zhenwei.core.crypto.CryptoException in project LinLong-Java by zhenwei1108.
the class DESExample method performDecrypt.
/*
* This method performs all the decryption and writes
* the plain text to the buffered output stream created
* previously.
*/
private void performDecrypt(byte[] key) {
// initialise the cipher for decryption
cipher.init(false, new KeyParameter(key));
/*
* As the decryption is from our preformatted file,
* and we know that it's a hex encoded format, then
* we wrap the InputStream with a BufferedReader
* so that we can read it easily.
*/
BufferedReader br = new BufferedReader(new InputStreamReader(in));
/*
* now, read the file, and output the chunks
*/
try {
int outL;
byte[] inblock = null;
byte[] outblock = null;
String rv = null;
while ((rv = br.readLine()) != null) {
inblock = Hex.decode(rv);
outblock = new byte[cipher.getOutputSize(inblock.length)];
outL = cipher.processBytes(inblock, 0, inblock.length, outblock, 0);
/*
* Before we write anything out, we need to make sure
* that we've got something to write out.
*/
if (outL > 0) {
out.write(outblock, 0, outL);
}
}
try {
/*
* Now, process the bytes that are still buffered
* within the cipher.
*/
outL = cipher.doFinal(outblock, 0);
if (outL > 0) {
out.write(outblock, 0, outL);
}
} catch (CryptoException ce) {
}
} catch (IOException ioeread) {
ioeread.printStackTrace();
}
}
use of com.github.zhenwei.core.crypto.CryptoException in project LinLong-Java by zhenwei1108.
the class DESExample method performEncrypt.
/*
* This method performs all the encryption and writes
* the cipher text to the buffered output stream created
* previously.
*/
private void performEncrypt(byte[] key) {
// initialise the cipher with the key bytes, for encryption
cipher.init(true, new KeyParameter(key));
/*
* Create some temporary byte arrays for use in
* encryption, make them a reasonable size so that
* we don't spend forever reading small chunks from
* a file.
*
* There is no particular reason for using getBlockSize()
* to determine the size of the input chunk. It just
* was a convenient number for the example.
*/
// int inBlockSize = cipher.getBlockSize() * 5;
int inBlockSize = 47;
int outBlockSize = cipher.getOutputSize(inBlockSize);
byte[] inblock = new byte[inBlockSize];
byte[] outblock = new byte[outBlockSize];
/*
* now, read the file, and output the chunks
*/
try {
int inL;
int outL;
byte[] rv = null;
while ((inL = in.read(inblock, 0, inBlockSize)) > 0) {
outL = cipher.processBytes(inblock, 0, inL, outblock, 0);
/*
* Before we write anything out, we need to make sure
* that we've got something to write out.
*/
if (outL > 0) {
rv = Hex.encode(outblock, 0, outL);
out.write(rv, 0, rv.length);
out.write('\n');
}
}
try {
/*
* Now, process the bytes that are still buffered
* within the cipher.
*/
outL = cipher.doFinal(outblock, 0);
if (outL > 0) {
rv = Hex.encode(outblock, 0, outL);
out.write(rv, 0, rv.length);
out.write('\n');
}
} catch (CryptoException ce) {
}
} catch (IOException ioeread) {
ioeread.printStackTrace();
}
}
use of com.github.zhenwei.core.crypto.CryptoException in project LinLong-Java by zhenwei1108.
the class JPAKEUtil method validateZeroKnowledgeProof.
/**
* Validates the zero knowledge proof (generated by {@link #calculateZeroKnowledgeProof(BigInteger,
* BigInteger, BigInteger, BigInteger, BigInteger, String, Digest, SecureRandom)}) is correct.
*
* @throws CryptoException if the zero knowledge proof is not correct
*/
public static void validateZeroKnowledgeProof(BigInteger p, BigInteger q, BigInteger g, BigInteger gx, BigInteger[] zeroKnowledgeProof, String participantId, Digest digest) throws CryptoException {
/* sig={g^v,r} */
BigInteger gv = zeroKnowledgeProof[0];
BigInteger r = zeroKnowledgeProof[1];
BigInteger h = calculateHashForZeroKnowledgeProof(g, gv, gx, participantId, digest);
if (!(// g^x > 0
gx.compareTo(ZERO) == 1 && // g^x < p
gx.compareTo(p) == -1 && // g^x^q mod q = 1
gx.modPow(q, p).compareTo(ONE) == 0 && /*
* Below, I took an straightforward way to compute g^r * g^x^h,
* which needs 2 exp. Using a simultaneous computation technique
* would only need 1 exp.
*/
g.modPow(r, p).multiply(gx.modPow(h, p)).mod(p).compareTo(gv) == // g^v=g^r * g^x^h
0)) {
throw new CryptoException("Zero-knowledge proof validation failed");
}
}
use of com.github.zhenwei.core.crypto.CryptoException in project LinLong-Java by zhenwei1108.
the class RSADigestSigner method generateSignature.
/**
* Generate a signature for the message we've been loaded with using the key we were initialised
* with.
*/
public byte[] generateSignature() throws CryptoException, DataLengthException {
if (!forSigning) {
throw new IllegalStateException("RSADigestSigner not initialised for signature generation.");
}
byte[] hash = new byte[digest.getDigestSize()];
digest.doFinal(hash, 0);
try {
byte[] data = derEncode(hash);
return rsaEngine.processBlock(data, 0, data.length);
} catch (IOException e) {
throw new CryptoException("unable to encode signature: " + e.getMessage(), e);
}
}
Aggregations