Note that there are some explanatory texts on larger screens.

plurals
  1. POExtra character in decrypted file from java using aes
    primarykey
    data
    text
    <p>I have written code in vb.net to encrypt a file from a memory stream. I also decrypt the file as well as copy the memory stream to a file to assure encryption/ decryption works. My vb solution works.</p> <p>However my need is to decrypt using Java. When I decrypt my file, I always get an extra "?" character at the very beginning of the file, but other than that the resullts are perfect. Has anyone seen anything like this before? I must admit, my results are from using only one set of data, but I've encrypted it twice using new keys and vectors both times.</p> <p>A few details. I'm using AES, PKCS7 padding in vb, and PKCS5 padding in Java. The file can be of arbitrary length. Any help is appreciated.</p> <p>I am posting this from my phone, and don't have the code handy. I can add it tomorrow. I'm just hoping that this description rings a bell with someone.</p> <p>Thanks, SH</p> <p>When I wrote to the MemoryStream in VB, I declared a StreamWriter like so:</p> <pre><code>Writer = New IO.StreamWriter(MS, System.Text.Encoding.UTF8) </code></pre> <p>Here's my VB.NET encryption function.</p> <pre><code> Public Shared Function WriteEncryptedFile(ms As MemoryStream, FileName As String) As List(Of Byte()) Try Dim original() As Byte Dim myAes As System.Security.Cryptography.Aes = Aes.Create() myAes.KeySize = 128 myAes.Padding = PadMode Dim keys As New List(Of Byte()) keys.Add(myAes.Key) keys.Add(myAes.IV) original = ms.ToArray Dim encryptor As ICryptoTransform = myAes.CreateEncryptor(myAes.Key, myAes.IV) Using FileEncrypt As New FileStream(FileName, FileMode.Create, FileAccess.Write) Using csEncrypt As New CryptoStream(FileEncrypt, encryptor, CryptoStreamMode.Write) csEncrypt.Write(original, 0, original.Length) csEncrypt.FlushFinalBlock() FileEncrypt.Flush() FileEncrypt.Close() csEncrypt.Close() End Using End Using Return keys Catch e As Exception MsgBox("Error during encryption." &amp; vbCrLf &amp; e.Message) End Try Return Nothing End Function </code></pre> <p>And here's the Java decryption:</p> <pre><code>public static void DecryptLIGGGHTSInputFile(String fileIn, String fileOut, String base64Key, String base64IV) throws Exception { // Get the keys from base64 text byte[] key = Base64.decodeBase64(base64Key); byte[] iv= Base64.decodeBase64(base64IV); // Read fileIn into a byte[] int len = (int)(new File(fileIn).length()); byte[] cipherText = new byte[len]; FileInputStream bs = new FileInputStream(fileIn); bs.read(cipherText, 1, len-1); System.out.println(cipherText.length); System.out.println((double)cipherText.length/128); bs.close(); // Create an Aes object // with the specified key and IV. Cipher cipher = null; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // Encrypt the message. SecretKey secret = new SecretKeySpec(key, "AES"); /* cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec); cipherText = cipher.doFinal("Hello, World!".getBytes("UTF-8")); System.out.println(cipherText); */ cipher.init(Cipher.DECRYPT_MODE, secret , new IvParameterSpec(iv)); String plaintext = new String(cipher.doFinal(cipherText), "UTF-8"); System.out.println(plaintext.length()); FileWriter fw = new FileWriter(fileOut); fw.write(plaintext); fw.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload