title: Verify DSA Signature generated by Java in C#
body: <p>I know this is a topic with lots of existing questions but I am failing to find any existing answers which cover my exact case.</p>
  
  <p>I need to sign a string (a URL) in some Java code and deliver the string along with the signature to a C# program.</p>
  
  <p>I run the following Java code once to generate a DSA keypair:</p>
  
  <pre><code>KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
  SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
  keyGen.initialize(1024, random);
  
  KeyPair pair = keyGen.generateKeyPair();
  PrivateKey priv = pair.getPrivate();
  PublicKey pub = pair.getPublic();
  
  /* save the private key in a file */
  byte[] privkey = priv.getEncoded();
  FileOutputStream privkeyfos = new FileOutputStream("key.priv");
  privkeyfos.write(privkey);
  privkeyfos.close();
  
  /* save the public key in a file */
  byte[] pubkey = pub.getEncoded();
  FileOutputStream pubkeyfos = new FileOutputStream("key.public");
  pubkeyfos.write(pubkey);
  pubkeyfos.close();
  </code></pre>
  
  <p>I am then using the following code to generate the signature.</p>
  
  <pre><code>public static String Sign(String keyPath, byte[] data)
  {
    FileInputStream keyfis = new FileInputStream(new File(keyPath, "key.priv"));
    byte[] encKey = new byte[keyfis.available()];
    keyfis.read(encKey);
    keyfis.close();
  
    PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encKey);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);
  
    Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
  
    dsa.initSign(privKey);
  
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    BufferedInputStream bufin = new BufferedInputStream(in);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = bufin.read(buffer)) &gt;= 0)
    {
      dsa.update(buffer, 0, len);
    }
    bufin.close();
  
    byte[] realSig = dsa.sign();
  
    return new String(Base64.encodeBase64(realSig), "UTF-8");
  }
  </code></pre>
  
  <p>In my C# code I have access to the string, the Base64 encoded signature and the "key.public" file from the first step.</p>
  
  <p>Can anyone provide a block of code which combines these elements along with a suitable library to determine whether the string has been tampered with?</p>