繁体   English   中英

Java字符串加密

[英]Java string encrypt

我在Objective C中使用加密类作为我的iPhone应用程序,但我很难从我的Android应用程序中获得在JAVA中工作的相同功能。 我的加密代码如下:

NSString * _secret = @"password";
NSString * _key = @"1428324560542678";

StringEncryption *crypto = [[StringEncryption alloc] init];
NSData *_secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding];
CCOptions padding = kCCOptionPKCS7Padding;
NSData *encryptedData = [crypto encrypt:_secretData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];

我试图在JAVA中复制它,但是当我编码相同的数据时,我得到一个不同的字符串。 所以我做错了什么,但我无法弄清楚。 这是我的JAVA代码:

byte[] key = "1428324560542678".getBytes();

Cipher c = null;
            try {
                c = Cipher.getInstance("AES/ECB/PKCS7Padding");
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

SecretKeySpec k =  new SecretKeySpec(key, "AES");
            try {
                c.init(Cipher.ENCRYPT_MODE, k);
            } catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    try {
        EditText tv1passwordText = (EditText) findViewById(R.id.password);
        String password = URLEncoder.encode(tv1passwordText.getText().toString(), "UTF-8");

            byte[] encryptedData = c.doFinal( password.getBytes());

任何人都可以看到我错在哪里?

基于以下评论,我添加了getBytes,但生成的字符串仍然不同:

byte[] key = null;
            try {
                key = "1428324560542678".getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            Cipher c = null;
            try {
                c = Cipher.getInstance("AES/ECB/PKCS7Padding");
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            SecretKeySpec k =  new SecretKeySpec(key, "AES");
            try {
                c.init(Cipher.ENCRYPT_MODE, k);
            } catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                EditText tv1passwordText = (EditText) findViewById(R.id.password);

                byte[] password = tv1passwordText.getText().toString().getBytes("UTF-8");

                byte[] encryptedData = c.doFinal(password);

以下是加密和解密的示例:

public static SecretKey generateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    return secret = new SecretKeySpec(password.getBytes(), "AES");
}

public static byte[] encryptMsg(String message, SecretKey secret) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
/* Encrypt the message. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
    return cipherText;
}

public static String decryptMsg(byte[] cipherText, SecretKey secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {

    /* Decrypt the message, given derived encContentValues and initialization vector. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   cipher.init(Cipher.DECRYPT_MODE, secret);
    String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
    return decryptString;
}

要加密:

    SecretKey secret = EncUtil.generateKey();
    EncUtil.encryptMsg(<String to Encrypt>, secret))

解密

    EncUtil.decryptMsg(<byte[]>, secret))

如果可能,您应该使用CBC或CTR,而不是使用ECB。 欧洲央行不安全

看起来您的Objective-C代码使用的是UTF-8编码,但您没有在Java代码中指定它。 使用getBytes("UTF-8")

我注意到的一件事在过去引起的问题是加密的iOS字符串实际上是"Hello World\\0" ,例如最后要加密的字符串加密。 所以尝试在Java中将字符串末尾添加\\0 ,看看它是否产生相同的结果。

另外,java上的URLEncoder步骤可能会引入额外的控制字符以及iOS端不存在的其他内容。 将此后的文本与进入iOS加密步骤的文本进行比较以确保它们完全相同可能是值得的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM