我们想在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#OP: -
OYbW6pI8mgqU5xOcfG8N9wXs2 / gWMc6dcUSEoLXm3L5v9Ih9eN63xO31mXmEDLprIzusXaOS1rNNtBPi5I8FG3IukVgicagrkLul1vfa142z + XDULJXFmg5rxPa6iJzXqeZ6x3wxbfI3T / ZqGwxqbg ==
我们无法获得像java这样的确切加密数据。请在Windows Phone 7中建议或提供密码加密/解密的任何链接。
答案 0 :(得分:0)
使用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 的键
答案 1 :(得分:0)
最后,我从 stackoverflow 获得了解决方案。 I found the solution from here ..工作正常......
解决方案是: -
地穴类: -
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);
}
}