I am trying to encode a message with SH1 RSA , but I have no experience with a security object, except for some basic information about RSA . I was given the private key as a String . I managed to write the following block of code to do this job, but I'm not sure if I am doing the job reliably and correctly.
I am not an expert, but I have placed my private key, since the line in the code is unsafe, I think. Can anyone guide me?
String privateKeyString = "mykeyhere..."; byte[] privateKeyBytes = privateKeyString.getBytes(); String encodedPrivateKey = Base64.encodeToString(privateKeyBytes, Base64.URL_SAFE); KeyFactory factory = KeyFactory.getInstance(RSA); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPrivateKey.getBytes()); RSAPrivateKey privateKey = (RSAPrivateKey) factory.generatePrivate(keySpec); Signature instance = Signature.getInstance(ALGORITHM); instance.initSign(privateKey); instance.update(content.getBytes()); return new String(instance.sign());
My private key has the form:
"-----BEGIN PRIVATE KEY-----\n"+ "MIIE...\n"+ "cH0iRj...\n"+ "O0Hhj...\n"+ . . . "fG6...\n"+ "B6/hF...\n"+ "3Mq38...\n"+ "-----END PRIVATE KEY-----\n"
source share