Пример #1
0
        public void Remove_RemovesPaddingFromBlock_Removed()
        {
            PKCS7Padding padding = new PKCS7Padding();

            byte[] bytes = { 0, 0, 0, 0, 4, 4, 4, 4 };
            CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 0 }, padding.Remove(bytes));
        }
Пример #2
0
        public void Remove_RemovesPaddingFromBlock_RemovedWholeBlock()
        {
            PKCS7Padding padding = new PKCS7Padding();

            byte[] bytes = { 8, 8, 8, 8, 8, 8, 8, 8 };
            CollectionAssert.AreEqual(new byte[0], padding.Remove(bytes));
        }
Пример #3
0
        public void Add_AddsPaddingToEmptyBlock_Added()
        {
            PKCS7Padding padding = new PKCS7Padding();

            byte[] bytes = new byte[8];
            padding.Add(bytes);
            CollectionAssert.AreEqual(new byte[] { 8, 8, 8, 8, 8, 8, 8, 8 }, bytes);
        }
Пример #4
0
        public void Add_AddsPaddingToBlock_Added()
        {
            PKCS7Padding padding = new PKCS7Padding();

            byte[] bytes = new byte[8];
            padding.Add(bytes, 3);
            CollectionAssert.AreEqual(new byte[] { 0, 0, 0, 0, 4, 4, 4, 4 }, bytes);
        }
Пример #5
0
        public void Add_ProvidesEmptyBlock_Added()
        {
            byte[]       bytes   = new byte[16];
            PKCS7Padding padding = new PKCS7Padding();

            padding.Add(bytes);
            CollectionAssert.AreEqual(new byte[] { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }, bytes);
        }
Пример #6
0
        public void Add_ProvidesFullBlock_NotAdded()
        {
            byte[]       bytes   = new byte[16];
            PKCS7Padding padding = new PKCS7Padding();

            padding.Add(bytes, 16);
            CollectionAssert.AreEqual(new byte[16], bytes);
        }
Пример #7
0
        public void PadTest()
        {
            PKCS7Padding target    = new PKCS7Padding(); // TODO: Initialize to an appropriate value
            int          blockSize = 0;                  // TODO: Initialize to an appropriate value

            byte[] input    = null;                      // TODO: Initialize to an appropriate value
            byte[] expected = null;                      // TODO: Initialize to an appropriate value
            byte[] actual;
            actual = target.Pad(blockSize, input);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Пример #8
0
        public void Add_ProvidesBlock_Added()
        {
            byte[] bytes = new byte[16];
            bytes[0] = 1;
            bytes[1] = 2;
            bytes[2] = 3;
            bytes[3] = 4;
            bytes[4] = 5;
            PKCS7Padding padding = new PKCS7Padding();

            padding.Add(bytes, 4);
            CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4, 5, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11 }, bytes);
        }
Пример #9
0
            /*
             * 对明文加密.
             * @param text 需要加密的明文
             * @return 加密后base64编码的字符串
             */
            private String Encrypt(String random, String plaintext)
            {
                try
                {
                    byte[] randomBytes    = System.Text.Encoding.UTF8.GetBytes(random);    // random.getBytes(CHARSET);
                    byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plaintext); // plaintext.getBytes(CHARSET);
                    byte[] lengthByte     = Utils.Int2Bytes(plainTextBytes.Length);
                    byte[] corpidBytes    = System.Text.Encoding.UTF8.GetBytes(corpId);    // corpId.getBytes(CHARSET);
                                                                                           //MemoryStream byteStream = new MemoryStream();
                    var bytestmp = new List <byte>();
                    bytestmp.AddRange(randomBytes);
                    bytestmp.AddRange(lengthByte);
                    bytestmp.AddRange(plainTextBytes);
                    bytestmp.AddRange(corpidBytes);
                    byte[] padBytes = PKCS7Padding.GetPaddingBytes(bytestmp.Count);
                    bytestmp.AddRange(padBytes);
                    byte[] unencrypted = bytestmp.ToArray();

                    RijndaelManaged rDel = new RijndaelManaged
                    {
                        Mode    = CipherMode.CBC,
                        Padding = PaddingMode.Zeros,
                        Key     = aesKey,
                        IV      = aesKey.ToList().Take(16).ToArray()
                    };
                    ICryptoTransform cTransform  = rDel.CreateEncryptor();
                    byte[]           resultArray = cTransform.TransformFinalBlock(unencrypted, 0, unencrypted.Length);
                    return(Convert.ToBase64String(resultArray, 0, resultArray.Length));


                    //Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
                    //SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
                    //IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
                    //cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
                    //byte[] encrypted = cipher.doFinal(unencrypted);
                    //String result = base64.encodeToString(encrypted);
                    //return result;
                }
                catch (Exception)
                {
                    throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_ENCRYPT_TEXT_ERROR);
                }
            }
Пример #10
0
        public void PKCS7PaddingConstructorTest()
        {
            PKCS7Padding target = new PKCS7Padding();

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
 public void Setup()
 {
     _padder = new PKCS7Padding();
 }
Пример #12
0
 public void SetUp()
 {
     _padding = new PKCS7Padding();
 }
Пример #13
0
            /*
             * 对密文进行解密.
             * @param text 需要解密的密文
             * @return 解密得到的明文
             */
            private String Decrypt(String text)
            {
                byte[] originalArr;
                try
                {
                    byte[]          toEncryptArray = Convert.FromBase64String(text);
                    RijndaelManaged rDel           = new RijndaelManaged
                    {
                        Mode    = CipherMode.CBC,
                        Padding = PaddingMode.Zeros,
                        Key     = aesKey,
                        IV      = aesKey.ToList().Take(16).ToArray()
                    };
                    ICryptoTransform cTransform = rDel.CreateDecryptor();
                    originalArr = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                    //return System.Text.UTF8Encoding.UTF8.GetString(resultArray);

                    //// 设置解密模式为AES的CBC模式
                    //Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
                    //SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
                    //IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
                    //cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
                    //// 使用BASE64对密文进行解码
                    //byte[] encrypted = Base64.decodeBase64(text);
                    //// 解密
                    //originalArr = cipher.doFinal(encrypted);
                }
                catch (Exception)
                {
                    throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_DECRYPT_TEXT_ERROR);
                }

                String plainText;
                String fromCorpid;

                try
                {
                    // 去除补位字符
                    byte[] bytes = PKCS7Padding.RemovePaddingBytes(originalArr);
                    Console.Out.WriteLine("bytes size:" + bytes.Length);

                    // 分离16位随机字符串,网络字节序和corpId
                    byte[] networkOrder = bytes.Skip(16).Take(4).ToArray();// Arrays.copyOfRange(bytes, 16, 20);
                    for (int i = 0; i < 4; i++)
                    {
                        Console.Out.WriteLine("networkOrder size:" + (int)networkOrder[i]);
                    }

                    Console.Out.WriteLine("bytes plainText:" + networkOrder.Length + " " + JsonSerializer.Serialize(networkOrder));
                    int plainTextLegth = Utils.Bytes2int(networkOrder);
                    Console.Out.WriteLine("bytes size:" + plainTextLegth);

                    plainText  = System.Text.UTF8Encoding.UTF8.GetString(bytes.Skip(20).Take(plainTextLegth).ToArray()); // new String(Arrays.copyOfRange(bytes, 20, 20 + plainTextLegth), CHARSET);
                    fromCorpid = System.Text.UTF8Encoding.UTF8.GetString(bytes.Skip(20 + plainTextLegth).ToArray());     //new String(Arrays.copyOfRange(bytes, 20 + plainTextLegth, bytes.length), CHARSET);
                    Console.Out.WriteLine("bytes plainText:" + plainText);
                }
                catch (Exception)
                {
                    throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_DECRYPT_TEXT_LENGTH_ERROR);
                }
                Console.Out.WriteLine(fromCorpid + "=====" + corpId);


                // corpid不相同的情况
                if (!fromCorpid.Equals(corpId))
                {
                    throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_DECRYPT_TEXT_CORPID_ERROR);
                }
                return(plainText);
            }