【发布时间】:2017-01-06 12:01:07
【问题描述】:
我想在 Swift 3 中转换以下 base64 编码的String:
dfYcSGpvBqyzvkAXkdbHDA==
与其等价的String:
uöHjo¬³¾@‘ÖÇ
以下网站做得很好:
http://www.motobit.com/util/base64-decoder-encoder.asp
http://www.utilities-online.info/base64/#.WG-FwrFh2Rs
PHP 的函数base64_decode 也是如此。这个函数的documentation 说:
如果输入包含 base64 之外的字符,则返回 FALSE 字母表。
但我无法在 Swift 3 中做同样的事情。以下代码也不能完成这项工作:
func convertBase64ToNormalString(base64String:String)->String!{
let decodedData = Data(base64Encoded: base64String, options: Data.Base64DecodingOptions())
let bytes = decodedData?.bytes
return String(bytes: bytes, encoding: NSUTF8StringEncoding)
}
这里是关于为什么我需要将 base64 字符串转换为字符串的上下文信息: 我的 Php 开发人员希望我发送所有使用 AES 算法加密的 API 参数值。为此,我使用this lib。 他给了我十六进制格式的 AES 密钥(我在我的 last question 中提到)和 base64 中的 iv(上面给出),他指示我在使用之前解码这个 base64 密钥,因为他也在他的 PHP 代码中做同样的事情。下面是他的PHP加解密代码:
function encryptParamAES($plaintext, $encryptionEnabled = true) {
if (! $encryptionEnabled) {
return $plaintext;
}
// --- ENCRYPTION ---
// Constants =========================================================
// the key should be random binary, use scrypt, bcrypt or PBKDF2 to
// convert a string into a key
// key is specified using hexadecimal
$key = pack ( 'H*', "dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4" );
// create a random IV to use with CBC encoding
$iv_size = 16;
$iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
// End Of constants ===================================================
// echo "IV: " . base64_encode ( $iv ) . "\n<br>";
// echo "IV Size: " . $iv_size . "\n<br>";
// show key size use either 16, 24 or 32 byte keys for AES-128, 192
// and 256 respectively
// $key_size = strlen ( $key );
// echo "Key size: " . $key_size . "\n<br><br>";
// creates a cipher text compatible with AES (Rijndael block size = 128)
// to keep the text confidential
// only suitable for encoded input that never ends with value 00h
// (because of default zero padding)
$ciphertext = mcrypt_encrypt ( MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv );
// prepend the IV for it to be available for decryption
$ciphertext = $iv . $ciphertext;
// encode the resulting cipher text so it can be represented by a string
$ciphertext_base64 = base64_encode ( $ciphertext );
return $ciphertext_base64;
}
function decryptParamAES($ciphertext_base64, $encryptionEnabled = true) {
if (! $encryptionEnabled) {
return $ciphertext_base64;
}
// --- DECRYPTION ---
// Constants =========================================================
// the key should be random binary, use scrypt, bcrypt or PBKDF2 to
// convert a string into a key
// key is specified using hexadecimal
$key = pack ( 'H*', "dcb04a9e103a5cd8b53763051cef09bc66abe029fdebae5e1d417e2ffc2a07a4" );
// create a random IV to use with CBC encoding
$iv_size = 16;
$iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
// End Of constants ===================================================
$ciphertext_dec = base64_decode ( $ciphertext_base64 );
// retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
$iv_dec = substr ( $ciphertext_dec, 0, $iv_size );
// retrieves the cipher text (everything except the $iv_size in the front)
$ciphertext_dec = substr ( $ciphertext_dec, $iv_size );
// may remove 00h valued characters from end of plain text
$plaintext_dec = mcrypt_decrypt ( MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec );
return rtrim ( $plaintext_dec );
}
我刚看到这段 PHP 代码,想知道为什么他不使用 $iv 作为mcrypt_decrypt 函数的最后一个参数!!会更新你。但问题仍然存在,PHP 函数 base64_decode 不会为上述 base64 字符串返回 FALSE!
我自己通过终端命令测试了这个功能:php test.php。这里 test.php 包含以下代码:
<?php
$iv = base64_decode ( "dfYcSGpvBqyzvkAXkdbHDA==" );
echo $iv;
?>
输出是:u?Hjo???@???
【问题讨论】:
-
这与您之前的问题stackoverflow.com/questions/41485494/… 中的问题基本相同,我的建议stackoverflow.com/a/41487408/1187415 也是相同的:不要 将数据转换为字符串。 – 如果您必须这样做(出于某种神秘原因),请选择不同的编码(如您之前问题的答案中所述)。
标签: character-encoding swift3 base64