【问题标题】:AES between Android and OpenbsdAndroid 和 Openbsd 之间的 AES
【发布时间】:2013-03-09 14:29:54
【问题描述】:

我是使用 AES 加密的新手。我需要在 Android 上加密一个字符串并将其发送到 OpenBSD 进行解密。 我可以使用 OpenSSl 在 Openbsd 上加密/解密,而 Android 使用此代码,但来自 Android 的加密字符串不等于 OpenBSD 中的解密字符串 谁能帮帮我。

...
public class StringCryptor 
{
    private static final String CIPHER_ALGORITHM = "AES";
    private static final String RANDOM_GENERATOR_ALGORITHM = "SHA1PRNG";
    private static final int RANDOM_KEY_SIZE = 128;
    // Private key already generated with generatekey()
    static String PKEY= "15577737BBD910E794A6B3C250678DAF";
    // Convert PKEY to byte[]
    static byte[] secretKey = toByte(PKEY);

    // Encrypts string and encode in Base64
    public static String encrypt( String password, String data ) throws Exception 
    {
         byte[] clear = data.getBytes();
         SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, CIPHER_ALGORITHM );
         Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
         cipher.init( Cipher.ENCRYPT_MODE, secretKeySpec );
         byte[] encrypted = cipher.doFinal( clear );
         String encryptedString = Base64.encodeToString( encrypted, Base64.DEFAULT );
         return encryptedString;
    }

    // Decrypts string encoded in Base64
    public static String decrypt( String password, String encryptedData ) throws Exception 
    {
         SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );
         Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
         cipher.init( Cipher.DECRYPT_MODE, secretKeySpec );
         byte[] encrypted = Base64.decode( encryptedData, Base64.DEFAULT );
         byte[] decrypted = cipher.doFinal( encrypted );
         return new String( decrypted );
    }


    // Convert String To Hexa
    public static String toHex(byte[] buf) { 
        if (buf == null)  
            return "";    
        StringBuffer result = new StringBuffer(2*buf.length);  
        for (int i = 0; i < buf.length; i++) {
           appendHex(result, buf[i]);       
        }        
       return result.toString();
    }
  // Convert hex To byte
  public static byte[] toByte(String hexString) { 
        int len = hexString.length()/2;      
        byte[] result = new byte[len];        
        for (int i = 0; i < len; i++)               
            result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();   
        return result;
  }

  private final static String HEX = "0123456789ABCDEF";
  private static void appendHex(StringBuffer sb, byte b) {
      sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  }

//public static byte[] generateKey( byte[] seed ) throws Exception
    //{
    //KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM );
        //SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM );
        //secureRandom.setSeed( seed );
        //keyGenerator.init( RANDOM_KEY_SIZE, secureRandom );
        //SecretKey secretKey = keyGenerator.generateKey();
        //return secretKey.getEncoded();
    //}
}
...

android生成的加密字符串

    Bonjour >>>>>>> NkrWPLgiY0rt34iaNzhjOg==

在 OpenBSD 中,我使用 android 中生成的私钥加密字符串

#openssl version
openSSl 0.9.8k 25 Mar 2009
#echo "Bonjour">test.txt
#openssl enc -aes1278 -a -in text.txt -K 15577737BBD910E794A6B3C250678DAF -iv 0
4UwyKgMGJ41xPwTph2qHXQ==

【问题讨论】:

  • 两边的钥匙一样吗?

标签: android aes openbsd


【解决方案1】:

我花时间测试了你的代码,问题很简单。如果您这样做echo "Bonjour" &gt; test.txt,则会自动将“换行”添加到Bonjour

因此,在 Java 中,您加密了字符串 "Bonjour",但 openssl 读取的 text.txt 文件包含字符串 "Bonjour\n"。您可以通过将-n 标志添加到echo 来更改它。现在 openssl 应该打印和 Java 一样:

$ echo -n "Bonjour" > test.txt
$ openssl enc -aes128 -a -in test.txt -K 15577737BBD910E794A6B3C250678DAF  -iv 0
NkrWPLgiY0rt34iaNzhjOg==

显然我说IV是Java随机生成的说法是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多