Convert Publish JAVA key encryption to C#

Asked

Viewed 109 times

1

I need to send a hash encrypted in c# but the API so me this possibility in java, has how to 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();
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.