[代码] [C/C++]代码
// Needs #include
CkString strOut;
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
bool success;
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Generate a 1024-bit RSA key pair.
rsa.GenerateKey(1024);
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
// If some other non-Chilkat application or web service is going to be verifying
// the signature, it is important to match the byte-ordering.
// The LittleEndian property may be set to true
// for little-endian byte ordering,
// or false for big-endian byte ordering.
// Microsoft apps typically use little-endian, while
// OpenSSL and other services (such as Amazon CloudFront)
// use big-endian.
rsa.put_LittleEndian(false);
const char * strData;
strData = "This is the string to be signed.";
// Sign the string using the md5 hash algorithm.
// Other valid choices are "md2", "sha256", "sha384",
// "sha512", and "sha-1".
const char * hexSig;
hexSig = rsa.signStringENC(strData,"md5");
strOut.append(hexSig);
strOut.append("\r\n");
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
// Now verify the signature:
success = rsa.VerifyStringENC(strData,"md5",hexSig);
if (success == true) {
strOut.append("Signature verified!\r\n");
}
else {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
}
// Try it with an invalid signature:
success = rsa.VerifyStringENC(strData,"md5","not a valid sig");
if (success == true) {
strOut.append("Signature verified!\r\n");
}
else {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
}
// Try it with invalid data:
success = rsa.VerifyStringENC("Not the original data","md5",hexSig);
if (success == true) {
strOut.append("Signature verified!\r\n");
}
else {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
}
// Try it with the wrong hash algorithm:
success = rsa.VerifyStringENC(strData,"sha-1",hexSig);
if (success == true) {
strOut.append("Signature verified!\r\n");
}
else {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
}
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
// Needs #include
CkString strOut;
CkRsa rsa;
bool success;
success = rsa.UnlockComponent("Anything for 30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Generate a 1024-bit key. Chilkat RSA supports
// key sizes ranging from 512 bits to 4096 bits.
success = rsa.GenerateKey(1024);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Keys are exported in XML format:
const char * publicKeyXml;
publicKeyXml = rsa.exportPublicKey();
strOut.append(publicKeyXml);
strOut.append("\r\n");
const char * privateKeyXml;
privateKeyXml = rsa.exportPrivateKey();
strOut.append(privateKeyXml);
strOut.append("\r\n");
// Save the private key in PEM format:
CkPrivateKey privKey;
privKey.LoadXml(privateKeyXml);
privKey.SaveRsaPemFile("privateKey.pem");
// Save the public key in PEM format:
CkPublicKey pubKey;
pubKey.LoadXml(publicKeyXml);
pubKey.SaveOpenSslPemFile("publicKey.pem");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
CkString strOut;
CkRsa rsa;
bool success;
success = rsa.UnlockComponent("Anything for 30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// RSA public key in XML format:
const char * publicKey;
publicKey = "of3im3mRgd2NLXIGoK6uYLg6jj0Ug2b42rnqa5Tbwz2ieFqMJqt+++x2oqLYGz49nt+7/785g3XYWqoka4u9c9zul6YubIjnBM72dQy7rEkEfbUxgjcxqXyjZFx+FpaxFUecLu688XEu+9UA42VKiCgcl+E7TrqnfeeYpNXc=AQAB";
rsa.ImportPublicKey(publicKey);
// To use Optimal Asymmetric Encryption Padding (OAEP) padding,
// simply set the OaepPadding property:
rsa.put_OaepPadding(true);
// Encrypt a string and return the encrypted data base64-encoded:
rsa.put_EncodingMode("base64");
const char * plainText;
plainText = "RSA Encryption should be easy.";
bool usePrivateKey;
usePrivateKey = false;
const char * encryptedStr;
encryptedStr = rsa.encryptStringENC(plainText,usePrivateKey);
strOut.append(encryptedStr);
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
CkString strOut;
CkPrivateKey pkey;
// Load the private key from an RSA PEM file:
pkey.LoadPemFile("pvkey2_rsa.pem");
bool success;
const char * pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey.getXml();
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
// If some other non-Chilkat software is going to verify
// the signature, it is important to match the byte-ordering.
// The LittleEndian property may be set to true
// for little-endian byte ordering,
// or false for big-endian byte ordering.
// Microsoft apps typically use little-endian, while
// OpenSSL and other services (such as Amazon CloudFront)
// use big-endian.
rsa.put_LittleEndian(false);
const char * strData;
strData = "This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "md2", "md5", "sha256",
// "sha384", and "sha512".
const char * hexSig;
hexSig = rsa.signStringENC(strData,"sha-1");
strOut.append(hexSig);
strOut.append("\r\n");
strOut.append("Success!\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [PHP]代码
PHP中获取文件扩展名的N种方法
从网上收罗的,基本上就以下这几种方式:
第1种方法:
function get_extension($file)
{
substr(strrchr($file, '.'), 1);
}
第2种方法:
function get_extension($file)
{
return substr($file, strrpos($file, '.')+1);
}
第3种方法:
function get_extension($file)
{
return end(explode('.', $file));
}
第4种方法:
function get_extension($file)
{
$info = pathinfo($file);
return $info['extension'];
}
第5种方法:
function get_extension($file)
{
return pathinfo($file, PATHINFO_EXTENSION);
}
以上几种方式粗看了一下,好像都行,特别是1、2种方法,在我不知道pathinfo有第二个参数之前也一直在用。但是仔细考虑一下,前四种方法都有各种各样的毛病。要想完全正确获取文件的扩展名,必须要能处理以下三种特殊情况。
没有文件扩展名
路径中包含了字符.,如/home/test.d/test.txt
路径中包含了字符.,但文件没有扩展名。如/home/test.d/test
很明显:1、2不能处理第三种情况,3不能正确处理第一三种情况。4可以正确处理,但是在不存在扩展名时,会发出一个警告。只有第5种方法才是最正确的方法。顺便看一下pathinfo方法。官网上介绍如下:
$file_path = pathinfo('/www/htdocs/your_image.jpg');
echo "$file_path ['dirname']\n";
echo "$file_path ['basename']\n";
echo "$file_path ['extension']\n";
echo "$file_path ['filename']\n"; // only in PHP 5.2+
它会返回一个数组,包含最多四个元素,但是并不会一直有四个,比如在没有扩展名的情况下,就不会有extension元素存在,所以第4种方法才会发现警告。但是phpinfo还支持第二个参数。可以传递一个常量,指定返回某一部分的数据:
PATHINFO_DIRNAME - 目录
PATHINFO_BASENAME - 文件名(含扩展名)
PATHINFO_EXTENSION - 扩展名
PATHINFO_FILENAME - 文件名(不含扩展名,PHP>5.2)
这四个常量的值分别是1、2、4、8,刚开始我还以为可以通过或运算指定多个:
pathinfo($file, PATHINFO_EXTENSION | PATHINFO_FILENAME);
后来发现这样不行,这只会返回几个进行或运算常量中最小的那个。也就是四个标志位中最小位为1的常量。
[代码] [C/C++]代码
// Needs #include
// Needs #include
// Needs #include
// Needs #include
CkString strOut;
bool success;
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
CkCertStore certStore;
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
success = certStore.LoadPfxFile("chilkat.pfx","test");
if (success != true) {
strOut.append(certStore.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
CkCert *cert = 0;
cert = certStore.FindCertBySubject("Chilkat Software, Inc.");
if (cert == 0 ) {
strOut.append(certStore.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
CkPrivateKey *pkey = 0;
pkey = cert->ExportPrivateKey();
if (pkey == 0 ) {
strOut.append(cert->lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
const char * pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey->getXml();
delete pkey;
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
// If some other non-Chilkat application or web service is going to be verifying
// the signature, it is important to match the byte-ordering.
// The LittleEndian property may be set to true
// for little-endian byte ordering,
// or false for big-endian byte ordering.
// Microsoft apps typically use little-endian, while
// OpenSSL and other services (such as Amazon CloudFront)
// use big-endian.
rsa.put_LittleEndian(false);
const char * strData;
strData = "This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "md2" and "md5".
const char * hexSig;
hexSig = rsa.signStringENC(strData,"sha-1");
strOut.append(hexSig);
strOut.append("\r\n");
strOut.append("Success!\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
CkString strOut;
CkRsa rsa;
bool success;
success = rsa.UnlockComponent("Anything for 30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
www.wangkai.name 小型臭氧发生器 侳侱侲
// Load a public/private key pair from a .snk key file.
const char * xmlKey;
xmlKey = rsa.snkToXml("chilkat2.snk");
strOut.append(xmlKey);
strOut.append("\r\n");
// The xmlKey contains both public and private keys.
// Import either the public or private for encrypting or
// decrypting.
rsa.ImportPrivateKey(xmlKey);
bool bUsePrivateKey;
bUsePrivateKey = true;
const char * encryptedText;
encryptedText = rsa.encryptStringENC("Hello World!",bUsePrivateKey);
strOut.append(encryptedText);
strOut.append("\r\n");
// Now decrypt with the public key:
rsa.ImportPublicKey(xmlKey);
bUsePrivateKey = false;
const char * decryptedText;
decryptedText = rsa.decryptStringENC(encryptedText,bUsePrivateKey);
strOut.append(decryptedText);
strOut.append("\r\n");
// Note: We could have just as well encrypted using the public key
// and decrypted using the private key.
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
// Needs #include
// Needs #include
CkString strOut;
CkCert cert;
bool success;
// Load a digital certificate.
success = cert.LoadFromFile("digitalCert.cer");
if (success != true) {
strOut.append(cert.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
CkPublicKey *pubKey = 0;
// A .cer file does not contain the private key. It should contain
// the public key...
pubKey = cert.ExportPublicKey();
// Let's have a look at it (in XML format).
strOut.append("Public Key from Certificate:");
strOut.append("\r\n");
strOut.append(pubKey->GetXml());
strOut.append("\r\n");
// An RSA public key consists of a modulus and exponent.
// An RSA private key includes both the modulus and exponent,
// as well as other "big" numbers: P, Q, D, etc.
// Let's load an RSA private key from a DER-encoded file:
CkPrivateKey privKey;
success = privKey.LoadRsaDerFile("PrivateKey.key");
if (success != true) {
strOut.append(privKey.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode(wangkai.name));
return;
}
// If this private key is the matching half to the public key from
// the certificate, then the modulus and exponent should
// be identical. (Thus, a "private key" really contains both the public part as well as the private parts...).
strOut.append("Private Key from DER:");
strOut.append("\r\n");
strOut.append(privKey.GetXml());
strOut.append("\r\n");
CkRsa rsa;
success = rsa.UnlockComponent("Anything for 30-day trial.");
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// You now realize that the private key really contains both
// private and public parts. Therefore, we may import the private
// key and the choose to use either the public or private in our
// encrypt/decrypt operations:
const char * privKeyXml;
privKeyXml = privKey.getXml();
success = rsa.ImportPrivateKey(privKeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
// Needs #include
CkString strOut;
bool success;
CkPrivateKey pkey;
// Load the private key from an RSA PEM file:
success = pkey.LoadPemFile("private.pem");
if (success != true) {
strOut.append(pkey.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
const char * pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey.getXml();
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
const char * strData;
strData = "secret";
// Create an OpenSSL style signature:
const char * hexSig;
hexSig = rsa.openSslSignStringENC(strData);
strOut.append(hexSig);
strOut.append("\r\n");
// Recover the data using the corresponding public key:
CkPublicKey pubkey;
// Load the public key from a PEM file:
success = pubkey.LoadOpenSslPemFile("public.pem");
if (success != true) {
strOut.append(pubkey.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
const char * pubkeyXml;
// Get the public key in XML format:
pubkeyXml = pubkey.getXml();
CkRsa rsa2;
// Import the public key into the RSA component:
success = rsa2.ImportPublicKey(pubkeyXml);
if (success != true) {
strOut.append(rsa2.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
rsa2.put_EncodingMode("hex");
// Recover the original data.
const char * originalData;
originalData = rsa2.openSslVerifyStringENC(hexSig);
strOut.append(originalData);
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
CkString strOut;
CkPrivateKey pkey;
// Load the private key from an RSA PEM file:
pkey.LoadPkcs8EncryptedFile("privateKey.key","myPassword");
bool success;
const char * pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey.getXml();
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append("RSA component unlock failed\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
rsa.put_EncodingMode("hex");
// If some other non-Chilkat application or web service is going to be verifying
// the signature, it is important to match the byte-ordering.
// The LittleEndian property may be set to true
// for little-endian byte ordering,
// or false for big-endian byte ordering.
// Microsoft apps typically use little-endian, while
// OpenSSL and other services (such as Amazon CloudFront)
// use big-endian.
rsa.put_LittleEndian(false);
const char * strData;
strData = "This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "md2", "sha256", "sha384", "sha512", and "md5".
const char * hexSig;
hexSig = rsa.signStringENC(strData,"sha-1");
strOut.append(hexSig);
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
CkString strOut;
CkRsa rsa;
bool success;
success = rsa.UnlockComponent("Anything for 30-day trial");
if (success != true) {
// The LastErrorText provides detailed information about
// any failure. It is standard to all Chilkat components.
// This is good information to send to support@chilkatsoft
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
success = rsa.GenerateKey(1024);
if (success != true) {
// The LastErrorText provides detailed information about
// any failure. It is standard to all Chilkat components.
// This is good information to send to support@chilkatsoft
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
CkString strOut;
CkRsa rsa;
bool success;
success = rsa.UnlockComponent("Anything for 30-day trial");
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
CkPrivateKey key;
// Load an RSA private key from a PKCS8 file:
success = key.LoadPkcs8File("rsaPrivateKey.p8");
if (success != true) {
strOut.append(key.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Get the key as XML:
const char * keyXml;
keyXml = key.getXml();
// We'll encrypt with the public key and decrypt with the private
// key. (It's also possible to do the reverse.)
// Note: An RSA private key is composed of different parts internally: modulus, exponent, P, Q, etc.
// An RSA public-key is a sub-set of the private key.
// Therefore, when you have a private key, you really have
// both public and private keys.
success = rsa.ImportPublicKey(keyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Encrypt a VISA credit card number:
// 1234-5678-0000-9999
const char * ccNumber;
ccNumber = "1234567800009999";
bool usePrivateKey;
usePrivateKey = false;
rsa.put_EncodingMode("base64");
const char * encryptedStr;
encryptedStr = rsa.encryptStringENC(ccNumber,usePrivateKey);
strOut.append("Encrypted:");
strOut.append("\r\n");
strOut.append(encryptedStr);
strOut.append("\r\n");
// Now decrypt:
CkRsa rsaDecryptor;
rsaDecryptor.put_EncodingMode("base64");
rsaDecryptor.ImportPrivateKey(keyXml);
usePrivateKey = true;
const char * decryptedStr;
decryptedStr = rsaDecryptor.decryptStringENC(encryptedStr,usePrivateKey);
strOut.append("Decrypted:");
strOut.append("\r\n");
strOut.append(decryptedStr);
strOut.append("\r\n");
// Important: RSA encryption should only be used to encrypt small amounts of data.
// It is typically used for encrypting symmetric encryption
// keys such that a symmetric encryption algorithm, such as
// AES is then used to encrypt/decrypt bulk data.
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] [C/C++]代码
// Needs #include
// Needs #include
CkString strOut;
CkPrivateKey pkey;
// Load the private key from an RSA PEM file:
pkey.LoadPemFile("myKey.pem");
bool success;
const char * pkeyXml;
// Get the private key in XML format:
pkeyXml = pkey.getXml();
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the private key into the RSA component:
success = rsa.ImportPrivateKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// OpenSSL uses BigEndian byte ordering:
rsa.put_LittleEndian(false);
// The resulting signature will be a Base64 string:
rsa.put_EncodingMode("base64");
// For simplicity, we're not loading
// the data to be signed from a file. We are instead simply
// using a literal string value.
const char * strData;
strData = "This is the text to be signed.";
// Hash the input using MD5, and then sign the hash:
// Other valid hash algorithm choices are "md2" and "sha-1".
const char * base64Sig;
base64Sig = rsa.signStringENC(strData,"md5");
strOut.append(base64Sig);
strOut.append("\r\n");
strOut.append("Success!\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
[代码] 验证java签名
// Needs #include
// Needs #include
CkString strOut;
CkPublicKey pubKey;
bool success;
// Load an RSA public key from an ASN.1 DER file
success = pubKey.LoadRsaDerFile("pubKey.der");
if (success != true) {
strOut.append(pubKey.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
const char * pkeyXml;
// Get the public key in XML format:
pkeyXml = pubKey.getXml();
CkRsa rsa;
// Any string argument automatically begins the 30-day trial.
success = rsa.UnlockComponent("30-day trial");
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
// Import the public key into the RSA component:
success = rsa.ImportPublicKey(pkeyXml);
if (success != true) {
strOut.append(rsa.lastErrorText());
strOut.append("\r\n");
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
return;
}
rsa.put_EncodingMode("base64");
const char * strData;
strData = "The quick brown fox jumps over the lazy dog";
// IMPORTANT: This Base64 signature will have to be updated
// with the digital signature produced by the Java code at:
// cknotes/?p=283
const char * base64Sig;
base64Sig = "VGV5A+bodHBpBwwJZdf17Bv+lkBTm/gteOf8iCgEEfNzBosZLaAB8X55BIZIkE2zKRXoMcJT+iCxsj+1hnlwJeKZ+Gya58lrHw6NWm2N0O/KyfnuEzADOM86X0xrkgdFT6SYpbZ9dWPC59NiHeEdVyjOXNJ3fBpUSQ5/5pvVWm0=";
// Verify the signature produced by the Java code at:
// cknotes/?p=283
// The VerifyStringENC method hashes the input data and verifies
// the hash against the signature.
success = rsa.VerifyStringENC(strData,"sha-1",base64Sig);
// Is the signature verified?
if (success == true) {
strOut.append("Signature Verified!");
strOut.append("\r\n");
}
else {
strOut.append("Signature not verified!");
strOut.append("\r\n");
}
SetDlgItemText(IDC_EDIT1,strOut.getUnicode());