AES encryption Java to c#

前端 未结 2 1528
深忆病人
深忆病人 2021-01-07 15:47

I have the following code which encrypts/decrypts data for me in java

I need to encrypt/decrypt data in C# on a windows phone(8) device and same data should be able

2条回答
  •  余生分开走
    2021-01-07 15:58

    AES encryption can be done on C# as well as on Java, but there are some differences.

    Sadly now I remember it quite poor, but native wp7 cryptography had some problems with ciphers. I've written about it here and here. You'd better download BouncyCastle.Crypto library for wp7.


    after downloading that library, try this:

            private char[] keyValue = new char[] { 'S', 'D', 'P', 'i', 'b', 'm', 'B', 'H', 'A', 'R', 'T', 'I', 'P', 'K', 'e', 'y' };
            private char[] keyValue1 = new char[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
            private byte[] key;
            private byte[] key1;
    
            public AESPage()
            {
                InitializeComponent();
            }
    
            private void buttonEncrypt_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
                key = keyValue.Select(x => Convert.ToByte(x)).ToArray();
                key1 = keyValue1.Select(s => Convert.ToByte(s)).ToArray();
    
                String password = "encrypt_this";
                String passwordEnc = encrypt(GetBytes(password));
                String passwordDec = decrypt(GetBytes(passwordEnc));
            }
    
            private String encrypt(byte[] Data)
            {
                IBufferedCipher cipher = CipherUtilities.GetCipher("AES");
                cipher.Init(true, new KeyParameter(key));
                byte[] encVal = cipher.DoFinal(Data);
                MemoryStream memoryStream = new MemoryStream();
                //Encrypt Data 
                memoryStream.Write(encVal, 0, encVal.Length);
                memoryStream.Flush();
                //Return encrypted String 
                byte[] decryptBytes = memoryStream.ToArray();
                return GetString(decryptBytes);
            }
    
            private String decrypt(byte[] encryptedData)
            {
                IBufferedCipher cipher = CipherUtilities.GetCipher("AES");
                cipher.Init(false, new KeyParameter(key1));
                byte[] decValue = cipher.DoFinal(encryptedData);
                MemoryStream memoryStream = new MemoryStream();
                //Decrypt Data 
                memoryStream.Write(decValue, 0, decValue.Length);
                memoryStream.Flush();
                //Return decrypted String 
                byte[] decryptBytes = memoryStream.ToArray();
                return GetString(decryptBytes);
            }
    
            private byte[] GetBytes(string str)
            {
                byte[] bytes = new byte[str.Length * sizeof(char)];
                System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
                return bytes;
            }
    
            private String GetString(byte[] result)
            {
                return System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
            }
    

    I've written this for a simple windows-phone page with single button without writing additional class, like your AESencrp, but I think the idea is clear.

提交回复
热议问题