如何在Java中加密并在Android和iOS中解密[英] How to encrypt in Java and decrypt in Android and iOS

本文是小编为大家收集整理的关于如何在Java中加密并在Android和iOS中解密的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个运行Java-jar文件的Linux服务器,该文件加密多个文件.

Android和iPhone应用程序下载该文件,并将其解密.我必须使用什么算法?

我认识到我在Java中使用的算法在Android中不起作用.我在Java中所做的是:

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

上述代码中没有工作的? 有其他选择吗?

推荐答案

ios:

我使用nsstring+aescrypt( >)

样本:

NSString* encrypted = [plainText AES256EncryptWithKey:@"MyEncryptionKey"];
NSString* decrypted = [encrypted AES256DecryptWithKey:@"MyEncryptionKey"];

android(aes256cipher- :

加密:

String base64Text="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Request(crypt) ##############
    cipherData = AES256Cipher.encrypt(ivBytes, keyBytes, passval1.getBytes("UTF-8"));
    base64Text = Base64.encodeToString(cipherData, Base64.DEFAULT);
}
catch ( Exception e ) {
    e.printStackTrace();
}        

解密:

String base64Text="";
String plainText="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Response(decrypt) ##############
base64Text = User.__currentUser.getPasscode();
    cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
    plainText = new String(cipherData, "UTF-8");            
}
catch ( Exception e )
{
    e.printStackTrace();
}

其他推荐答案

下面的链接提供了使用对称键加密的加密和解密的一个很好的例子.

使用的对称键是自定义纯文本. 如果我们需要使用iOS设备进行解密,这会有所帮助. 该示例使用AES 128位加密.请注意,它使用IV参数. 由于加密为128位,键的长度应为16.

在Android方面,可以使用相同的方法实现,因为该语言是Java.在iOS commonCryptor.h 中可用于加密解密.

http:///www.java-- redefined.com/2015/06/symmetric-key-ecryption-ios-java.html

本文地址:https://www.itbaoku.cn/post/102546.html

问题描述

I have a Linux server running a Java-jar file that encrypts several files.

The Android and iPhone App download that file and shall decrypt it. What algorithm I have to use to do so?

I recognized that the algorithms I used in Java do not work in Android. What I did in Java was:

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

what didn't work in above code? Any alternatives?

推荐答案

iOS:

I use NSString+AESCrypt (https://github.com/Gurpartap/AESCrypt-ObjC)

Sample:

NSString* encrypted = [plainText AES256EncryptWithKey:@"MyEncryptionKey"];
NSString* decrypted = [encrypted AES256DecryptWithKey:@"MyEncryptionKey"];

Android (AES256Cipher - https://gist.github.com/dealforest/1949873):

Encrypt:

String base64Text="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Request(crypt) ##############
    cipherData = AES256Cipher.encrypt(ivBytes, keyBytes, passval1.getBytes("UTF-8"));
    base64Text = Base64.encodeToString(cipherData, Base64.DEFAULT);
}
catch ( Exception e ) {
    e.printStackTrace();
}        

Decrypt:

String base64Text="";
String plainText="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Response(decrypt) ##############
base64Text = User.__currentUser.getPasscode();
    cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
    plainText = new String(cipherData, "UTF-8");            
}
catch ( Exception e )
{
    e.printStackTrace();
}

其他推荐答案

The link below gives a nice example of encryption and decryption using Symmetric key encryption.

The symmetric key used is a custom plain text. This helps if we need to to decrypt using a IOS device. The example uses a AES 128 bit encryption. Note that it uses IV parameters. Since the encryption is 128 bit the length of the key should be 16.

On Android side the same method implementations can be used since the language is Java. In IOS CommonCryptor.h can be used for encryption decryption.

http://www.java-redefined.com/2015/06/symmetric-key-encryption-ios-java.html

相关标签/搜索