用php加密并用Dart解密(flutter)

时间:2018-09-08 01:32:53

标签: php encryption dart flutter

有没有人想过要加密来自我的php api的响应并使用dart解密本地数据。我在移动应用程序中使用了flutter。

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以使用双方都支持的AES加密/解密。

flutter_string_encryption

PHP

答案 1 :(得分:0)

您可以使用Cipher2库在库中快速加密,借助库可以使用“ aes-128-cbc”方法加密和解密字符串

    //Make sure you import the library, stringEncryption is a user define function you can define your own
    stringEncryption() async { //call this method 
    String plainText ='String to encrypt';
    String key = '1245714587458745'; //combination of 16 character
    String iv = 'e16ce913a20dadb8'; ////combination of 16 character
    String encryptedString =
    await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);
    print("key:$key");
    print("iv:$iv");
    print("String:$encryptedString");

    //for decrypt use decrypt function 
    decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);
    //parameters: encryptedString,sameKey,SameIv
    }

    //To decrypt in PHP
    $method = 'aes-128-cbc';
    $decryptedString = openssl_decrypt("encryptedString", $method, "SameKeyUsedInFlutter", 0, "SameIvUsedInFlutter");

    //To encrypt in PHP
    $encryptedString = openssl_encrypt("Text to encrypt", $method, "SameKeyUsedInFlutter", 0, "SameIvUsedInFlutter");
    //Key and IV must need to match

答案 2 :(得分:0)

function CryptoJSAesDecrypt(passphrase,encrypted_json_string){

var obj_json = JSON.parse(encrypted_json_string);

var encrypted = obj_json.ciphertext;
var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
var iv = CryptoJS.enc.Hex.parse(obj_json.iv);   

var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999});


var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv});

return decrypted.toString(CryptoJS.enc.Utf8);

} 我想将 php func crypto 隐藏起来