首页 \ 问答 \ 解密加密文本(Decrypt an encrypted text)

解密加密文本(Decrypt an encrypted text)

我在Windows窗体应用程序中有一个文本框和一个“解密”按钮,我在其中放入一个加密字符串并尝试解密但问题是这样。 首先,我从这个网站上的一个人那里得到了这个名为DataEncryptor类代码:

public class DataEncryptor
{
    TripleDESCryptoServiceProvider symm;

    #region Factory
    public DataEncryptor()
    {
        this.symm = new TripleDESCryptoServiceProvider();
        this.symm.Padding = PaddingMode.PKCS7;
    }
    public DataEncryptor(TripleDESCryptoServiceProvider keys)
    {
        this.symm = keys;
    }

    public DataEncryptor(byte[] key, byte[] iv)
    {
        this.symm = new TripleDESCryptoServiceProvider();
        this.symm.Padding = PaddingMode.PKCS7;
        this.symm.Key = key;
        this.symm.IV = iv;
    }

    #endregion

    #region Properties
    public TripleDESCryptoServiceProvider Algorithm
    {
        get { return symm; }
        set { symm = value; }
    }
    public byte[] Key
    {
        get { return symm.Key; }
        set { symm.Key = value; }
    }
    public byte[] IV
    {
        get { return symm.IV; }
        set { symm.IV = value; }
    }

    #endregion

    #region Crypto

    public byte[] Encrypt(byte[] data) { return Encrypt(data, data.Length); }
    public byte[] Encrypt(byte[] data, int length)
    {
        try
        {
            // Create a MemoryStream.
            var ms = new MemoryStream();

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            var cs = new CryptoStream(ms,
                symm.CreateEncryptor(symm.Key, symm.IV),
                CryptoStreamMode.Write);

            // Write the byte array to the crypto stream and flush it.
            cs.Write(data, 0, length);
            cs.FlushFinalBlock();

            // Get an array of bytes from the 
            // MemoryStream that holds the 
            // encrypted data.
            byte[] ret = ms.ToArray();

            // Close the streams.
            cs.Close();
            ms.Close();

            // Return the encrypted buffer.
            return ret;
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
        }
        return null;
    }

    public string EncryptString(string text)
    {
        return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(text)));
    }

    public byte[] Decrypt(byte[] data) { return Decrypt(data, data.Length); }
    public byte[] Decrypt(byte[] data, int length)
    {
        try
        {
            // Create a new MemoryStream using the passed 
            // array of encrypted data.
            MemoryStream ms = new MemoryStream(data);

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            CryptoStream cs = new CryptoStream(ms,
                symm.CreateDecryptor(symm.Key, symm.IV),
                CryptoStreamMode.Read);

            // Create buffer to hold the decrypted data.
            byte[] result = new byte[length];

            // Read the decrypted data out of the crypto stream
            // and place it into the temporary buffer.
            cs.Read(result, 0, result.Length);
            return result;
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
        }
        return null;
    }

    public string DecryptString(string data)
    {
        return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data))).TrimEnd('\0');
    }

    #endregion

}

他给出了它的用法:

string message="A very secret message here.";
DataEncryptor keys=new DataEncryptor();
string encr=keys.EncryptString(message);

// later
string actual=keys.DecryptString(encr);

我复制了他的代码,并在加密和解密工作:

//my code
private void proceedED(string data)
{
    DataEncryptor key = new DataEncryptor();
    string encr = key.EncryptString(data);
    string actual = key.DecryptString(encr);
    encryptedLabel.Text = encr;
    decryptedLabel.Text = actual;     
}

然后我创建了一个这样的方法:

private void proceedDecrypt(string data) 
{
    DataEncryptor key = new DataEncryptor();
    string decr = key.DecryptString(data);
    decryptedData.Text = decr;
}

问题是,当我提交时它崩溃了,我不知道为什么。 我认为它应该是一个真正的加密字符串,因为它只是一个普通的字符串。 我该如何解决?


I got a textbox and a 'decrypt' button in my Windows Form Application where I put an encrypted string in there and try to decrypt it but the problem is this. First, I got this class code called DataEncryptor from a guy on this website:

public class DataEncryptor
{
    TripleDESCryptoServiceProvider symm;

    #region Factory
    public DataEncryptor()
    {
        this.symm = new TripleDESCryptoServiceProvider();
        this.symm.Padding = PaddingMode.PKCS7;
    }
    public DataEncryptor(TripleDESCryptoServiceProvider keys)
    {
        this.symm = keys;
    }

    public DataEncryptor(byte[] key, byte[] iv)
    {
        this.symm = new TripleDESCryptoServiceProvider();
        this.symm.Padding = PaddingMode.PKCS7;
        this.symm.Key = key;
        this.symm.IV = iv;
    }

    #endregion

    #region Properties
    public TripleDESCryptoServiceProvider Algorithm
    {
        get { return symm; }
        set { symm = value; }
    }
    public byte[] Key
    {
        get { return symm.Key; }
        set { symm.Key = value; }
    }
    public byte[] IV
    {
        get { return symm.IV; }
        set { symm.IV = value; }
    }

    #endregion

    #region Crypto

    public byte[] Encrypt(byte[] data) { return Encrypt(data, data.Length); }
    public byte[] Encrypt(byte[] data, int length)
    {
        try
        {
            // Create a MemoryStream.
            var ms = new MemoryStream();

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            var cs = new CryptoStream(ms,
                symm.CreateEncryptor(symm.Key, symm.IV),
                CryptoStreamMode.Write);

            // Write the byte array to the crypto stream and flush it.
            cs.Write(data, 0, length);
            cs.FlushFinalBlock();

            // Get an array of bytes from the 
            // MemoryStream that holds the 
            // encrypted data.
            byte[] ret = ms.ToArray();

            // Close the streams.
            cs.Close();
            ms.Close();

            // Return the encrypted buffer.
            return ret;
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
        }
        return null;
    }

    public string EncryptString(string text)
    {
        return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(text)));
    }

    public byte[] Decrypt(byte[] data) { return Decrypt(data, data.Length); }
    public byte[] Decrypt(byte[] data, int length)
    {
        try
        {
            // Create a new MemoryStream using the passed 
            // array of encrypted data.
            MemoryStream ms = new MemoryStream(data);

            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            CryptoStream cs = new CryptoStream(ms,
                symm.CreateDecryptor(symm.Key, symm.IV),
                CryptoStreamMode.Read);

            // Create buffer to hold the decrypted data.
            byte[] result = new byte[length];

            // Read the decrypted data out of the crypto stream
            // and place it into the temporary buffer.
            cs.Read(result, 0, result.Length);
            return result;
        }
        catch (CryptographicException ex)
        {
            Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
        }
        return null;
    }

    public string DecryptString(string data)
    {
        return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data))).TrimEnd('\0');
    }

    #endregion

}

And he gave the usage of it:

string message="A very secret message here.";
DataEncryptor keys=new DataEncryptor();
string encr=keys.EncryptString(message);

// later
string actual=keys.DecryptString(encr);

I copied his code and works at encrypting and decrypting:

//my code
private void proceedED(string data)
{
    DataEncryptor key = new DataEncryptor();
    string encr = key.EncryptString(data);
    string actual = key.DecryptString(encr);
    encryptedLabel.Text = encr;
    decryptedLabel.Text = actual;     
}

Then I created a method like this:

private void proceedDecrypt(string data) 
{
    DataEncryptor key = new DataEncryptor();
    string decr = key.DecryptString(data);
    decryptedData.Text = decr;
}

The problem is that it crashes when I submit and I don't know why. I think it should be a true encrypted string because it's just a normal string. How do I fix this?


原文:https://stackoverflow.com/questions/30795328
更新时间:2022-09-13 09:09

最满意答案

尝试这样的事情:

$customer = \Stripe\Customer::retrieve($customerID);
$cardID = $customer->default_source;

if(isset($cardID)){
    echo $cardID;
} else {
    echo "No card";
    //Code for entering card info..
}

在那里你将获得卡ID,然后你可以简单地检查你是否得到了什么。 如果客户有一张卡,那么你会获得卡ID,如果客户没有卡,你将无法获得任何卡。


Try something like this:

$customer = \Stripe\Customer::retrieve($customerID);
$cardID = $customer->default_source;

if(isset($cardID)){
    echo $cardID;
} else {
    echo "No card";
    //Code for entering card info..
}

There you will get the card id, and then you can simply check if you get something. If customer have a card then you get card id, if costumer does not have a card you won't get anything.

相关问答

更多
  • 因此,这里的想法是使用Card对象或Token对象上的fingerprint ,而不是id本身,因为如果多次添加相同的卡,这些fingerprint将会不同。 当你得到一个新的卡片令牌时,你可以通过Retrieve Token API检索它,并在card哈希中查找fingerprint 。 您将保留数据库中与特定客户和/或卡关联的已知指纹列表,以便您可以检测到重复的卡片。 注意:确保您使用密钥来获取这些信息。 否则,如果您使用可发布密钥,则可能无法获得指纹值。 So the idea here would ...
  • 你不能。 该网站不提供有关该卡的所有信息,因为这是不必要的,并构成诈骗威胁。 在API文档中,我们将找到共享信息的示例。 "source": { "id": "card_1BLoXK2eZvKYlo2CUZoDPXh1", "object": "card", "address_city": null, "address_country": null, "address_line1": null, "address_line1_check": null, "address_line2": null, "add ...
  • 尝试这样的事情: $customer = \Stripe\Customer::retrieve($customerID); $cardID = $customer->default_source; if(isset($cardID)){ echo $cardID; } else { echo "No card"; //Code for entering card info.. } 在那里你将获得卡ID,然后你可以简单地检查你是否得到了什么。 如果客户有一张卡,那么你会获得卡ID, ...
  • 客户可以拥有多张卡,因此$customer->sources->data是一个数组(因为您应该可以从该属性值的方括号中看出)。 因此,您需要为其编制索引。 print_r($customer->sources->data[0]->id); 如果客户已经保存了多张信用卡,您应该循环使用它: foreach ($customer->sources->data as $card) { print_r($card->id); } A customer can have multiple cards, s ...
  • 您无需保存access_token和refresh_token ,只stripe_user_id包含连接的帐户ID的stripe_user_id与Connect配合使用。 您可以针对所有请求使用您平台的秘密API密钥,并将共享客户文档中显示的已连接帐户ID传递到条带帐户标头。 更一般地说,这个头文件可以添加到几乎每个API请求中,以使它指向一个连接的帐户: https://stripe.com/docs/connect/authentication#authentication-via-the-stripe ...
  • 因此Stripe Checkout用于标记卡片,而不用管理已经标记并连接到客户的卡片。 您需要构建某种用户界面来执行此操作。 但是,您可以通过使用下面详细介绍的创建充电API端点来为这些卡充电: https://stripe.com/docs/api#create_charge 只需分别提供与客户ID和卡ID相同的customer和source参数。 希望有所帮助! So Stripe Checkout is for tokenizing cards, not managing cards that hav ...
  • 几天前我遇到了这个问题而且我已经设法使用这样的json_encode() API来解决它(需要将此导出为我身边的AJAX返回)。 $cards = \Stripe\Customer::retrieve($_SESSION['stripe_customer_id'])->sources->all(array("object" => "card")); $list = json_encode($cards->data); echo($list); 希望这可以帮助。 顺便说一句:您的响应看起来与var_dump ...
  • 您可能还想查看此SO答案如何在Stripe(Rails)中创建费用和客户,以获取有关在Rails应用程序中使用Stripe的更多详细信息。 对于Ruby文档,您可以在Stripe Ruby API上找到很好的示例。 在Stripe术语中,卡被称为客户的source 。 您可以从token创建source ,但是一旦创建了source ,您就可以处理Customer对象上的source和default_source元素,并从客户的source检索card对象。 另请注意,除了创建source (或一次性费用) ...
  • 由于您正在创建客户对象,因此根据您要存储的内容的描述,它似乎并不重要。 Stripe不会对物理商品进行任何实现,因此他们在客户对象上提供的存储主要是为了您的利益。 因此,当您访问客户对象(通过类似cus_8Dmu7vi6wah58z的ID)时,它应返回所有发货信息和元数据。 发货散列中有一个专用名称字段 ,但它不会从姓氏中抽出名字。 如果这是您真正想要的东西,那么将其存储在元数据字段中可能会更容易。 您还可以注意,将“送货”信息存储在送货哈希中可能会有所帮助,并将“开票”信息存储在元数据哈希中。 Since ...
  • 使用Stripe添加卡有两种方法。 1)使用卡令牌。 如果你有一个令牌,就像你一样,你不需要任何其他东西,所以你可以做到 customer.cards.create({ :card => current_user.stripe_card_token }) 2)你也可以这样做: customer.cards.create({ :card => { :number => params[:user][:scardnumber], :exp_month => params[:user] ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)