Convert Encryption with public key JAVA to C #

1

I need to send an encrypted hash in C # but the API only gives me this possibility in java, how can I follow this logic and apply in C #?

try {
        try {
            cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
        } catch (SecurityException se) {
            //workaround for tests
            cipher = Cipher.getInstance("RSA");
        }

        BufferedReader pemReader = null;
        pemReader = new BufferedReader(new InputStreamReader(
                new ByteArrayInputStream(publicKey.getBytes("UTF-8"))));

        StringBuffer content = new StringBuffer();
        String line = null;
        while ((line = pemReader.readLine()) != null) {
            if (line.indexOf("-----BEGIN PUBLIC KEY-----") != -1) {
                while ((line = pemReader.readLine()) != null) {
                    if (line.indexOf("-----END PUBLIC KEY") != -1) {
                        break;
                    }
                    content.append(line.trim());
                }
                break;
            }
        }
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT))));
        byte[] cipherText = cipher.doFinal(toHash().getBytes());

        return Base64.encodeToString(cipherText, Base64.DEFAULT);
    } catch (SecurityException e) {
        e.printStackTrace();
}
    
asked by anonymous 14.06.2018 / 16:48

0 answers