Read RSA private key of format PKCS1 in JAVA

Java does not come with out-of-the-box support for PKCS1 keys. You can however use Bouncycastle PEMParser pemParser = new PEMParser(new FileReader(privateKeyFile)); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(“BC”); Object object = pemParser.readObject(); KeyPair kp = converter.getKeyPair((PEMKeyPair) object); PrivateKey privateKey = kp.getPrivate();

Hash String via SHA-256 in Java

To hash a string, use the built-in MessageDigest class: import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.math.BigInteger; public class CryptoHash { public static void main(String[] args) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(“SHA-256”); String text = “Text to hash, cryptographically.”; // Change this to UTF-16 if needed md.update(text.getBytes(StandardCharsets.UTF_8)); byte[] digest = md.digest(); String hex = … Read more

Android 4.2 broke my encrypt/decrypt code and the provided solutions don’t work

First a disclaimer: DO NOT ever use SecureRandom to derive a key! This is broken and doesn’t make sense! The following block of code from the question tries to deterministically derive a key from a password, called the “seed” as the password is used to “seed” the random number generator. KeyGenerator keygen = KeyGenerator.getInstance(“AES”); SecureRandom … Read more