问题描述
当我加密下面的字符串时,下面的字符串的长度显然大于1024,并且一旦加密就可以解密,但是一旦字符串减少到小于1024,就可以解密而不会挂起
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
+ "<FTSingleCreditResponse>\n"
+ " <Amount>1000.00</Amount>\n"
+ " <BeneficiaryAccountName>Akogun Olasoji</BeneficiaryAccountName>\n"
+ " <BeneficiaryAccountNumber>000000020</BeneficiaryAccountNumber>\n"
+ " <BeneficiaryBankVerificationNumber></BeneficiaryBankVerificationNumber>\n"
+ " <BeneficiaryKYCLevel>1</BeneficiaryKYCLevel>\n"
+ " <ChannelCode>12</ChannelCode>\n"
+ " <DestinationInstitutionCode>999194</DestinationInstitutionCode>\n"
+ " <NameEnquiryRef>999999200825144654200825144654</NameEnquiryRef>\n"
+ " <Narration>Transaction from me you</Narration>\n"
+ " <OriginatorAccountName>Ajao Niyi</OriginatorAccountName>\n"
+ " <OriginatorAccountNumber>0105498919</OriginatorAccountNumber>\n"
+ " <OriginatorBankVerificationNumber>10000000002</OriginatorBankVerificationNumber>\n"
+ " <OriginatorKYCLevel>1</OriginatorKYCLevel>\n"
+ " <PaymentReference>ref/2015/08/05/val 3</PaymentReference>\n"
+ " <ResponseCode>00</ResponseCode>\n"
+ " <SessionID>999999200827170808200827170807</SessionID>\n"
+ " <TransactionLocation>6.4300747,3.4110715</TransactionLocation>\n"
+ "</FTSingleCreditResponse>";
PGPLib在解密上述字符串的加密结果时会动手。
下面是我使用pgpLib进行的加密和解密方法
加密
public String encrypt(String text) {
try {
PGPLib pgp = new PGPLib();
boolean armor = false;
boolean withIntegrityCheck = true;
ByteArrayOutputStream o = new ByteArrayOutputStream();
logger.debug("step 1");
InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
logger.debug("step 2");
if (publicKeyFile == null) {
publicKeyFile = new File(publicKeyLocation);
}
logger.debug("step 3");
InputStream publicKeyStream = new FileInputStream(publicKeyFile);
logger.debug("step 4");
pgp.encryptStream(is,publicKeyLocation,publicKeyStream,o,armor,withIntegrityCheck);
logger.debug("step 5");
byte[] body = o.toByteArray();
int numberRead = body.length;
logger.debug(" -|- SSModuleClient :: pickMessage :: numberRead: " + numberRead);
return byte2hex(body);
} catch (IOException ex) {
logger.debug(" -|- SSModule :: MessageProcessor :: encrypt () :: Error Occurred ..."
+ ex.getMessage());
logger.debug("SSModule",ex);
} catch (PGPException ex) {
logger.debug(" -|- SSModule :: MessageProcessor :: encrypt () :: Error Occurred ..."
+ ex.getMessage());
logger.debug("SSModule",ex);
}
return "";
}
解密
public String decrypt(String text) {
String decryptedMessage = "";
try {
byte[] bytText = hex2byte(text);
PGPLib pgp = new PGPLib();
InputStream iStream = new ByteArrayInputStream(bytText);
logger.debug("SSModule :: decrypt () :: iStream.available(): "
+ iStream.available());
if (privateKeyFile == null) {
privateKeyFile = new File(privateKeyLocation);
}
InputStream privateKeyStream = new FileInputStream(privateKeyLocation);
PipedInputStream pin = new PipedInputStream();
OutputStream oStream = new PipedOutputStream(pin);
logger.debug("text: " + text);
logger.info("privateKeyPassphrase: " + privateKeyPassphrase);
pgp.decryptStream(iStream,privateKeyStream,getPrivateKeyPassphrase(),oStream);
do {
logger.debug("pin.available(): " + pin.available());
} while (pin.available() <= 0);
byte[] body = new byte[pin.available()];
pin.read(body);
decryptedMessage = new String(body).toString();
} catch (Exception ex) {
logger.debug("SSModule :: MessageProcessor :: decrypt () :: Error Occurred ..."
+ ex.getMessage());
logger.debug("SSModule",ex);
}
return decryptedMessage;
}
从解密方法来看,它不会超出pgp.decryptStream
,它只是挂起并且不会走得更远,其他字符串可以很好地加密和解密,但是这个特定字符串不能做到
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)