use of javax.crypto.Cipher in project UltimateAndroid by cymcsg.
the class TripleDES method decrypt.
/**
* Decrypt the message with TripleDES
*
* @param message
* @return
* @throws Exception
*/
public static String decrypt(byte[] message) throws Exception {
byte[] values = Base64decodingByte(message, 0);
final MessageDigest md = MessageDigest.getInstance("SHA-1");
final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
final byte[] keyBytes = copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8; ) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
String s1 = "12345678";
byte[] bytes = s1.getBytes();
final IvParameterSpec iv = new IvParameterSpec(bytes);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(values);
return new String(plainText, "UTF-8");
}
use of javax.crypto.Cipher in project UltimateAndroid by cymcsg.
the class TripleDES method decrypt.
/**
* Decrypt the message with TripleDES
*
* @param message
* @return
* @throws Exception
*/
public static String decrypt(String message) throws Exception {
if (message == null || message == "")
return "";
byte[] values = Base64decoding(message, 0);
final MessageDigest md = MessageDigest.getInstance("SHA-1");
final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
final byte[] keyBytes = copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8; ) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
String s1 = "12345678";
byte[] bytes = s1.getBytes();
final IvParameterSpec iv = new IvParameterSpec(bytes);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(values);
return new String(plainText, "UTF-8");
}
use of javax.crypto.Cipher in project android_frameworks_base by ParanoidAndroid.
the class AndroidKeyStoreTest method testKeyStore_KeyOperations_Wrap_Encrypted_Success.
public void testKeyStore_KeyOperations_Wrap_Encrypted_Success() throws Exception {
setupPassword();
mKeyStore.load(null, null);
setupKey();
// Test key usage
Entry e = mKeyStore.getEntry(TEST_ALIAS_1, null);
assertNotNull(e);
assertTrue(e instanceof PrivateKeyEntry);
PrivateKeyEntry privEntry = (PrivateKeyEntry) e;
PrivateKey privKey = privEntry.getPrivateKey();
assertNotNull(privKey);
PublicKey pubKey = privEntry.getCertificate().getPublicKey();
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.WRAP_MODE, pubKey);
byte[] expectedKey = new byte[] { 0x00, 0x05, (byte) 0xAA, (byte) 0x0A5, (byte) 0xFF, 0x55, 0x0A };
SecretKey expectedSecret = new SecretKeySpec(expectedKey, "AES");
byte[] wrappedExpected = c.wrap(expectedSecret);
c.init(Cipher.UNWRAP_MODE, privKey);
SecretKey actualSecret = (SecretKey) c.unwrap(wrappedExpected, "AES", Cipher.SECRET_KEY);
assertEquals(Arrays.toString(expectedSecret.getEncoded()), Arrays.toString(actualSecret.getEncoded()));
}
use of javax.crypto.Cipher in project MSEC by Tencent.
the class Tools method encryptWithPriKey.
public static byte[] encryptWithPriKey(byte[] buf, PrivateKey p) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
try {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, p);
return cipher.doFinal(buf);
} catch (Exception e) {
e.printStackTrace();
return new byte[0];
}
}
use of javax.crypto.Cipher in project MSEC by Tencent.
the class Tools method decryptWithPriKey.
public static byte[] decryptWithPriKey(byte[] buf, PublicKey p) {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
try {
SecureRandom random = new SecureRandom();
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, p, random);
return cipher.doFinal(buf);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Aggregations