加密字符串AES Windows Phone 8.1 [英] Encrypt String AES Windows Phone 8.1

查看:164
本文介绍了加密字符串AES Windows Phone 8.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个需要加密字符串的应用程序。
我是AES加密的新功能。



我必须在服务器上运行的代码进行加密。

  public static string Encrypt(string text,byte [] key,byte [] iv,int keysize = 128,int blocksize = 128,CipherMode cipher = CipherMode.CBC,PaddingMode padding = PaddingMode.PKCS7)
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = blocksize;
aes.KeySize = keysize;
aes.Mode = cipher;
aes.Padding = padding;

byte [] src = Encoding.UTF8.GetBytes(text);
using(ICryptoTransform encrypt = aes.CreateEncryptor(key,iv))
{
byte [] dest = encrypt.TransformFinalBlock(src,0,src.Length);
encrypt.Dispose();
return Convert.ToBase64String(dest);
}
}

我已经有了这个,有一个图书馆Bouncy Castle,但是我似乎找不到一个简单的例子。

  byte [] key = Encoding.UTF8.GetBytes([SECRETKEY ]); 
byte [] iv = Encoding.UTF8.GetBytes([IV]);
var cipher = CipherUtilities.GetCipher(AES / CBC / PKCS7Padding);

cipher.Init(true,new KeyParameter(key));

如果可以帮助我也有运行Android客户端的代码。
让我知道,然后我可以发表。

解决方案

你可以使用PCLCrypto库,有一个nuget包可用(Install-Package PclCrypto)



https://github.com/AArnott/PCLCrypto/wiki/Crypto-Recipes


执行AES加密/解密




  byte [] keyMaterial; 
byte [] data;
var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(keyMaterial);
byte [] iv = null; //这是可选的,但加密和解密两者必须相同。
byte [] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key,data,iv);
byte [] plainText = WinRTCrypto.CryptographicEngine.Decrypt(key,cipherText,iv);

cipherText变量是加密数据,plainText是再次解密的加密变量


I'm making an app which will need to encrypt a string. I'm completely new to AES encryption.

I have to code that runs on the server to encrypt.

public static string Encrypt(string text, byte[] key, byte[] iv, int keysize = 128, int blocksize = 128, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
{
    AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
    aes.BlockSize = blocksize;
    aes.KeySize = keysize;
    aes.Mode = cipher;
    aes.Padding = padding;

    byte[] src = Encoding.UTF8.GetBytes(text);
    using (ICryptoTransform encrypt = aes.CreateEncryptor(key, iv))
    {
        byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
        encrypt.Dispose();
        return Convert.ToBase64String(dest);
    }
}

I already have this, with a library Bouncy Castle, but I can't seem to find a simple example.

byte[] key = Encoding.UTF8.GetBytes("[SECRETKEY]");
            byte[] iv = Encoding.UTF8.GetBytes("[IV]");
            var cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding");

            cipher.Init(true, new KeyParameter(key));

If it can help I also have the code which runs an Android client. Let me know then I can post it.

解决方案

You can use the PCLCrypto library, there's a nuget package available (Install-Package PclCrypto)

From https://github.com/AArnott/PCLCrypto/wiki/Crypto-Recipes:

Perform AES encryption/decryption

byte[] keyMaterial;
byte[] data;
var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(keyMaterial);
byte[] iv = null; // this is optional, but must be the same for both encrypting and decrypting
byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key, data, iv);
byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(key, cipherText, iv);

The cipherText variable is the encrypted data, plainText is the encrypted variable decrypted again

这篇关于加密字符串AES Windows Phone 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆