我收到了这段代码:
public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
{
if (strIn.Length < 1)
return strIn;
// Convert the input string to a byte array
byte[] btToEncrypt = System.Text.Encoding.Unicode.GetBytes(strIn);
RijndaelManaged cryptoRijndael = new RijndaelManaged();
cryptoRijndael.Mode =
CipherMode.ECB;//Doesn't require Initialization Vector
cryptoRijndael.Padding =
PaddingMode.PKCS7;
// Create a key (No IV needed because we are using ECB mode)
ASCIIEncoding textConverter = new ASCIIEncoding();
// Get an encryptor
ICryptoTransform ictEncryptor = cryptoRijndael.CreateEncryptor(btKey, null);
// Encrypt the data...
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, ictEncryptor, CryptoStreamMode.Write);
// Write all data to the crypto stream to encrypt it
csEncrypt.Write(btToEncrypt, 0, btToEncrypt.Length);
csEncrypt.Close();
//flush, close, dispose
// Get the encrypted array of bytes
byte[] btEncrypted = msEncrypt.ToArray();
// Convert the resulting encrypted byte array to string for return
return (Convert.ToBase64String(btEncrypted));
}
private static List<int> GetRandomSubstitutionArray(string number)
{
// Pad number as needed to achieve longer key length and seed more randomly.
// NOTE I didn't want to make the code here available and it would take too longer to clean, so I'll tell you what I did. I basically took every number seed that was passed in and prefixed it and postfixed it with some values to make it 16 characters long and to get a more unique result. For example:
// if (number.Length = 15)
// number = "Y" + number;
// if (number.Length = 14)
// number = "7" + number + "z";
// etc - hey I already said this is a hack ;)
// We pass in the current number as the password to an AES encryption of each of the
// digits 0 - 9. This returns us a set of values that we can then sort and get a
// random order for the digits based on the current state of the number.
Dictionary<string, int> prefixCipherResults = new Dictionary<string, int>();
for (int ndx = 0; ndx < 10; ndx++)
prefixCipherResults.Add(DoPrefixCipherEncrypt(ndx.ToString(), Encoding.UTF8.GetBytes(number)), ndx);
// Order the results and loop through to build your int array.
List<int> group = new List<int>();
foreach (string key in prefixCipherResults.Keys.OrderBy(k => k))
group.Add(prefixCipherResults[key]);
return group;
}
来自此链接Encrypt a number to another number of the same length
我需要将“DoPrefixCypherEncrypt”转换/调整为AesManaged而不是RijdaelManaged。
谢谢你们
更新 谢谢你的所有答案:
我最终找到了使用WP 8.1中可用类的另一种方法。
而不是:
public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
{
if (strIn.Length < 1)
return strIn;
// Convert the input string to a byte array
byte[] btToEncrypt = System.Text.Encoding.Unicode.GetBytes(strIn);
AesManaged cryptoRijndael = new AesManaged();
cryptoRijndael.Mode = CipherMode.ECB; cryptoRijndael.Padding = PaddingMode.PKCS7; //Mode Doesn't require Initialization Vector
// Create a key (No IV needed because we are using ECB mode)
ASCIIEncoding textConverter = new ASCIIEncoding();
// Get an encryptor
ICryptoTransform ictEncryptor = cryptoRijndael.CreateEncryptor(btKey, null);
// Encrypt the data...
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, ictEncryptor, CryptoStreamMode.Write);
// Write all data to the crypto stream to encrypt it
csEncrypt.Write(btToEncrypt, 0, btToEncrypt.Length); csEncrypt.Close(); //flush, close, dispose
// Get the encrypted array of bytes
byte[] btEncrypted = msEncrypt.ToArray();
// Convert the resulting encrypted byte array to string for return
return (Convert.ToBase64String(btEncrypted));
}
与非WinRT .NET兼容。
我能够使用:
public static string DoPrefixCipherEncrypt(string strIn, byte[] btKey)
{
if (strIn.Length < 1)
return strIn;
IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(strIn, BinaryStringEncoding.Utf16LE);
IBuffer keyMaterial = CryptographicBuffer.CreateFromByteArray(btKey);
SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
// create symmetric key from derived password key
CryptographicKey symmKey = symProvider.CreateSymmetricKey(keyMaterial);
var buffEncrypted = CryptographicEngine.Encrypt(symmKey, plainBuffer, null);
var strEncrypted = CryptographicBuffer.EncodeToBase64String(buffEncrypted);
return strEncrypted;
}
答案 0 :(得分:1)
只需用AesManaged替换RijndaelManaged的实例化和声明:
AesManaged cryptoRijndael = new AesManaged();
我试过了,它运作得很好。我建议同样重命名一下cryptoRijndael变量名 - 但这不会改变代码功能的任何内容。
MSDN上有一个示例如何使用AesManaged语句:
AES算法本质上是Rijndael对称算法 固定块大小和迭代计数。这个类的功能相同 作为RijndaelManaged类的方式,但限制块为128位和 不允许反馈模式。
所以只需用AesManaged替换RijndaelManaged的引用即可。只要您不在规定的限制范围内使用它就可以了。
严格来说,AES is a subset of Rijndael所以Rijndael Managed应该已经涵盖了你需要的任何内容。
来自Wikipedia:
AES基于两个比利时人开发的Rijndael密码[5] 密码学家,Joan Daemen和Vincent Rijmen,他们提交了一份文件 在AES选择过程中向NIST提出建议。