vendredi 28 novembre 2014

Need help converting c# encryption to java (android) encryption for cross compatibility


Vote count:

0




I've been attempting to convert this class (been using it in my c# app for a couple years now) to java for use in an android application: http://ift.tt/1z2KyTy


Some areas where I am having trouble is things like MemoryStream, CryptoStream, and more specifically:



//C# CODE
//Grab IV from message
var iv = new byte[ivLength];
Array.Copy(encryptedMessage, nonSecretPayloadLength, iv, 0, iv.Length);

using (var decrypter = aes.CreateDecryptor(cryptKey, iv))
using (var plainTextStream = new MemoryStream())
{
using (var decrypterStream = new CryptoStream(plainTextStream, decrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(decrypterStream))
{
//Decrypt Cipher Text from Message
binaryWriter.Write(
encryptedMessage,
nonSecretPayloadLength + iv.Length,
encryptedMessage.Length - nonSecretPayloadLength - iv.Length - sentTag.Length
);
}
//Return Plain Text
return plainTextStream.ToArray();
}


I am not quite sure what the java equivalent to CryptoStream or MemoryStream.


What I've got so far (pertaining to this code example, not the whole class)



//Grab IV from message
byte[] iv = new byte[ivLength];
System.arraycopy(encryptedMessage, nonSecretPayloadLength, iv, 0, iv.length);

Cipher aes = Cipher.getInstance("AES/CBC/PKCS7Padding");
// MISSING: create IvParameterSpec
IvParameterSpec ivSpec = new IvParameterSpec(iv);
iv = ivSpec.getIV();

aes.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivSpec);

//byte[] decoded = aes.doFinal(Base64.decodeBase64(encryptedMessage));

ByteArrayOutputStream decrypterStream = new ByteArrayOutputStream();
DataOutputStream binaryWriter = new DataOutputStream(decrypterStream);

binaryWriter.write(
encryptedMessage,
nonSecretPayloadLength + iv.length,
encryptedMessage.length - nonSecretPayloadLength - iv.length - sentTag.length
);


If anyone could give me some pointers/help id be most grateful.



asked 1 min ago







Need help converting c# encryption to java (android) encryption for cross compatibility

Aucun commentaire:

Enregistrer un commentaire