Android Oreo version 8 throws bad decrypt error in production. For other android versions code is working fine.
Algorithm used is "AES/CBC/PKCS5Padding".
public String decrypt(String _encryptedText, String _key, String _iv)
throws InvalidKeyException, UnsupportedEncodingException,
InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException {
//Log.d("enetered decryption", "enetered decryption");
return encryptDecrypt(_encryptedText, _key, EncryptMode.DECRYPT, _iv);
}
public String decryptionflag(String input, String iv) {
String output = "";
try {
// CryptLib _crypt = new CryptLib();
String plainText = input;
CryptLib _crypt = new CryptLib();
output = _crypt.decrypt(plainText, key, iv); // decrypt
//Log.d("decrypted flag is", "" + output);
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
Log.e("", "Exception" + e.getMessage());
} catch (InvalidKeyException e){
Log.e("", "Exception" + e.getMessage());
} catch (UnsupportedEncodingException e){
Log.e("", "Exception" + e.getMessage());
} catch (BadPaddingException e){
Log.e("", "Exception" + e.getMessage());
} catch (IllegalBlockSizeException e){
Log.e("", "Exception" + e.getMessage());
} catch (NoSuchAlgorithmException e){
Log.e("", "Exception" + e.getMessage());
} catch (NoSuchPaddingException e){
Log.e("", "Exception" + e.getMessage());
}
return output;
}
Keys used for encryption and decryption are same. And this code is working for UAT (android oreo version) but giving error for production.
Related
It's my code:
try {
System.out.println("Cloudinaryyyy 0000 :");
Map result = cloudinary.api().resources(ObjectUtils.asMap("resource_type","raw"));
System.out.println("Cloudinaryyyy :"+result);
}
catch (IOException e) {
Log.d("DownloadManager", "Error:" + e);
}
catch (NetworkOnMainThreadException e) {
System.out.println("Error 3" + e);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error 3" + e);
}
In the cloudinary it throws the exception.
Did you remember to configure the Cloudinary object?
Map config = new HashMap();
config.put("cloud_name", "n07t21i7");
config.put("api_key", "123456789012345");
config.put("api_secret", "abcdeghijklmnopqrstuvwxyz12");
Cloudinary cloudinary = new Cloudinary(config);
Also, you can try to run the sample projects on https://github.com/cloudinary/cloudinary_java
I can't find anything to read about /dev/graphics/fb0, below is my code .I already exec "chmod 777 /dev/graphics/fb0" before reading fb0; My test environment is Android 4.0 ,4.3 and 4.4.
Below is my code:
public static synchronized Bitmap getScreenShotBitmap() {
FileInputStream buf = null;
try {
fbFile = new File(FB0FILE1);
buf = new FileInputStream(fbFile);
dStream=new DataInputStream(buf);
dStream.readFully(piex); //Java NULLPOINTException
dStream.close();
for(int i=0;i<piex.length;i+=2)
{
colors[i/2]= (int)0xff000000 +
(int) (((piex[i+1]) << (16))&0x00f80000)+
(int) (((piex[i+1]) << 13)&0x0000e000)+
(int) (((piex[i]) << 5)&0x00001A00)+
(int) (((piex[i]) << 3)&0x000000f8);
}
return Bitmap.createBitmap(colors, screenWidth,screenHeight,Bitmap.Config.RGB_565);
} catch (FileNotFoundException e) {
LogUtil.e(TAG, "FileNotFoundException error",e);
} catch (IOException e) {
LogUtil.e(TAG, "IOException error",e);
}catch (Exception e) {
LogUtil.e(TAG, "Exception error",e);
}
finally {
if(buf!=null){
try {
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
Now this is the exception:
I want to encode my Url-params with Base64 and crypt(AES with CFB Mode), but it will not working and I can't get the error. Can someone help me? Here is my code:
private String toBase64Crypt(String cryptString) {
try {
SecretKeySpec key = new SecretKeySpec(pwd.getBytes("UTF8"), "AES");
byte[] cryptByte = cryptString.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
cryptString = Base64.encodeToString(cipher.doFinal(cryptByte),Base64.DEFAULT);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return cryptString;
}
This question already has an answer here:
Problems outsourcing RSA encryption and decryption
(1 answer)
Closed 9 years ago.
i'm developing an android app. Unfortunately i have a compatibility problem. My source code works with firmware 4.1, but crashes with devices with firmware 2.2. The error seems to be a "ClassNotFoundException" caused by the ObjectInputStream.
My sourcecode for the encryption and decryption:
private Key getPublicKey() {
try {
InputStream fis = activity.getResources().openRawResource(R.raw.publick);
ObjectInputStream ois = new ObjectInputStream(fis);
Key RSApublicKey = (Key) ois.readObject();
return RSApublicKey;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
private Key getPrivateKey() {
try {
InputStream fis = activity.getResources().openRawResource(R.raw.privatek);
ObjectInputStream ois = new ObjectInputStream(fis);
Key RSAprivateKey = (Key) ois.readObject();
return RSAprivateKey;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
Log.e("error", "Error: " + e);
e.printStackTrace();
}
return null;
}
public byte[] encrypt(String data) {
byte[] encrypted = null;
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());
encrypted = cipher.doFinal(data.getBytes());
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
catch (NoSuchProviderException e) {
e.printStackTrace();
}
return encrypted;
}
public String decrypt(byte[] encrypted) {
byte[] decrypted = null;
try {
Cipher cipher;
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
decrypted = cipher.doFinal(encrypted);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
catch (NoSuchProviderException e) {
e.printStackTrace();
}
return new String(decrypted);
}
The error in logcat:
03-23 10:13:19.706: E/Error(427): Error: java.lang.ClassNotFoundException: com.android.org.bouncycastle.jce.provider.JCERSAPrivateCrtKey
Do you have any ideas, how i could get it work with android 2.2?
You are better off using the Key.getEncoded() method to serialize the key. The Key.getFormat() already hints that RSA private keys should default to PKCS#8 encoding.
You can then use the PKCS8EncodedKeySpec class and an instance of an "RSA" KeyFactory to deserialize the resulting byte array.
The serialized keys may not work correctly over different implementations of PrivateKey (as you've already found out by now), and PKCS#8 is a well defined and often used standard.
Well, if it works on 4.1+ but not on 2.2, I guess it's a class from android that has been included after 2.2. You can test this by trying to compile with android 2.2 a project that uses this class and see if you can. If this is the case just find it and include it in your project.
From this codesnippet:
try {
EncryptionUtils smimeUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME);
char[] smimePw = new String("hello world").toCharArray();
EncryptionKeyManager smimeKeyMgr = smimeUtils.createKeyManager();
smimeKeyMgr.loadPrivateKeystore(privateKeyStore, smimePw);
} catch (NoSuchProviderException e) {
Log.e("NoSuchProvider: ", e.getMessage());
} catch (CertificateException e) {
Log.e("Certificate: ", e.getMessage());
} catch (KeyStoreException e) {
Log.e("KeyStore: ", e.getMessage());
} catch (NoSuchAlgorithmException e) {
Log.e("No Such Algorithm: ", e.getMessage());
} catch (IOException e) {
Log.e("IO: ", e.getMessage());
}
I try to load the S/MIME encryption manager, but this code throws an NoSuchProviderException telling me this:
No provider configured for S/MIME
I'm following this guide, which is telling me to add the following .jar-files:
javamail-crypto.jar
bcprov-jdk14-122.jar
bcmail-jdk14-122.jar
I couldn't find the extact version with the ending -122 but I included the following jar-files to my build path:
bcprov-jdk14-147
bcmail-jdk14-147
javamail-crypto
javamail-crypto-bouncycastle-smime
But it still throws the exception, could anybody give me a hint on how to solve this issue?