Mã nguồn PHP:
public static byte[] EncryptBuffer(string rsaKeyString, byte[] btSecret)
{
int keySize = 0;
int blockSize = 0;
int lastblockSize = 0;
int counter = 0;
int iterations = 0;
int index = 0;
byte[] btPlaintextToken;
byte[] btEncryptedToken;
byte[] btEncryptedSecret;
RSACryptoServiceProvider rsaSender = new RSACryptoServiceProvider();
rsaSender.FromXmlString(rsaKeyString);
keySize = rsaSender.KeySize / 8;
blockSize = keySize - 11;
if ((btSecret.Length % blockSize) != 0)
{
iterations = btSecret.Length / blockSize + 1;
}
else
{
iterations = btSecret.Length / blockSize;
}
btPlaintextToken = new byte[blockSize]; //See REMARK 2 below.
btEncryptedSecret = new byte[iterations * keySize]; //See REMARK 2 below.
for (index = 0, counter = 0; counter < iterations; counter++, index += blockSize)
{
if (counter == (iterations - 1)) //last block
{
//Size of last block to be encrypted.
lastblockSize = btSecret.Length % blockSize;
//REMARK 1: Caution! The last block of buffer may be of different size!
// Don't copy into a block which is larger than what's actually there in the last block.
// - you won't get exception, but last block will not be decrypted properly.
btPlaintextToken = new byte[lastblockSize];
Array.Copy(btSecret, index, btPlaintextToken, 0, lastblockSize);
}
else
{
Array.Copy(btSecret, index, btPlaintextToken, 0, blockSize);
}
//REMARK 2: Okay, here's the TRICK! (Again, you won't find this on MSDN!)
//(a) Size of blocks going in: blockSize (btPlaintextToken going in)
//(b) Size of blocks going out: keySize (btEncryptedToken coming out)
//If you don't do this carefully, you get cryptographic exception: "Bad Data"
btEncryptedToken = rsaSender.Encrypt(btPlaintextToken, false); //set fOAEP to true only if you have the high encryption pack.
Array.Copy(btEncryptedToken, 0, btEncryptedSecret, counter * keySize, keySize);
}
return btEncryptedSecret;
}
public static byte[] DecryptBuffer(string rsaKeyString, byte[] btEncryptedSecret)
{
int keySize = 0;
int blockSize = 0;
int counter = 0;
int iterations = 0;
int index = 0;
int byteCount = 0;
byte[] btPlaintextToken;
byte[] btEncryptedToken;
byte[] btDecryptedSecret;
RSACryptoServiceProvider rsaReceiver = new RSACryptoServiceProvider();
rsaReceiver.FromXmlString(rsaKeyString);
keySize = rsaReceiver.KeySize / 8;
blockSize = keySize - 11;
//REMARK 3: This should always be true: "btEncryptedSecret.Length % keySize == 0"
if ((btEncryptedSecret.Length % keySize) != 0)
{
//Error condition.
return null;
}
iterations = btEncryptedSecret.Length / keySize;
btEncryptedToken = new byte[keySize]; //See REMARK 4 below.
System.Collections.Queue tokenQueue = new System.Collections.Queue();
for (index = 0, counter = 0; counter < iterations; index += blockSize, counter++)
{
Array.Copy(btEncryptedSecret, counter * keySize, btEncryptedToken, 0, keySize);
//REMARK 4: Okay, here's another trick:
//(a) Encrypted blocks going in: btEncryptedToken / size: keySize
//(b) Decrypted plain text coming out: btPlaintextToken / size: blockSize
btPlaintextToken = rsaReceiver.Decrypt(btEncryptedToken, false);
tokenQueue.Enqueue(btPlaintextToken);
}
byteCount = 0;
foreach (byte[] PlaintextToken in tokenQueue)
{
//REMARK 5: Size of decrypted buffer depends on size of last block.
byteCount += PlaintextToken.Length;
}
counter = 0;
btDecryptedSecret = new byte[byteCount]; //REMARK 5 (see foreach loop above)
foreach (byte[] PlaintextToken in tokenQueue)
{
if (counter == (iterations - 1))
{
Array.Copy(PlaintextToken, 0, btDecryptedSecret, btDecryptedSecret.Length - PlaintextToken.Length, PlaintextToken.Length);
}
else
{
Array.Copy(PlaintextToken, 0, btDecryptedSecret, counter * blockSize, blockSize);
}
counter++;
}
return btDecryptedSecret;
}
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ