none
请问在wp7下怎样实现3des加密解密 RRS feed

  • 问题

  • 请各位大侠帮忙看看,真诚求教,在网上看见有用BouncyCastle Crypto library的方法,然后摘取3des算法,但是自己找不到这个算法
    2011年7月26日 7:29

答案

  • Encrypted Credential Store

    The Windows Phone SDK 7.1 Beta 2 now provides access to a set of cryptography APIs. For applications that require login credentials, these APIs enable you to store the credentials in an encrypted way. Now your users do not have to log in anew each time they use your application.

    Managed API documentation:


    Cedar
    2011年7月26日 9:01
    版主

全部回复

  • Here's the full class that will do unauthenticated string encryption using 256-bit AES keys with 8-byte salt values and 2000 key-derivation iterations . It has 2 public methods: EncryptString and DecryptString.

     

    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;

    namespace UtdUtils
    {
    public static class StringEncryption
    {
    public static readonly int KeyLengthBits = 256; //AES Key Length in bits
    public static readonly int SaltLength = 8; //Salt length in bytes
    private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

    public static string DecryptString(string ciphertext, string passphrase)
    {
    var inputs = ciphertext.Split(":".ToCharArray(), 3);
    var iv = Convert.FromBase64String(inputs[0]); // Extract the IV
    var salt = Convert.FromBase64String(inputs[1]); // Extract the salt
    var ciphertextBytes = Convert.FromBase64String(inputs[2]); // Extract the ciphertext

    // Derive the key from the supplied passphrase and extracted salt
    byte[] key = DeriveKeyFromPassphrase(passphrase, salt);

    // Decrypt
    byte[] plaintext = DoCryptoOperation(ciphertextBytes, key, iv, false);

    // Return the decrypted string
    return Encoding.UTF8.GetString(plaintext);
    }

    public static string EncryptString(string plaintext, string passphrase)
    {
    var salt = GenerateRandomBytes(SaltLength); // Random salt
    var iv = GenerateRandomBytes(16); // AES is always a 128-bit block size
    var key = DeriveKeyFromPassphrase(passphrase, salt); // Derive the key from the passphrase

    // Encrypt
    var ciphertext = DoCryptoOperation(Encoding.UTF8.GetBytes(plaintext), key, iv, true);

    // Return the formatted string
    return String.Format("{0}:{1}:{2}", Convert.ToBase64String(iv), Convert.ToBase64String(salt), Convert.ToBase64String(ciphertext));
    }

    private static byte[] DeriveKeyFromPassphrase(string passphrase, byte[] salt, int iterationCount = 2000)
    {
    var keyDerivationFunction = new Rfc2898DeriveBytes(passphrase, salt, iterationCount);//PBKDF2

    return keyDerivationFunction.GetBytes(KeyLengthBits / 8);
    }

    private static byte[] GenerateRandomBytes(int lengthBytes)
    {
    var bytes = new byte[lengthBytes];
    rng
    .GetBytes(bytes);

    return bytes;
    }

    // This function does both encryption and decryption, depending on the value of the "encrypt" parameter
    private static byte[] DoCryptoOperation(byte[] inputData, byte[] key, byte[] iv, bool encrypt)
    {
    byte[] output;

    using (var aes = new AesCryptoServiceProvider())
    using (var ms = new MemoryStream())
    {
    var cryptoTransform = encrypt ? aes.CreateEncryptor(key, iv) : aes.CreateDecryptor(key, iv);

    using (var cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
    cs
    .Write(inputData, 0, inputData.Length);

    output
    = ms.ToArray();
    }

    return output;
    }
    }
    }


    Cedar
    2011年7月26日 8:59
    版主
  • Encrypted Credential Store

    The Windows Phone SDK 7.1 Beta 2 now provides access to a set of cryptography APIs. For applications that require login credentials, these APIs enable you to store the credentials in an encrypted way. Now your users do not have to log in anew each time they use your application.

    Managed API documentation:


    Cedar
    2011年7月26日 9:01
    版主
  • 我需要的是3des的算法,而且现在开发的平台在wp7.0上,不好用那些类,请问还有别的方法吗?
    2011年7月26日 12:47
  • 我需要的是3des的算法,而且现在开发的平台在wp7.0上,不好用那些类,请问还有别的方法吗?
    你解决了吗?
    2011年12月15日 5:42