C# equivalent of the Java SecretKeySpec for AES
我有以下用Java编写的代码。我需要与此等效的C#。
1 2 3 4 5 | Key key = new SecretKeySpec(keyValue,"AES"); Cipher c = Cipher.getInstance("AES"); c.init(1, key); byte[] encVal = c.doFinal(Data.getBytes()); encryptedValue = new BASE64Encoder().encode(encVal); |
此处等效于Java中的C#代码。
1 2 3 4 5 6 7 8 9 | System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); AesManaged tdes = new AesManaged(); tdes.Key = UTF8.GetBytes(keyValue); tdes.Mode = CipherMode.ECB; tdes.Padding = PaddingMode.PKCS7; ICryptoTransform crypt = tdes.CreateEncryptor(); byte[] plain = Encoding.UTF8.GetBytes(text); byte[] cipher = crypt.TransformFinalBlock(plain, 0, plain.Length); String encryptedText = Convert.ToBase64String(cipher); |