Encryption ESAPI加密和解密

Encryption ESAPI加密和解密,encryption,groovy,base64,esapi,Encryption,Groovy,Base64,Esapi,我正在使用ESAPI Base64加密和解密,如中所示: 这就是我的代码的外观: import org.owasp.esapi.crypto.CipherText; import org.owasp.esapi.crypto.PlainText; import org.owasp.esapi.errors.EncryptionException; import org.owasp.esapi.reference.crypto.JavaEncryptor; import javax.crypto

我正在使用ESAPI Base64加密和解密,如中所示:

这就是我的代码的外观:

import org.owasp.esapi.crypto.CipherText;
import org.owasp.esapi.crypto.PlainText;
import org.owasp.esapi.errors.EncryptionException;
import org.owasp.esapi.reference.crypto.JavaEncryptor;
import javax.crypto.EncryptedPrivateKeyInfo
import org.owasp.esapi.ESAPI
import org.owasp.esapi.ValidationErrorList
import org.owasp.esapi.Validator
import org.apache.commons.codec.binary.Base64;
class SampleMain {
public String decrypt2(String cryptedText){
    String clearText=null;
    try {
        CipherText cipherText=CipherText.fromPortableSerializedBytes(Base64.decodeBase64(cryptedText));
        clearText=ESAPI.encryptor().decrypt(cipherText).toString();     
    }
    catch (  EncryptionException e) {
        System.out.println("EsapiEncryptor.decrypt: " + e.getMessage(),e);
    }
    return clearText.toString();
}

public String encrypt2(String clearText){
    String cryptedText=null;
    try {
        CipherText cipherText=ESAPI.encryptor().encrypt(new PlainText(clearText));
        cryptedText=Base64.encodeBase64(cipherText.asPortableSerializedByteArray());
    }
    catch (  EncryptionException e) {
        System.out.println("EsapiEncryptor.encrypt: " + e.getMessage(),e);
    }
    return cryptedText;
}

public static void main(String[] args) throws EncryptionException{

            String myplaintext = "MyPlaintext";
            SampleMain sample = new SampleMain();

            String enString = sample.encrypt2(myplaintext);
            System.out.println("-----------enString-----------: " + enString);

            String deString = sample.decrypt2(enString);
            System.out.println("-----------deString-----------: " + deString);  

        }

}
但是当我尝试运行这个简单的程序时,我得到以下异常:

Apr 01, 2017 12:43:30 PM org.owasp.esapi.reference.JavaLogFactory$JavaLogger log
WARNING: [SECURITY FAILURE Anonymous:null@unknown -> /DefaultName/IntrusionDetector] Likely tampering with KDF version on serialized ciphertext.KDF version read from serialized ciphertext (123190483) is out of range. Valid range for KDF version is [20110203, 99991231].
org.owasp.esapi.errors.EncryptionException: Version info from serialized ciphertext not in valid range.
    at org.owasp.esapi.crypto.CipherTextSerializer.convertToCipherText(CipherTextSerializer.java:299)
    at org.owasp.esapi.crypto.CipherTextSerializer.<init>(CipherTextSerializer.java:80)
    at org.owasp.esapi.crypto.CipherText.fromPortableSerializedBytes(CipherText.java:176)
    at org.owasp.esapi.crypto.CipherText$fromPortableSerializedBytes$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at gov.gsa.dss.test.SampleMain.decrypt2(SampleMain.groovy:30)
    at gov.gsa.dss.test.SampleMain$decrypt2$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at gov.gsa.dss.test.SampleMain.main(SampleMain.groovy:59)
2017年4月1日12:43:30 PM org.owasp.esapi.reference.JavaLogFactory$JavaLogger log
警告:[安全故障匿名:null@unknown->/DefaultName/IntrusionDetector]可能篡改了序列化密文上的KDF版本。从序列化密文(123190483)读取的KDF版本超出范围。KDF版本的有效范围为[20110203,99991231]。
org.owasp.esapi.errors.EncryptionException:序列化密文的版本信息不在有效范围内。
位于org.owasp.esapi.crypto.CipherTextSerializer.ConverttoChiperText(CipherTextSerializer.java:299)
位于org.owasp.esapi.crypto.CipherTextSerializer.(CipherTextSerializer.java:80)
位于org.owasp.esapi.crypto.CipherText.fromPortableSerializedBytes(CipherText.java:176)
位于org.owasp.esapi.crypto.CipherText$fromPortableSerializedBytes$0.call(未知源)
位于org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
位于gov.gsa.dss.test.SampleMain.decrypt2(SampleMain.groovy:30)
www.gov.gsa.dss.test.SampleMain$decrypt2$0.call(未知来源)
位于org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
位于org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
请访问gov.gsa.dss.test.SampleMain.main(SampleMain.groovy:59)
你知道我为什么会犯这样的错误或是这么简单的程序吗。谢谢。

这对我很有用:

public String decrypt2(String encryptedText) {
    byte[] encryptedTextTextAsBytes = encryptedText.getBytes(StandardCharsets.UTF_8)
    CipherText cipherText = CipherText.fromPortableSerializedBytes(Base64.decodeBase64(encryptedTextTextAsBytes))
    ESAPI.encryptor().decrypt(cipherText).toString()
}

public String encrypt2(String clearText) {
    CipherText cipherText = ESAPI.encryptor().encrypt(new PlainText(clearText))
    new String(Base64.encodeBase64(cipherText.asPortableSerializedByteArray()), StandardCharsets.UTF_8)
}

您正在向Base64.decodeBase64()传递一个字符串,它可能会编译,但我不确定Groovy如何处理它。您应该传递字节[](请参见如何获取EncryptedTextAsBytes)。这也许能解释你的错误,也许不能。我猜您没有发布产生您提到的错误的确切代码。

为什么不同时显示程序的输出?您只显示异常。这是输出的外观:------------enString----------------:[B@1e800aaa2017年4月1日12:43:30 PM org.owasp.esapi.reference.JavaLogFactory$JavaLogger日志警告:[匿名安全故障:null@unknown->/DefaultName/CryptoHelper]可能的数据篡改。遇到无效的KDF版本#。2017年4月1日12:43:30 PM org.owasp.esapi.reference.JavaLogFactory$JavaLogger log…您的代码甚至没有编译,但您已经提供了成功编译代码的运行时异常堆栈跟踪。为什么不显示实际导致问题的代码,而不是一些错误不相关的代码?对不起,我应该提到这是groovy代码,不完全是Java代码。但是它可以编译。谢谢@Hugues Moreau,你是对的,我必须传递bytes[],它对我有效。(原始帖子中的代码正是我产生错误的代码)。感谢你的帮助!!!