Java encryption and decryption in C # with Rijndael

Using the Rijndael algorithm, is it possible to encrypt the configuration file (or section in the configuration file) and then decrypt this file in Java? Assumptions can be made, for example:

  • Transition to IV (non-auto-generated idea :: GenerateIV ();)
  • Hand over the key
  • BlockSize - 128 (standard)

Assuming this can be done, my next question on this is:

  • Can keySize be 256? I know 128 is AES, but we would like to use 256. I also don’t know if Java has this provider for 256 or if I need to use BouncyCastle
  • What is a gasket? PKCS7?
  • I assume CiperMode will be CBC

Something like this in C #? But there is no clue if it can be decrypted in Java ... maybe even my C # is wrong?

public static void initCrypt() { byte[] keyBytes = System.Text.UTF8Encoding.UTF8.GetBytes("abcdefghijklmnop"); rijndaelCipher = new RijndaelManaged(); PasswordDeriveBytes pdb = new PasswordDeriveBytes(keyBytes, new SHA1CryptoServiceProvider().ComputeHash(keyBytes)); byte[] key = pdb.GetBytes(32); byte[] iv = pdb.GetBytes(16); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; //PaddingMode.PKCS7 or None or Zeros rijndaelCipher.KeySize = 256; //192, 256 rijndaelCipher.BlockSize = 128; rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = iv; } 
+4
source share
3 answers

I would check if this is supported by an external library like keyczar .

As Jeff Atwood taught us on his blog recently, 99% of developers should not treat themselves with the details of low-level encryption procedures (because we are likely to mess them up).

+2
source

Depending on the use of this configuration file, you may use an external program.

For example, if you want to protect the configuration file when it is on the disk, but you are in the order the contents of which are stored in memory while the program is running, you can use gpg to encrypt the file, decrypt it into memory using the user password required by the program when you start it, and then clear the memory when you turn off the program. [ 1]

[1] It is worth noting that there is no real way to guarantee that the contents will not be written to disk due to swap memory and the like. It depends on the operating system and the many factors that you can see if you are interested in it.

+1
source

Q1: It must be 128 or you have to use BouncyCastle

Q2: Yes PKCS7

Q3: Yes CBC

If your question is not dead, I can give you working examples of C # and java

0
source

Source: https://habr.com/ru/post/1286314/


All Articles