我正在帮助一些学生做一些编程工作,并希望知道如何在加密后解密信用卡号码。这是加密属性:
public string Encrypt(string ccNum)
{
ccNum = Convert.ToString((Convert.ToInt64(ccNum) * 13) + 43);
return ccNum;
}
我认为解密属性与加密相反,但它不起作用。这是我的decrypt属性代码:
public string Decrypt(string ccNum) //DOESN'T DECRYPT
{
string decCCNum = Convert.ToString((Convert.ToInt64(ccNum) - 43) / 13);
MessageBox.Show("Customer Name: " + FirstName + " " + LastName + "\n" +
"Encypted Card #: " + Encrypt(ccNum) + "\n" +
"Decrypted Card #: " + decCCNum , "Customer Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return ccNum;
}
非常感谢任何帮助。
答案 0 :(得分:2)
这是一个简单的数学问题,因为你已经:
x =(y * z)+ w,求解y:y =(x - w)/ z
我建议您编写单元测试以断言您的代码工作(就像其他人建议的那样,您将返回原始加密号码):
public static class CreditCardEncryptor
{
public static string Encrypt(string ccNum)
{
return Convert.ToString(Convert.ToInt64(ccNum) * 13 + 43);
}
public static string Decrypt(string encryptedCcNum)
{
return Convert.ToString((Convert.ToInt64(encryptedCcNum) - 43) / 13);
}
}
[TestFixture]
public class CreditCardEncryptorTests
{
[TestCase("5105105105105100")]
[TestCase("4012888888881881")]
[TestCase("4222222222222")]
[TestCase("4111111111111111")]
public void WhenIDecryptItShouldReturnCcNumber(string ccNum)
{
Assert.That(ccNum, Is.EqualTo(CreditCardEncryptor.Decrypt(CreditCardEncryptor.Encrypt(ccNum))));
}
}
答案 1 :(得分:0)
要解密,参数应该是加密的数字,您的方法应该是这样的:
public string Decrypt(string encryptedNumber)
{
string decCCNum = Convert.ToString((Convert.ToInt64(encryptedNumber) - 43) / 13);
MessageBox.Show("Customer Name: " + FirstName + " " + LastName + "\n" +
"Encypted Card #: " + encryptedNumber + "\n" +
"Decrypted Card #: " + decCCNum , "Customer Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return decCCNum;
}