[英]Cipher encryption/decryption in windows phone 7
我們想在Windows Phone 7中使用密碼加密/解密。 但是,當我們嘗試使用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 / hN5zPwWbbTWCyY4YCY4YCY2Y3Y
警察:-
OYbW6pI8mgqU5xOcfG8N9wXs2 / gWMc6dcUSEoLXm3L5v9Ih9eN63xO31mXmEDLprIzusXaOS1rNNtBPi5I8FG3IukVgicagrkLul1vfa142z + XDULJXFmg5rxPa6iJzXqeZ6x3wxbfI3T / ZqGwxqbg ==
我們無法獲得像java這樣的確切加密數據。 請建議或提供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
也許是Windows Phone 7特有的某種功能導致它無法正常工作?
最后,我從stackoverflow得到了解決方案。 我從這里找到解決方案 ..它工作正常..
解決方案是:-
地穴類:-
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.