private void adjustRowHeights(Sheet sheet, List<RowInfo> rows, SortedSet<Integer> createdColumnNumbers) {
SortedMap<Integer, Float> columnWidthsInPx = [] as TreeMap;
createdColumnNumbers.each {
columnWidthsInPx.put(it, sheet.getColumnWidthInPixels(it));
}
rows.each { RowInfo rowInfo ->
if ( rowInfo.hasMergedCells ) {
Row excelRow = sheet.getRow(rowInfo.rowIndex);
// Find the column with the longest text - that's the one that will determine
// the row height
//
NestedCellInfo longestCell = rowInfo.getCellWithLongestContent();
String cellText = longestCell.getText();
if ( cellText != null && cellText.size() > 5 ) {
int colIdx = rowInfo.cells.indexOf(longestCell);
// Figure out available width in pixels, taking colspans into account
//
float columnWidthInPx = columnWidthsInPx[colIdx];
int numberOfMergedColumns = longestCell.colSpan;
(numberOfMergedColumns - 1).times {
columnWidthInPx += columnWidthsInPx[colIdx + it];
}
// Setup the font we'll use for figuring out where the text will be wrapped
//
XSSFFont cellFont = longestCell.getCellFont();
int fontStyle = Font.PLAIN;
if ( cellFont.getBold() ) fontStyle = Font.BOLD;
if ( cellFont.getItalic() ) fontStyle = Font.ITALIC;
java.awt.Font currFont = new java.awt.Font(
cellFont.getFontName(),
fontStyle,
cellFont.getFontHeightInPoints());
AttributedString attrStr = new AttributedString(cellText);
attrStr.addAttribute(TextAttribute.FONT, currFont);
// Use LineBreakMeasurer to count number of lines needed for the text
//
FontRenderContext frc = new FontRenderContext(null, true, true);
LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
int nextPos = 0;
int lineCnt = 0;
while (measurer.getPosition() < cellText.length()) {
nextPos = measurer.nextOffset( columnWidthInPx );
lineCnt++;
measurer.setPosition(nextPos);
}
if ( lineCnt > 1 ) {
excelRow.setHeight((short)(excelRow.getHeight() * lineCnt * /* fudge factor */ 0.7));
}
}
}
}
我发现这种方式可以加密字符串,工作正常,但当我尝试解密字符串时,上面的代码不起作用。 解密输出喜欢
S '= ɚ?
有人知道如何修复解密部分吗?提前感谢!
答案 0 :(得分:5)
正如我在评论中所述,这就是你所拥有的
//encrypt
$encryptxt=mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key,$str,MCRYPT_MODE_CBC,$iv);
$encryptxt64=base64_encode($encryptxt);
//decrypt
$decryptxt=mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,$str,MCRYPT_MODE_CBC,$iv);
$decryptxt64=base64_decode($decryptxt);
它应该是FILO(倒数第一个)
这样你就可以解密加密输出和不 base64编码输出,
喜欢如此:
$encryptxt=mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key,$str,MCRYPT_MODE_CBC,$iv);
$encryptxt64=base64_encode($encryptxt);
//decrypt
$decryptxt64=base64_decode($str);
$decryptxt=mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,$decryptxt64,MCRYPT_MODE_CBC,$iv);
作为注释,MCRYPT_RIJNDAEL_256不是AES 256,为此您需要MCRYPT_RIJNDAEL_128和32byte密钥,所以在某些方面128更好,(128是更小的块密码)
我建议做的另一件事是在输入字符串中使用md5哈希,然后在加密之前将其预先输入到输入字符串。这样,当您解密它时,您可以包含前32个字符并使用它来检查输入。基本上你需要知道输入字符串才能看出它是否被解密。但是,通过对它进行散列然后进行比较,您不再需要知道它是否有效。
所以一起(没有经过测试,但应该让你接近)
function encrypt ($key,$iv,$str)
{
$block=mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$padding=$block-(strlen($str) % $block);
$str.=str_repeat(chr($padding), $padding);
///prepend a md5 hash of the plain text input before encrypting it ( so we can check it later )
$str = md5( $str ) . $str;
$encryptxt=mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key,$str,MCRYPT_MODE_CBC,$iv);
$encryptxt64=base64_encode($encryptxt);
return $encryptxt64;
}
function decrypt ($key,$iv,$str)
{
$block=mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$padding=$block-(strlen($str) % $block);
$str.=str_repeat(chr($padding), $padding);
$decryptxt64=base64_decode($str);
$decryptxt=mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,$decryptxt64,MCRYPT_MODE_CBC,$iv);
///Separate the md5 hash from the other text, and then hash the other text and compare to the hash we included when encrypting if they are = it all worked.
/// it is perfectly safe to use md5 here because it will be part of what we encrypt, and not accessible until it is decrypted.
///it's sole purpose is to give us 2 things we can compare to check that the decryption worked
$hash = substr( $decryptxt, 0, 32); //find first 32 characters (md5 output is always 32 characters long )
$decryptxt = substr($decryptxt, 33); //find everything after the fist 32
if( $hash != md5($decryptxt) ){
die( 'fail' ); /// or some other error
}
return $decryptxt64;
}