A c-sharp program for encrypting and decrypting files is transplanted to java.

now I need to transplant a c-sharp file encryption and decryption program to java, but I don"t know much about encryption and decryption.
this encryption and decryption logic is also a little twisted.

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    /// <summary>
    /// 
    /// </summary>
    public class CryptoHelpException : ApplicationException
    {
        public CryptoHelpException(string msg) : base(msg) { }
    }

    public class CryptoHelp
    {
        private const ulong FC_TAG = 0xFC010203040506CF;

        private const int BUFFER_SIZE = 128 * 1024;

        /// <summary>
        /// Byte
        /// </summary>
        /// <param name="b1">Byte</param>
        /// <param name="b2">Byte</param>
        /// <returns>true</returns>
        private static bool CheckByteArrays(byte[] b1, byte[] b2)
        {
            if (b1.Length == b2.Length)
            {
                for (int i = 0; i < b1.Length; PPi)
                {
                    if (b1[i] != b2[i])
                        return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// Rijndael SymmetricAlgorithm
        /// </summary>
        /// <param name="password"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        private static SymmetricAlgorithm CreateRijndael(string password, byte[] salt)
        {
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA256", 1000);

            SymmetricAlgorithm sma = Rijndael.Create();
            sma.KeySize = 256;
            sma.Key = pdb.GetBytes(32);
            sma.Padding = PaddingMode.PKCS7;
            return sma;
        }

        /// <summary>
        /// 
        /// </summary>
        private static RandomNumberGenerator rand = new RNGCryptoServiceProvider();

        /// <summary>
        /// Byte
        /// </summary>
        /// <param name="count">Byte</param>
        /// <returns>Byte</returns>
        private static byte[] GenerateRandomBytes(int count)
        {
            byte[] bytes = new byte[count];
            rand.GetBytes(bytes);
            return bytes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <param name="password"></param>
        public static void EncryptFile(string inFile, string outFile, string password)
        {
            using (FileStream fin = File.OpenRead(inFile),
                fout = File.OpenWrite(outFile))
            {
                long lSize = fin.Length; // 
                int size = (int)lSize;
                byte[] bytes = new byte[BUFFER_SIZE]; // 
                int read = -1; // 
                int value = 0;

                // IVsalt
                byte[] IV = GenerateRandomBytes(16);
                byte[] salt = GenerateRandomBytes(16);

                // 
                SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
                sma.IV = IV;

                // IVsalt
                fout.Write(IV, 0, IV.Length);
                fout.Write(salt, 0, salt.Length);

                // 
                HashAlgorithm hasher = SHA256.Create();
                using (CryptoStream cout = new CryptoStream(fout, sma.CreateEncryptor(), CryptoStreamMode.Write),
                    chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                {
                    BinaryWriter bw = new BinaryWriter(cout);
                    bw.Write(lSize);

                    bw.Write(FC_TAG);

                    // 
                    while ((read = fin.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        cout.Write(bytes, 0, read);
                        chash.Write(bytes, 0, read);
                        value += read;
                    }
                    // 
                    chash.Flush();
                    chash.Close();

                    // 
                    byte[] hash = hasher.Hash;

                    // 
                    cout.Write(hash, 0, hash.Length);

                    // 
                    cout.Flush();
                    cout.Close();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <param name="password"></param>
        public static void DecryptFile(string inFile, string outFile, string password)
        {
            // 
            using (FileStream fin = File.OpenRead(inFile),
                fout = File.OpenWrite(outFile))
            {

                int size = (int)fin.Length;
                byte[] bytes = new byte[BUFFER_SIZE];
                int read = -1;
                int value = 0;
                int outValue = 0;

                byte[] IV = new byte[16];
                fin.Read(IV, 0, 16);
                byte[] salt = new byte[16];
                fin.Read(salt, 0, 16);

                SymmetricAlgorithm sma = CryptoHelp.CreateRijndael(password, salt);
                sma.IV = IV;

                value = 32;
                long lSize = -1;

                // , 
                HashAlgorithm hasher = SHA256.Create();

                //using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read),
                //    chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
                //{
                CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read), chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write);
                // 
                BinaryReader br = new BinaryReader(cin);
                lSize = br.ReadInt64();
                ulong tag = br.ReadUInt64();

                if (FC_TAG != tag)
                    throw new CryptoHelpException("");

                long numReads = lSize / BUFFER_SIZE;

                long slack = (long)lSize % BUFFER_SIZE;

                for (int i = 0; i < numReads; PPi)
                {
                    read = cin.Read(bytes, 0, bytes.Length);
                    fout.Write(bytes, 0, read);
                    chash.Write(bytes, 0, read);
                    value += read;
                    outValue += read;
                }

                if (slack > 0)
                {
                    read = cin.Read(bytes, 0, (int)slack);
                    fout.Write(bytes, 0, read);
                    chash.Write(bytes, 0, read);
                    value += read;
                    outValue += read;
                }

                chash.Flush();
                chash.Close();

                fout.Flush();
                fout.Close();

                byte[] curHash = hasher.Hash;

                // 
                byte[] oldHash = new byte[hasher.HashSize / 8];
                read = cin.Read(oldHash, 0, oldHash.Length);
                if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
                    throw new CryptoHelpException("");
                cin.Flush();
                cin.Close();

                //}

                if (outValue != lSize)
                    throw new CryptoHelpException("");
            }
        }
    }
}

Apr.02,2021
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e93b02-1478.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e93b02-1478.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?