我正在ios End上进行AES加密,而我对base64编码该字符串并将其发送到php端。在php端,我有以下代码:
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
infoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:infoLabel];
UILabel *secondInfoLabel = [[UILabel alloc] initWithFrame:infoLabel.frame];
secondInfoLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:secondInfoLabel];
NSDictionary * secondTextAttributes =
@{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSStrokeColorAttributeName : [UIColor whiteColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-3.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
secondInfoLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Welcome To" attributes:secondTextAttributes];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(0, 0);
shadow.shadowBlurRadius = 2;
NSDictionary * textAttributes =
@{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : [UIColor blackColor],
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:-6.0],
NSFontAttributeName : [UIFont systemFontOfSize:40] };
infoLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Welcome To" attributes:textAttributes];
但是,当我运行此代码时,我得到了这个"anil.mnd@gmail.cA���u�" 。我应该有anil.mnd@gmail.com。我只得到前16个字符正确。
我是加密新手,所以不知道出了什么问题。
答案 0 :(得分:4)
我是加密新手,所以不知道出了什么问题。
如果您是新手,想要“正常工作”的内容,请使用defuse/php-encryption而不是尝试自己编写。
但是,如果你想迎接挑战,请继续阅读:
您的代码无法阅读。让我们添加一些空格。
$(function() {
$( ".sortable" ).sortable({
revert: true
});
$( ".draggable" ).draggable({
connectToSortable: ".sortable",
helper: "clone",
revert: "invalid"
});
$( "ul, li" ).disableSelection();
});
具体问题:
$key = 'a16byteslongkey!';
$data = base64_decode('LsCH4nvvGPKN67v94Ig9BweQgOk9rtDdK7ZugeJkTS8=');
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = substr($data, 0, $iv_size);
function aes256_cbc_decrypt($key, $data, $iv)
{
if (32 !== strlen($key)) {
$key = hash('SHA256', $key, true);
}
if (16 !== strlen($iv)) {
$iv = hash('MD5', $iv, true);
}
$data = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
$data,
MCRYPT_MODE_ECB,
$iv
);
$padding = ord($data[strlen($data) - 1]);
return substr($data, 0, -$padding);
}
$result = aes256_cbc_decrypt($key,$data,$iv);
var_dump($result);
作为名为MCRYPT_MODE_ECB
的函数(您看过the penguin吗?)具体建议:
aes256_cbc
和strlen()
很脆弱。请参阅:function overloading。random_bytes()
。