mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-06-11 02:53:23 +02:00
BackUp DES
This commit is contained in:
45
NFC/Crypto/DES.cs
Normal file
45
NFC/Crypto/DES.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Modes;
|
||||
using Org.BouncyCastle.Crypto.Paddings;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace NFC.Crypto
|
||||
{
|
||||
public class DES
|
||||
{
|
||||
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEngine();
|
||||
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
|
||||
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
|
||||
KeyParameter keyParam = new KeyParameter(key);
|
||||
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
|
||||
|
||||
// Encrypt
|
||||
cipher.Init(true, keyParamWithIV);
|
||||
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
|
||||
int length = cipher.ProcessBytes(data, outputBytes, 0);
|
||||
cipher.DoFinal(outputBytes, length);
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEngine();
|
||||
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
|
||||
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
|
||||
KeyParameter keyParam = new KeyParameter(key);
|
||||
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
|
||||
|
||||
// Decrypt
|
||||
cipher.Init(false, keyParamWithIV);
|
||||
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
|
||||
int length = cipher.ProcessBytes(data, outputBytes, 0);
|
||||
cipher.DoFinal(outputBytes, length);
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
}
|
||||
}
|
45
NFC/Crypto/TripleDES.cs
Normal file
45
NFC/Crypto/TripleDES.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Modes;
|
||||
using Org.BouncyCastle.Crypto.Paddings;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace NFC.Crypto
|
||||
{
|
||||
public class TripleDES
|
||||
{
|
||||
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEdeEngine();
|
||||
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
|
||||
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
|
||||
KeyParameter keyParam = new KeyParameter(key);
|
||||
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
|
||||
|
||||
// Encrypt
|
||||
cipher.Init(true, keyParamWithIV);
|
||||
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
|
||||
int length = cipher.ProcessBytes(data, outputBytes, 0);
|
||||
cipher.DoFinal(outputBytes, length);
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEdeEngine();
|
||||
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
|
||||
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
|
||||
KeyParameter keyParam = new KeyParameter(key);
|
||||
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
|
||||
|
||||
// Decrypt
|
||||
cipher.Init(false, keyParamWithIV);
|
||||
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
|
||||
int length = cipher.ProcessBytes(data, outputBytes, 0);
|
||||
cipher.DoFinal(outputBytes, length);
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user