/** * 密文解密 * @param msgSignature 签名串 * @param timeStamp 时间戳 * @param nonce 随机串 * @param encryptMsg 密文 * @return 解密后的原文 * @throws DingTalkEncryptException */ public String getDecryptMsg(String msgSignature, String timeStamp, String nonce, String encryptMsg) { //校验签名 String signature = getSignature(token, timeStamp, nonce, encryptMsg); LogHelper.AddLog2(51, signature + "/" + msgSignature, null); if (!signature.Equals(msgSignature)) { throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_SIGNATURE_ERROR); } // 解密 String result = decrypt(encryptMsg); return(result); }
/* * 对明文加密. * @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(); rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.Zeros; rDel.Key = aesKey; rDel.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 e) { LogHelper.AddLog2(200, e.ToString(), null); throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_ENCRYPT_TEXT_ERROR); } }
/** * 数字签名 * @param token isv token * @param timestamp 时间戳 * @param nonce 随机串 * @param encrypt 加密文本 * @return * @throws DingTalkEncryptException */ public String getSignature(String token, String timestamp, String nonce, String encrypt) { try { String[] array = new String[] { token, timestamp, nonce, encrypt }; LogHelper.AddLog2(551, string.Join(";", array), null); //Arrays.sort(array); var tmparray = array.ToList(); tmparray.Sort(new JavaStringComper()); array = tmparray.ToArray(); LogHelper.AddLog2(552, string.Join(";", array), null); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { sb.Append(array[i]); } String str = sb.ToString(); LogHelper.AddLog2(444, sb.ToString(), null); //MessageDigest md = MessageDigest.getInstance("SHA-1"); //md.update(str.getBytes()); //byte[] digest = md.digest(); System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create(); System.Text.Encoding encoder = System.Text.Encoding.ASCII; byte[] combined = encoder.GetBytes(str); ////byte 转换 //sbyte[] myByte = new sbyte[] //byte[] mySByte = new byte[myByte.Length]; //for (int i = 0; i < myByte.Length; i++) //{ // if (myByte[i] > 127) // mySByte[i] = (sbyte)(myByte[i] - 256); // else // mySByte[i] = (sbyte)myByte[i]; //} byte[] digest = hash.ComputeHash(combined); LogHelper.AddLog2(553, string.Join(";", digest), null); StringBuilder hexstr = new StringBuilder(); String shaHex = ""; for (int i = 0; i < digest.Length; i++) { shaHex = ((int)digest[i]).ToString("x");// Integer.toHexString(digest[i] & 0xFF); if (shaHex.Length < 2) { hexstr.Append(0); } hexstr.Append(shaHex); } LogHelper.AddLog2(554, hexstr.ToString(), null); return(hexstr.ToString()); } catch (Exception e) { throw new DingTalkEncryptException(DingTalkEncryptException.COMPUTE_SIGNATURE_ERROR); } }