0

I want sign a file using C#.Net and verify the file using Java. I am using SHA256withRSA algorithm for file sign process. I have a code both sign and verify using Java. But I need to convert file signing process to C#. I would be much grateful if somebody give me the guidance to resolve this matter.

File signing processing in Java

    File file = new File(fileLocation);
    signature = Signature.getInstance("SHA256withRSA");
    signature.initSign(privateKey, new SecureRandom());

    byte [] bytes;
    try (InputStream is = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()))) {
       bytes = new byte[1024];
        for (int read = 0; (read = is.read(bytes)) != -1;) {
            signature.update(bytes,0,read);
        }
    }

    byte[] signatureBytes = signature.sign();
    byte[] signatureLength = ByteBuffer.allocate(4).putInt(signatureBytes.length).array();

    byte[] version = ByteBuffer.allocate(4).putInt(1).array();
    String writeLoc = outPutLocation + File.separator + "TEST_" + FilenameUtils.getName(fileLocation);

    FileOutputStream outputStream = new FileOutputStream(new File(writeLoc));
    outputStream.write(version);
    outputStream.write(signatureLength);
    outputStream.write(signatureBytes);
    try (InputStream is = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()))) {
        bytes = new byte[1024];
        for (int read = 0; (read = is.read(bytes)) != -1;) {
            outputStream.write(bytes,0,read);
        }
    }
    outputStream.close();

File verify process in Java

    PublicKey publicKey = this.getPublicKey(stringKey);
    this.signatureOne = Signature.getInstance("SHA256withRSA");
    int signedDataLen = this.getSignatureLength(fileLocation);

    if (signedDataLen == 256) {
        byte[] signature = this.getSignatureBytes(fileLocation, signedDataLen);
        if (publicKey != null) {
            this.signatureOne.initVerify(publicKey);
            if (this.signatureOne.verify(signature)) {
                this.writeVerifiedFileToNewLocation(fileLocation, outPutLocation, signedDataLen);
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    } else {
        return false;
    }
sampathlk
  • 338
  • 2
  • 17
  • 3
    Usually questions "please provide code in different language" are downvoted or closed. Maybe you could try search for similar code or implement it yourself and then ask for details when you will get blocked – gusto2 Jun 05 '19 at 11:49
  • The duplicate should at least get you started on writing your .NET code. If you get stuck you can ask another question, this time you can include your .NET code. Good luck. – President James K. Polk Jun 05 '19 at 14:21
  • Thank you @JamesKPolk. After working for few days I could able to find the solution for this.There is no code change in the Java signed file verification part.In C#.Net only I had to do the generate RSACryptoServiceProvider object from the private raw key and sign the file.Cheers! – sampathlk Jun 14 '19 at 08:43

0 Answers0