convert binary to file p12 in Android - android

I get in one api a certificate digital with this value so:
response api imagen
I need convert this value to a physical file on Android device any folder how extension .p12
for after upload from the app.
finally read the file with next function FileInputStream:
try {
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream(filepath), etPassword.getText().toString().toCharArray());
} catch (Exception e) {
}
any help thank you very much

Related

Error While trying to stream encrypted video using exoplayer

I have been trying to stream encrypted video on my server using exoplayer.
I figured out everything about the custom data source and it is streaming other online videos which are not encrypted but when I am trying to decrypt the video I am getting an error
I only know the password string (Which is "abc123" I also have md5 of this)and I have no idea how should I exactly convert it to the key and then use it with my cipher
Error 1) When I am using password string as the key I am getting unsupported key size error,
2) When I am using md5 as key I am getting UnrecognizedInputFormatException
String sb="abc123";
byte []b=sb.getBytes();
mSecretKeySpec = new SecretKeySpec(b,"AES");
mIvParameterSpec = new IvParameterSpec("abc123".getBytes());
try {
mCipher = Cipher.getInstance("AES/CBC/NoPadding");
mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec,mIvParameterSpec);
} catch (Exception e) {
System.out.println(e.getMessage()+e.getCause()+"fuckeddd");
e.printStackTrace();
}
“abc123” is not a valid encryption key. An encryption key is a 16 byte binary value.

Android - unzip file during first app run

I have an apk file which contains zipped database (in asset folder). I want to unzip this database during first app run. For that case I am going to use zip4j like so:
public void unzipping() {
String source = "";//here source of zipped database
String destination = "";//here where database should be after installation
String password = "mypassword";
try {
ZipFile zipFile = new ZipFile(source);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}
The problem is that I don't know what correct path to specify in "source" and "destination".
There is nothing wrong with using encryption to protect user data. But understand, that the attacker can always decompile/memory dump your app and grab the password you are using to read/write to the database.
I'd a SQLCipher , which makes it transparent to the other parts of the app.

where can I upload json file?

I have wrote a json file with data I will get it to recycleview in android but I don't know where can I upload this file to access it into android project
There are 2 ways you can do that:
Local Storage: You can save a JSON file in your project locally. (already answered by others)
Upload your JSON: You can upload your JSON at jsonbin.io and it will generate an API that you can use in your project.
You can store JSON file in your assets folder......
void saveStringToFile(String path, String content) {
try {
File newFile = new File(path);
newFile.createNewFile();
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(content.getBytes());
fos.flush();
fos.close();
Constant.logD("File "+ newFile.getName()+ " is saved successfully at "+ newFile.getAbsolutePath());
} catch (Exception e) {
Constant.logE("Unable to save file", e);
}
}
Mention a path in a mobile sdcard like Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + System.currentTimeMillis()+".jpg" as path
Based on your requirement:
Best option is to host it on a (web server if you have one)
If you don't, share the file on GDrive or Dropbox (or similar hosting services which provide free storage). Share it with read-only access for your app to read from.
You can put your json file in the assets folder and best option is host the json file on the server and use the data (API) in your project.

Use new File() with Android Studio

I am getting a File Not Found exception when I try to access any files using Android Studio. I am able to open a text file with AssetManager but I need to open a p12 file for oAuth Authentication. The code I'm using is taken from https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_accounts
It seems that files can't be accessed this way in Android Studio? What is the alternative? I am trying to display events from a public calendar so I'm not even really sure I need oAuth (I didn't for a web app).
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(PlusScopes.PLUS_ME))
.setServiceAccountPrivateKeyFromP12File(new File(KEY))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
Log.d("FUCK", "cant open");
e.printStackTrace();
}
new File(KEY) creates a file object pointing to the path KEY. That file doesn't exist, so you have problems. If your p12 file is in assets, you'll need to copy it to your phone's file system first and pass in the file, rather than the asset name.

Convert HttpResponse to an .apk file

The problem is this:
I make an internet connection to some url and receive an HttpResponse with an app_example.apk.
Then I want to create a file (an .apk)
in the sdcard with this data so that this downloaded application
can be installed later.
How can I convert the HttpResponse to an .apk file?
Let's clear some details:
I have to get this apk file through an internet connection to my server
I don't want to install this applications I receive on the sdcard
All of this has to be done in my code, I cannot use android market
I am currently writing to that file.
What I'm doing is converting the HttpResponse to a byte[ ],
then that byte[ ] is written to a file (an .apk) using an ObjectOutputStream.
Like this:
// byte[] appByteArray - already has the internet response converted in bytes
try {
file = new File(Environment.getExternalStorageDirectory()+"/"+appName+".apk");
file.createNewFile();
FileOutputStream stream = null;
stream = new FileOutputStream(file, false);
ObjectOutputStream objectOut =
new ObjectOutputStream(new BufferedOutputStream(stream));
objectOut.writeObject(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
In the end, the file is created
and has the received content.
When I try to install it,
through a VIEW intent (using the default installer)
I get a parse error saying that it could not find the AndroidManifest.xml.
I think that in some step along the way, the received data is being corrupted.
Do you have another method to solve this?
Many thanks
Don't use an ObjectOutputStream, byte array is serialized as Object, not written as raw data.
Are you sure that you have SD card write permission? android.permission.WRITE_EXTERNAL_STORAGE
Don't write into SD card root directory. Number of files in root dir can be limited. Instead create you app subdirectory on SD CARD.
This code works for me:
try {
String filePath = Environment.getExternalStorageDirectory()
+ "/myappdir/" + appName + ".apk";
File file = new File(filePath);
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream objectOut = new BufferedOutputStream(
new FileOutputStream(file));
objectOut.write(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
This may not be the core problem, but I don't think you want to wrap stream in an ObjectOutputStream, since that is used for object serialization. It could be that it is adding extra data to the file so it can be deserialized with ObjectInputStream.
I would try pulling the apk off of the emulator (or device) and check it's MD5 versus the file on the server to make sure that the bits are being written out correctly.
Take a look at Pavel P's answer.
Also, I would note that your idea of installing the APK using the VIEW intent action does work, as I have tested this technique in the past.
However, unless the user has explicitly gone into Settings → Applications and selected "Allow non-Market applications", your installation will fail and the user will just see a screen telling them that for security reasons the installation has been blocked.
Basically you really need to rely on having fairly tech-savvy users who are willing to overlook a scary security warning and go and disable that setting.

Categories

Resources