Skip to content

Instantly share code, notes, and snippets.

@knight-ryu12
Created March 5, 2018 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knight-ryu12/1d0dcb9b3099107aade5d26562670471 to your computer and use it in GitHub Desktop.
Save knight-ryu12/1d0dcb9b3099107aade5d26562670471 to your computer and use it in GitHub Desktop.
Java BouncyCastle ECDSA sect233r1 signing
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.InvalidKeySpecException;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
public class Main {
/*
1) copy ctcert.bin into the ctcert section of the footer
2) hash the hashes in footer.bin
3) use the private key in ctcert.bin to sign the hash of the hashes (use bouncycastle for this??)
4) place the newly generated signature in the correct spot
*/
public static void main(String[] args) {
try {
DoSigning();
} catch (IOException | InvalidAlgorithmParameterException | NoSuchProviderException | NoSuchAlgorithmException | InvalidKeyException | SignatureException | InvalidKeySpecException e) {
System.out.println(e.getMessage());
System.exit(-1);
}
}
private static void DoSigning() throws IOException, InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException, InvalidKeySpecException {
byte [] footer = Files.readAllBytes(Paths.get("C:\\Users\\jason\\Desktop\\cryptor\\footer.bin"));
byte [] ctcert_bin = Files.readAllBytes(Paths.get("C:\\Users\\jason\\Desktop\\cryptor\\ctcert.bin"));
byte[] public_key = new byte[0x3C];
System.arraycopy(ctcert_bin, 0x108, public_key, 0, 0x3C);
byte[] privkey = new byte[0x1E];
for (int i = 0; i < 0x1E; i++)
privkey[i] = ctcert_bin[0x180 + 0x1E - 1 - i];
BigInteger private_key = new BigInteger(privkey);
byte[] hashes = new byte[14*0x20];
System.arraycopy(footer, 0, hashes, 0, 14*0x20);
MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-256"); } catch (Exception e) {}
byte[] hash_of_hashes = md.digest(hashes);
System.out.print("The hash of the hashes --> ");
for (byte b : hash_of_hashes) System.out.printf("%X", b);
System.out.println();
//======== ECDSA CODE =======
Security.addProvider(new BouncyCastleProvider());
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("sect233r1");
KeyFactory factory = KeyFactory.getInstance("ECDSA", "BC");
ECParameterSpec params = new ECParameterSpec();
ECPrivateKeySpec spec = new ECPrivateKeySpec(private_key, params);
PrivateKey privKey = factory.generatePrivate(spec);
System.out.println(privKey.toString());
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA", "BC");
ecdsaSign.initSign(privKey);
ecdsaSign.update(hash_of_hashes);
byte[] signature = ecdsaSign.sign();
System.out.print("The signature of the hash of the hashes --> ");
for (byte b : signature) System.out.printf("%X", b);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment