Cipher.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Org.BouncyCastle.Crypto;
  2. using Org.BouncyCastle.Crypto.Parameters;
  3. using Org.BouncyCastle.Math;
  4. using Org.BouncyCastle.Math.EC;
  5. namespace Admin.NET.Core;
  6. public class Cipher
  7. {
  8. private int ct;
  9. private ECPoint p2;
  10. private SM3Digest sm3keybase;
  11. private SM3Digest sm3c3;
  12. private readonly byte[] key;
  13. private byte keyOff;
  14. public Cipher()
  15. {
  16. ct = 1;
  17. key = new byte[32];
  18. keyOff = 0;
  19. }
  20. public static byte[] ByteConvert32Bytes(BigInteger n)
  21. {
  22. if (n == null)
  23. return null;
  24. byte[] tmpd;
  25. if (n.ToByteArray().Length == 33)
  26. {
  27. tmpd = new byte[32];
  28. Array.Copy(n.ToByteArray(), 1, tmpd, 0, 32);
  29. }
  30. else if (n.ToByteArray().Length == 32)
  31. {
  32. tmpd = n.ToByteArray();
  33. }
  34. else
  35. {
  36. tmpd = new byte[32];
  37. for (int i = 0; i < 32 - n.ToByteArray().Length; i++)
  38. {
  39. tmpd[i] = 0;
  40. }
  41. Array.Copy(n.ToByteArray(), 0, tmpd, 32 - n.ToByteArray().Length, n.ToByteArray().Length);
  42. }
  43. return tmpd;
  44. }
  45. private void Reset()
  46. {
  47. sm3keybase = new SM3Digest();
  48. sm3c3 = new SM3Digest();
  49. byte[] p = ByteConvert32Bytes(p2.Normalize().XCoord.ToBigInteger());
  50. sm3keybase.BlockUpdate(p, 0, p.Length);
  51. sm3c3.BlockUpdate(p, 0, p.Length);
  52. p = ByteConvert32Bytes(p2.Normalize().YCoord.ToBigInteger());
  53. sm3keybase.BlockUpdate(p, 0, p.Length);
  54. ct = 1;
  55. NextKey();
  56. }
  57. private void NextKey()
  58. {
  59. var sm3keycur = new SM3Digest(this.sm3keybase);
  60. sm3keycur.Update((byte)(ct >> 24 & 0xff));
  61. sm3keycur.Update((byte)(ct >> 16 & 0xff));
  62. sm3keycur.Update((byte)(ct >> 8 & 0xff));
  63. sm3keycur.Update((byte)(ct & 0xff));
  64. sm3keycur.DoFinal(key, 0);
  65. keyOff = 0;
  66. ct++;
  67. }
  68. public ECPoint Init_enc(SM2 sm2, ECPoint userKey)
  69. {
  70. AsymmetricCipherKeyPair key = sm2.ecc_key_pair_generator.GenerateKeyPair();
  71. ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)key.Private;
  72. ECPublicKeyParameters ecpub = (ECPublicKeyParameters)key.Public;
  73. BigInteger k = ecpriv.D;
  74. ECPoint c1 = ecpub.Q;
  75. p2 = userKey.Multiply(k);
  76. Reset();
  77. return c1;
  78. }
  79. public void Encrypt(byte[] data)
  80. {
  81. sm3c3.BlockUpdate(data, 0, data.Length);
  82. for (int i = 0; i < data.Length; i++)
  83. {
  84. if (keyOff == key.Length)
  85. {
  86. NextKey();
  87. }
  88. data[i] ^= key[keyOff++];
  89. }
  90. }
  91. public void Init_dec(BigInteger userD, ECPoint c1)
  92. {
  93. p2 = c1.Multiply(userD);
  94. Reset();
  95. }
  96. public void Decrypt(byte[] data)
  97. {
  98. for (int i = 0; i < data.Length; i++)
  99. {
  100. if (keyOff == key.Length)
  101. {
  102. NextKey();
  103. }
  104. data[i] ^= key[keyOff++];
  105. }
  106. sm3c3.BlockUpdate(data, 0, data.Length);
  107. }
  108. public void Dofinal(byte[] c3)
  109. {
  110. byte[] p = ByteConvert32Bytes(p2.Normalize().YCoord.ToBigInteger());
  111. sm3c3.BlockUpdate(p, 0, p.Length);
  112. sm3c3.DoFinal(c3, 0);
  113. Reset();
  114. }
  115. }