Encrypt with Java and decrypt with C #

Code that encrypts with Java to build a mechanism that decrypts the character string encrypted on the Java side for authentication of Android running on Java and API server running on .NET and performs authentication processing on the .Net side , I made a code to decrypt with C #.

Encrypt.java



    @TargetApi(Build.VERSION_CODES.O)
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String Encrypt(String text, String key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= key.getBytes(StandardCharsets.UTF_8);
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);

        byte[] results = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
        String result = Base64.getEncoder().encodeToString(results);
        return result;
    }
 

Decrypt.cs



       public static string Decrypt(string textToDecrypt, string key)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;

            //string decodedUrl = HttpUtility.UrlDecode(textToDecrypt);
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return encoding.GetString(plainText);
        }

Recommended Posts

Encrypt with Java and decrypt with C #
Encrypt / decrypt with AES256 in PHP and Java
Link Java and C ++ code with SWIG
How to encrypt and decrypt with RSA public key in Java
C # and Java Overrides Story
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Use JDBC with Java and Scala.
Kotlin post- and pre-increment and operator overload (comparison with C, Java, C ++)
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Call Java library from C with JNI
Let's try WebSocket with Java and javascript!
HTTPS with Spring Boot and Let's Encrypt
Java to C and C to Java in Android Studio
Java and JavaScript
XXE and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
How to make an app with a plugin mechanism [C # and Java]
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
I also tried WebAssembly with Nim and C
Summarize the differences between C # and Java writing
Prepare a scraping environment with Docker and Java
KMS) Envelope encryption with openssl and java decryption
[Java] Convert and import file values with OpenCSV
[Review] Reading and writing files with java (JDK6)
[Java] Align characters even with mixed half-width and full-width characters
Install java with Homebrew
Use fast Mapping library MapStruct with Lombok and Java 11
Getters and setters (Java)
Change seats with java
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Java] Thread and Runnable
Summary of ToString behavior with Java and Groovy annotations
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Java true and false
[Java] String comparison and && and ||
[Java] How to encrypt with AES encryption with standard library
Comfortable download with JAVA
[Java] Refer to and set private variables with reflection
Switch java with direnv
Java --Serialization and Deserialization
[Java] Arguments and parameters
Download Java with Ansible
I want to transition screens with kotlin and java!
timedatectl and Java TimeZone
[Java] Branch and repeat
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Let's scrape with Java! !!
Face recognition app made with Amazon Rekognition and Java
Build Java with Wercker
java (classes and instances)
[Java] Development with multiple files using package and import
Serverless Java EE starting with Quarkus and Cloud Run
[Java] Overload and override
Endian conversion with JAVA
Store in Java 2D map and turn with for statement
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Find the address class and address type from the IP address with Java