Windows Phone 7 中的密码加密/解密

本文关键字:加密 解密 密码 Phone Windows | 更新日期: 2023-09-27 18:36:40

我们希望在Windows Phone 7中使用密码加密/解密。我们已经使用java为Android做了。但是当我们尝试用 c# 进行开发时,我们会苦苦挣扎。

我们的 Java 代码:

public AES()
    {
        try
        {
            Security.addProvider(new BouncyCastleProvider());
            cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public String doDecrypt(String key, String cipherText)
    {
        try
        {
            byte[] raw = key.getBytes(Charset.forName("UTF-8"));
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            cipher.init(Cipher.DECRYPT_MODE, skey );
            return new String(cipher.doFinal(Base64.decode(cipherText,Base64.DEFAULT)), Charset.forName("UTF-8"));
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String doEncrypt(String key, String plainText)
    {
        try
        {
            byte[] raw = key.getBytes(Charset.forName("UTF-8"));
            SecretKeySpec skey = new SecretKeySpec(raw, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, skey );
            return Base64.encodeToString(cipher.doFinal(plainText.getBytes(Charset.forName("UTF-8"))),Base64.DEFAULT);
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }
}

在这里我们可以加密和解密。

我们的 C# 代码是:

public static byte[] EncryptWithAES(string dataToEncrypt, String Key)
        {
            byte[] encryptedData;
            byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(Key);

            using (AesManaged aesEnc = new AesManaged())
            {
                aesEnc.Key = keyBytes;
                aesEnc.IV = new byte[16];
                //Create encryptor for converting
                ICryptoTransform encryptor = aesEnc.CreateEncryptor(aesEnc.Key, aesEnc.IV);

                using (MemoryStream memStream = new MemoryStream())
                {
                    using (CryptoStream crypStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter srmWriter = new StreamWriter(crypStream))
                        {
                            srmWriter.Write(dataToEncrypt);
                        }
                        encryptedData = memStream.ToArray();
                    }
                }
            }
            return encryptedData;
        }

但是在这里我们收到不同的输出。

Java OP:-

OYbW6pI8mgqU5xOcfG8N92e28T9GUObtcea4XWqU0yQyJRULSLV/yjAzDh8gq9Hgj5K5OubZfdm//ts66eQMJYH4TBX0/hN5zPwQbdTWmfVU3dDyU2SyQek5zYcWW+OgnppL9jcMcJZg4pv2+q6x8w==

C# 操作:-

OYbW6pI8mgqU5xOcfG8N9wXs2/gWMc6dcUSEoLXm3L5v9Ih9eN63xO31mXmEDLprIzusXaOS1rNNtBPi5I8FG3IukVgicagrkLul1vfa142z+XDULJXFmg5rxPa6iJzXqeZ6x3wxbfI3T/ZqGwxqbg==

我们无法像java那样获得确切的加密数据。请建议或提供Windows Phone 7中密码加密/解密的任何链接。

Windows Phone 7 中的密码加密/解密

它使用 LINQPad 对我有用:

Console.Write(String.Join(" ", EncryptWithAES("hello", "AAECAwQFBgcICQoLDA0ODw==")));

收益 率:

91 209 208 157 151 41 81 76 99 8 248 231 34 62 204 1

也许是Window Phone 7的特定原因导致它无法正常工作?

https://stackoverflow.com/a/2919565/1185053 中的密钥

最后我从堆栈溢出中得到了解决方案。我从这里找到了解决方案..它工作正常..

解决方案是:-

地穴类:-

public class BCEngine
    {
        private Encoding _encoding;
        private IBlockCipher _blockCipher;
        private PaddedBufferedBlockCipher _cipher;
        private IBlockCipherPadding _padding;
        Pkcs7Padding pkcs = new Pkcs7Padding();
        public BCEngine(IBlockCipher blockCipher, Encoding encoding)
        {
            _blockCipher = blockCipher;
            _encoding = encoding;
        }
        public string Encrypt(string plain, string key)
        {
            byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
            return Convert.ToBase64String(result);
        }
        public string Decrypt(string cipher, string key)
        {
            byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
            return _encoding.GetString(result, 0, result.Length);
        }
        private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
        {
            try
            {                                                                                   
                _cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
                byte[] keyByte = _encoding.GetBytes(key);
                _cipher.Init(forEncrypt, new KeyParameter(keyByte));
                return _cipher.DoFinal(input);
            }
            catch (Org.BouncyCastle.Crypto.CryptoException ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
        public string AESEncryption(string plain, string key)
        {
            return Encrypt(plain, key);
        }
        public string AESDecryption(string cipher, string key)
        {
            return Decrypt(cipher, key);
        }
        public BCEngine()
        {
            _blockCipher = new AesEngine();
            _encoding = Encoding.UTF8;
            pkcs = new Pkcs7Padding();
            _padding = pkcs;
        }
    }

现在我可以从任何地方打电话来加密/解密文本。

例:-

public partial class AesExample : PhoneApplicationPage
    {
        public AesExample()
        {
            InitializeComponent();
           string key = "b09f72a0lkb1lktb";
            string plainText = "Text To Encrypt";
            BCEngine bcEngine = new BCEngine();
            string encryptedString= bcEngine.Encrypt(plainText, key);
            Console.WriteLine("'n'nEncrypted String==> " + encryptedString);
            BCEngine bcEnginenew = new BCEngine();
            string decryptedString = bcEnginenew.Decrypt(encryptedString, key);
            Console.WriteLine("'n'nDecrypted String==> " + decryptedString);
        }
    }