Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DES encryption to Android shared preferences based on pull requ…
…est from darko1002001.
- Loading branch information
Showing
4 changed files
with
170 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.obscure.keychain; | ||
|
||
import java.security.spec.AlgorithmParameterSpec; | ||
import java.security.spec.KeySpec; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.PBEKeySpec; | ||
import javax.crypto.spec.PBEParameterSpec; | ||
|
||
import android.util.Base64; | ||
|
||
/** | ||
* This class defines methods for encrypting and decrypting using the Triple DES | ||
* algorithm and for generating, reading and writing Triple DES keys. It also | ||
* defines a main() method that allows these methods to be used from the command | ||
* line. | ||
*/ | ||
public class DesEncryptor { | ||
|
||
Cipher ecipher; | ||
|
||
Cipher dcipher; | ||
|
||
byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 }; | ||
|
||
public DesEncryptor(String passPhrase) throws Exception { | ||
int iterationCount = 2; | ||
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); | ||
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); | ||
ecipher = Cipher.getInstance(key.getAlgorithm()); | ||
dcipher = Cipher.getInstance(key.getAlgorithm()); | ||
|
||
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); | ||
|
||
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); | ||
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); | ||
} | ||
|
||
public String encrypt(String str) throws Exception { | ||
return Base64.encodeToString(ecipher.doFinal(str.getBytes()), Base64.DEFAULT); | ||
} | ||
|
||
public String decrypt(String str) throws Exception { | ||
return new String(dcipher.doFinal(Base64.decode(str, Base64.DEFAULT))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters