android中的AES加密和php中的解密,反之亦然

皮胡

我试图通过针对https://aesencryption.net测试我的代码来学习AES 我以前在Base64.encodeBase64String和也有一个错误Base64.decodeBase64 // encode/decode Base64因此,我以某种方式操纵了Base64以解决该错误。我认为,现在在我的应用程序中,文本已正确加密和解​​密。但是,当我尝试在服务器端(位于aesencryption.net)加密或解密相同的文本时,该站点无法解密我的加密字符串。请帮忙。

以下是我的代码:

public class MainActivity extends AppCompatActivity {

    static final String TAG = "SymmetricAlgorithmAES";
    private static SecretKeySpec secretKey ;
    private static byte[] key ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Original text
        // {"type":"Success","httpCode":"200","code":"200","message":{"pin":"11111"},"extra":""}
        String theTestText = "hi";
        TextView tvorig = (TextView)findViewById(R.id.tvorig);
        tvorig.setText("\n[ORIGINAL]:\n" + theTestText + "\n");
        final String strPssword = "android";
        setKey(strPssword);

        // Encode the original data with AES
        byte[] encodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE,secretKey);
            encodedBytes = c.doFinal(theTestText.getBytes());
        } catch (Exception e) {
            Log.e(TAG, "AES encryption error");
        }

        TextView tvencoded = (TextView)findViewById(R.id.tvencoded);
        tvencoded.setText("[ENCODED]:\n" +
                Base64.encodeToString(encodedBytes, Base64.DEFAULT) + "\n");

        Log.d(TAG, Base64.encodeToString(encodedBytes, Base64.DEFAULT));


        // Decode the encoded data with AES
        byte[] decodedBytes = null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, secretKey);
            decodedBytes = c.doFinal(encodedBytes);
        } catch (Exception e) {
            Log.e(TAG, "AES decryption error");
        }
        TextView tvdecoded = (TextView)findViewById(R.id.tvdecoded);
        tvdecoded.setText("[DECODED]:\n" + new String(decodedBytes) + "\n");
    }



    public static void setKey(String myKey){
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            System.out.println(key.length);
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            System.out.println(key.length);
            System.out.println(new String(key,"UTF-8"));
            secretKey = new SecretKeySpec(key, "AES");


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

}

提前致谢。

皮胡

我这样做确实有效;)

public class AESCrypter {
    private final Cipher cipher;
    private final SecretKeySpec key;
    private AlgorithmParameterSpec spec;


    public AESCrypter(String password) throws Exception
    {
        // hash password with SHA-256 and crop the output to 128-bit for key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(password.getBytes("UTF-8"));
        byte[] keyBytes = new byte[32];
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        key = new SecretKeySpec(keyBytes, "AES");
        spec = getIV();
    }

    public AlgorithmParameterSpec getIV()
    {
        byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
        IvParameterSpec ivParameterSpec;
        ivParameterSpec = new IvParameterSpec(iv);

        return ivParameterSpec;
    }

    public String encrypt(String plainText) throws Exception
    {
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
        String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");

        return encryptedText;
    }

    public String decrypt(String cryptedText) throws Exception
    {
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
        byte[] decrypted = cipher.doFinal(bytes);
        String decryptedText = new String(decrypted, "UTF-8");

        return decryptedText;
    }
}

像这样调用此类:

 try {
            AESCrypter _crypt = new AESCrypter("password");
            String output = "";
            String plainText = "top secret message";
            output = _crypt.encrypt(plainText); //encrypt
            System.out.println("encrypted text=" + output);
            output = _crypt.decrypt(output); //decrypt
            System.out.println("decrypted text=" + output);
        } catch (Exception e) {
            e.printStackTrace();
        }

对于iPhone(代码在这里): https://github.com/Gurpartap/AESCrypt-ObjC

希望这段代码也对您有用:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

解密以C#加密的SQL Anywhere 16中的AES,反之亦然

将 Elixir 加密和解密逻辑映射到 PHP,反之亦然

Android中的AES加密和Node.js中的解密

在Scala中可以尝试,反之亦然

反之亦然,在Admin Django中

Android中的AES加密解密算法

压缩然后加密,反之亦然?

如何将 AES 加密输出字节转换为字符串,反之亦然

从C#中的类访问表单方法和变量,反之亦然

是否可以在TWIG基本模板中包含PHP模板,反之亦然?

如何在Android中从屏幕顶部到屏幕的300dp设置动画,反之亦然?

在 Android Studio 中字符串到二维码,反之亦然

如何在Hive中访问HBase表,反之亦然?

在Fortran中按名称获取文件单元,反之亦然

更改播放按钮以在Angular中暂停,反之亦然

将字节转换为 dart 中的位,反之亦然

替換 HTML 代碼中的 BBCodes,反之亦然

如何在Python中实现反之亦然的映射?

在 bash 中,如何用 null 替换空行,反之亦然?

在Swift中从左向右循环UISlider,反之亦然

使Windows字体在linux Mint中工作,反之亦然

eclipse中的字体从Mac到PC失败,反之亦然

如何检查点落在矩形中,反之亦然?

Xcode中的AES128加密和PHP中的解密成功与否取决于密钥值

Go中的AES-CTR加密和CryptoJS中的解密

JavaScript中的AES加密和Java中的解密

Java 中的加密和 Flutter 中的解密 (AES-256)

Java解密中的AES 128加密(PHP中)

PHP中的简单AES加密,Java中的解密