I have an encrypted messenger app that Android P broke. In P any call to any call to
SecureRandom.getInstance("SHA1PRNG", "Crypto")
will throw NoSuchProviderException.
EDIT If we are not supposed to use SHA1PRNG, then what provider are we supposed to use ??
In my app the following method gets such an exception . . .
public static AESSeedAndMessage doAESEncrypt(String inString)
{
//returnes encrypted message along with UNencrypted seed
AESSeedAndMessage returnStuff = new AESSeedAndMessage();
returnStuff.seed = generateAESSeed(); //get a random seed
byte[] encodedBytes = null;
// Set up secret key spec for 128-bit AES encryption and decryption
SecretKeySpec sks = null;
try
{
SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG", new CryptoProvider() );
sr.setSeed(returnStuff.seed.getBytes());
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128, sr);
sks = new SecretKeySpec((kg.generateKey()).getEncoded(), "AES");
// Encode the original data with AES
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
encodedBytes = c.doFinal(inString.getBytes());
}
catch (Exception e)
{
returnStuff.text = "error";
return returnStuff;
}
returnStuff.text = Base64.encodeToString(encodedBytes, Base64.DEFAULT);
return returnStuff;
}
I find quite a bit of material on this issue but I haven't been able to find out what to do to replace the removal of that crypto provider. What do I need to do in order to make this method work, hopefully, in all versions of Android?
EDIT FYI I had a problem with this code back in Android N when crypto was eliminated and had to add the following class to make it work in N.
import java.security.Provider;
/**
* Created by gary on 12/30/2017.
*/
//this is because Android N eliminated crypto
public class CryptoProvider extends Provider {
/**
* Creates a Provider and puts parameters
*/
public CryptoProvider() {
super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
put("SecureRandom.SHA1PRNG",
"org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
}
}