Possible Duplicate:
AES Encryption in Java and Decryption in C#
I have a file that is encrypted in C# alg has my IV and Key, the IV is 16 bytes and the Key is 32. The encryption properties are AES/CBC/PKCS7Padding. After doing the encryption I store off the keys since this is a 256byte encryption the bytes[] contains numbers ranging from 0 - 255. I have proven that if I encrypt the file using all number less than 128 my Java code will decrypt the file properly. However when i try to decrypt any files where the byte[] contains something that is larger than 127 it fails to decrypt because the keys are not correct. When I look at the bytes in Java they are auto converted to signed meaning a 241 is translated to a -14. I was thinking the bits should be the same and it should work but it does not. In the Java code I have placed the bytes in a string to verify they are exactly what is used in C# to Encrypt and that work to Decrypt on C#. They are but when placed into the byte[] that is passed they are translated.
Help please
bool Encrypt(string fileIn,string fileOut, Guid archiveKey, SymmetricAlgorithm alg) { // First we are going to open the file streams var fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read, FileShare.None); var fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
// Now create a crypto stream through which we are going
// to be pumping data.
// Our fileOut is going to be receiving the encrypted bytes.
var x = alg.CreateEncryptor();
var cs = new CryptoStream(fsOut,
x, CryptoStreamMode.Write);
// Now will will initialize a buffer and will be processing
// the input file in chunks.
// This is done to avoid reading the whole file (which can
// be huge) into memory.
bufferLen = Shared.GetFileAttributes.ProcessingBufferSize;
buffer = new byte[bufferLen];
do
{
// read a chunk of data from the input file
bytesRead = fsIn.Read(buffer, 0, bufferLen);
// encrypt it
cs.Write(buffer, 0, bytesRead);
var inc = bytesRead == Shared.GetFileAttributes.ProcessingBufferSize ? 1 : bytesRead / Shared.GetFileAttributes.ProcessingBufferSize;
} while (bytesRead != 0);
// close everything
// this will also close the unrelying fsOut stream
cs.Close();
fsIn.Close();
return true;
}
Java Code public void DecryptTheFile(String fileName, String fileOut, String keys) throws Exception
{
setupKeys(IV, KEY,keys);
// setup the cipher with the keys
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec key = new SecretKeySpec(KEY, "AES/CBC/PKCS7Padding");
IvParameterSpec ips = new IvParameterSpec(IV);
cipher.init(Cipher.DECRYPT_MODE, key,ips);
// decrypt a file
byte[] buffer = new byte[1024];
InputStream iFile = new FileInputStream(fileName);
OutputStream oFile = new FileOutputStream(fileOut);
iFile = new CipherInputStream(iFile, cipher);
int r = 0;
while ((r = iFile.read(buffer, 0, 1024)) > 0) {
oFile.write(buffer, 0, r);
}
oFile.close();
iFile.close();
}
private void setupKeys(byte[] IV,byte[] KEY,String keys)
{
String[] keyparts = keys.split(",");
for (int i = 0; i < 16; i++)
{
Long l = Long.parseLong(keyparts[i]);
IV[i] = (byte) (l.byteValue() );
if (siv == null) siv = l.toString();
else siv = siv + l.toString();
}
for (int i = 16; i < 47; i++)
{
Long l = Long.parseLong(keyparts[i]);
KEY[i - 16] = (byte) (l.byteValue() & 0xff);
if (skey == null ) skey = l.toString();
else skey = skey + l.toString();
}
}