コード例 #1
0
        private trafficMessage DecryptMessageAfterResponse(trafficMessage msgEncrypt)
        {
            trafficMessage msgSource = new trafficMessage();

            msgSource.transCode  = msgEncrypt.transCode;
            msgSource.merId      = msgEncrypt.merId;
            msgSource.extend     = msgEncrypt.extend;
            msgSource.encryptKey = CryptUtils.getStringFromBytes(CryptUtils.RSADecrypt(CryptUtils.getPrivateKeyXmlFromPFX(pfxFilePath, pfxPassword).PrivateKey.ToXmlString(true),
                                                                                       CryptUtils.Base64Decoder(msgEncrypt.encryptKey)), encodeCode);
            log.Write("Decrypted remote AESkey [" + msgSource.encryptKey + "]");
            //log.Write("encryptKey[" + msgSource.encryptKey + "]);

            byte[] sourceByte = CryptUtils.AESDecrypt(CryptUtils.Base64Decoder(msgEncrypt.encryptData),
                                                      msgSource.encryptKey);
            msgSource.encryptData = CryptUtils.getStringFromBytes(sourceByte, encodeCode);
            // log.Write("encryptData[" + msgSource.encryptData + "][" + msgEncrypt.encryptData + "]");

            msgSource.sign =
                CryptUtils.VerifySignWithPublicKey(
                    sourceByte,
                    CryptUtils.getPublicKeyXmlFromCer(cerFilePath),
                    CryptUtils.Base64Decoder(msgEncrypt.sign)
                    ).ToString();

            // log.Write("sign[" + msgSource.sign + "][" + msgEncrypt.sign + "]");
            //需要添加引用 System.Web
            return(msgSource);
        }
コード例 #2
0
        private trafficMessage EncryptMessageBeforePost(trafficMessage msgSource)
        {
            trafficMessage msgEncrypt = new trafficMessage();

            //随机生成16位密钥
            msgSource.encryptKey = CryptUtils.GuidTo16String();
            log.Write("Generated local AESkey [" + msgSource.encryptKey + "]");

            //encryptKey加密,杉德公钥RSA加密
            msgEncrypt.encryptKey = CryptUtils.Base64Encoder(CryptUtils.RSAEncrypt(CryptUtils.getPublicKeyXmlFromCer(cerFilePath).PublicKey.Key.ToXmlString(false),
                                                                                   CryptUtils.getBytesFromString(msgSource.encryptKey, encodeCode)));

            //log.Write("encryptKey[" + msgSource.encryptKey + "][" + msgEncrypt.encryptKey + "]");

            msgEncrypt.transCode = msgSource.transCode;
            msgEncrypt.merId     = msgSource.merId;
            msgEncrypt.extend    = msgSource.extend;

            //encryptData加密,AES加密
            msgEncrypt.encryptData = CryptUtils.Base64Encoder(CryptUtils.AESEncrypt(CryptUtils.getBytesFromString(msgSource.encryptData, encodeCode),
                                                                                    msgSource.encryptKey));

            //log.Write("encryptData[" + msgSource.encryptData + "][" + msgEncrypt.encryptData + "]");

            msgEncrypt.sign = CryptUtils.Base64Encoder(CryptUtils.CreateSignWithPrivateKey(CryptUtils.getBytesFromString(msgSource.encryptData, encodeCode),
                                                                                           CryptUtils.getPrivateKeyXmlFromPFX(pfxFilePath, pfxPassword)));

            //log.Write("sign[" + msgEncrypt.sign + "]");

            return(msgEncrypt);
        }