这个并不是我的原创,只是学习了一下VFP自带的类crypto.scx。
界面如下图——
原始文本
加密后的结果
相关代码
form.init
#DEFINE VFPROCKS_LOC"Visual FoxPro Rocks!"
#DEFINE DISPLAYWIDTH35
#DEFINENOCSP_LOC"The required Crypto Service Provider is not installed or not functioning properly on this system. Some or all of these samples may not work."
PUBLIC _cryptapi
_cryptapi=NEWOBJECT("_cryptapi","C:\Program Files (x86)\VFP9\Samples\Solution\Ffc\_crypt.vcx")
IF !_cryptapi.GetIsInstalled()
MESSAGEBOX(NOCSP_LOC)
RETURN .F.
ENDIF
新建方法
#DEFINE PAGE1_DESC_LOC"You can encrypt/decrypt strings using the Windows Crypto API routines. "+;
"Stream encryption provides for fast compact encryption. Block encryption offers a slower, "+;
"but stronger safer algorithm scheme."
#DEFINE PAGE1a_DESC_LOC"Click on the Encrypt button to encrypt text in the Password field using the "+;
"Key specified in the Key field."
流加密
LOCAL lcEncryptedStream,lcPassword,lcKey
lcEncryptedStream = ''
lcPassWord = THISFORM.txtPassword.Value
lcKey = THISFORM.txtKey.Value
IF _cryptapi.EncryptSessionStreamString(lcPassWord, lcKey, @lcEncryptedStream)
THISFORM.txtPassword.Value=""
THISFORM.txtPassword.Value = STRCONV(lcEncryptedStream,13)
ELSE
MESSAGEBOX("Error: "+MESSAGE())
RETURN
ENDIF
THISFORM.txtPassword.Readonly = .T.
THISFORM.txtKey.Readonly = .T.
THISFORM.txtPassword2.Readonly = .T.
THISFORM.txtKey2.Readonly = .T.
THISFORM.cmdEncrypt.Enabled = .F.
THISFORM.cmdDecrypt.Enabled = .T.
THISFORM.cmdEncrypt2.Enabled = .F.
流解密
LOCAL lcDecryptedStream,lcPassword,lcKey
lcDecryptedStream = ''
lcPassWord = STRCONV(THISFORM.txtPassword.Value,14)
lcKey = THISFORM.txtKey.Value
IF _cryptapi.DecryptSessionStreamString(lcPassWord, lcKey, @lcDecryptedStream)
THISFORM.txtPassword.Value=""
THISFORM.txtPassword.Value = lcDecryptedStream
ELSE
MESSAGEBOX("Error: "+MESSAGE())
ENDIF
THISFORM.txtPassword.Readonly = .F.
THISFORM.txtKey.Readonly = .F.
THISFORM.txtPassword2.Readonly = .F.
THISFORM.txtKey2.Readonly = .F.
THISFORM.cmdEncrypt.Enabled = .T.
THISFORM.cmdDecrypt.Enabled = .F.
THISFORM.cmdEncrypt2.Enabled = .T.
块加密
LOCAL lcEncryptedStream,lcPassword,lcKey
lcEncryptedStream = ''
lcPassWord = THISFORM.txtPassword2.Value
lcKey = THISFORM.txtKey2.Value
IF _cryptapi.EncryptSessionBlockString(lcPassWord, lcKey, @lcEncryptedStream)
THISFORM.txtPassword2.Value=""
THISFORM.txtPassword2.Value = STRCONV(lcEncryptedStream,15)
ELSE
IF _cryptapi.GetDoubleEncryptError()
MESSAGEBOX("Sorry, you cannot double-encrypt this text.")
ELSE
MESSAGEBOX("Error: "+MESSAGE())
ENDIF
RETURN
ENDIF
THISFORM.txtPassword2.Readonly = .T.
THISFORM.txtKey2.Readonly = .T.
THISFORM.txtPassword.Readonly = .T.
THISFORM.txtKey.Readonly = .T.
THISFORM.cmdEncrypt.Enabled = .F.
THISFORM.cmdEncrypt2.Enabled = .F.
THISFORM.cmdDecrypt2.Enabled = .T.
块解密
LOCAL lcDecryptedStream,lcPassword,lcKey
lcDecryptedStream = ""
lcPassWord = STRCONV(THISFORM.txtPassword2.Value,16)
lcKey = THISFORM.txtKey2.Value
IF _cryptapi.DecryptSessionBlockString(lcPassWord, lcKey, @lcDecryptedStream)
THISFORM.txtPassword2.Value=""
THISFORM.txtPassword2.Value = lcDecryptedStream
ELSE
MESSAGEBOX("Error: "+MESSAGE())
ENDIF
THISFORM.txtPassword2.Readonly = .F.
THISFORM.txtKey2.Readonly = .F.
THISFORM.txtPassword.Readonly = .F.
THISFORM.txtKey.Readonly = .F.
THISFORM.cmdDecrypt2.Enabled = .F.
THISFORM.cmdEncrypt2.Enabled = .T.
THISFORM.cmdEncrypt.Enabled = .T.
原始文件下载
http://gasz.netniu.cn/webfiles/images/202301/vfpcrypt.scx
http://gasz.netniu.cn/webfiles/images/202301/vfpcrypt.sct
最新回复