異なる言語でのAES暗号化と復号化

AES暗号化モードとモードを埋める:他にはあります

暗号化データ暗号化された16バイト長の長さが16バイトより少ないアルゴリズム/モード/パディング
AES / CBC / NoPadding 16がサポートしていない
AESを/ CBC / 32 16 PKCS5Padding 
ISO10126Padding AES / CBC / 32 16 
AES / CFB / 16元のデータ長NoPaddingを
AES / CFB / 32 16はPKCS5Padding 
ISO10126Padding AES / CFB / 32 16を
AES / ECB / NoPadding 16がサポートしていない
PKCS5Padding AES / ECB / 32 16 
AES / ISO10126Padding ECB / 32 16 
AES / OFB / 16元のデータ長NoPaddingを
AES / OFB / PKCS5Padding 32 16 
AES / OFB / ISO10126Padding 32 16 
AES / PCBC / NoPadding 16不支持
AES / PCBC / PKCS5Padding 32 16 
AES / PCBC / ISO10126Padding 32 16

  ます。http://blog.sina.com.cn/s/blog_679daa6b0100zmpp.html暗号化モードコンテンツの詳細

PHP AES暗号化のみ(ゼロパディング - データ長が16の整数倍ではないので、充填する必要がある)ZeroPadding充填、およびJavaではありません

このモードを満たしている、唯一のカップはJavaがNoPaddingを使用するためのパターンを埋めること、書くこと(コンテンツで満たされていません)

Javaの側のコード:

輸入javax.crypto.Cipher; 
輸入javax.crypto.spec.IvParameterSpec; 
インポートするjavax.crypto.spec.SecretKeySpec; 
輸入sun.misc.BASE64Decoder。

パブリッククラス暗号
{ 
    公共の静的な無効メイン(文字列引数[])をスロー例外{ 
        System.out.printlnは(暗号化())。
        System.out.println(DESENCRYPT())。
    } 

    パブリック静的文字列の暗号化()は、例外がスロー{ 
        {試みる
            =「テスト文字列」文字列データを、
            文字列のキー= "1234567812345678"; 
            文字列のIV = "1234567812345678"; 

            暗号暗号= Cipher.getInstance( "AES / CBC / NoPadding")。
            int型のblockSize =暗号。

            バイト[]データバイトの=のdata.getBytes()。
            int型plaintextLength = dataBytes.length。
            IF(!plaintextLength%のblockSize = 0){ 
                plaintextLength = plaintextLength +(のblockSize - (plaintextLength%のblockSize))。
            } 

            []平文バイト=新しいバイト[plaintextLength]。
            System.arraycopyの(データバイト、0、プレーンテキスト、0、dataBytes.length)。
            
            SecretKeySpec keyspec =新しいSecretKeySpec(key.getBytes()、 "AES"); 
            IvParameterSpec ivspec =新しいIvParameterSpec(iv.getBytes()); 

            cipher.init(Cipher.ENCRYPT_MODE、keyspec、ivspec)。
            バイト[] = cipher.doFinal(平文)暗号化されました。

            新しいsun.misc.BASE64Encoderを返す()(暗号化)をコードします。

        }キャッチ(例外e){ 
            e.printStackTrace(); 
            ヌルを返します。
        } 
    } 

    公共の静的な文字列DESENCRYPT(){例外をスロー
        試みる
        { 
            "== 2fbwW9 + 8vPId2 / foafZq6Q"文字列データ=。
            文字列のキー= "1234567812345678"; 
            文字列のIV = "1234567812345678"; 
            
            バイト[] encrypted1 =新しいBASE64Decoder()decodeBuffer(データ)。
            
            暗号暗号= Cipher.getInstance( "AES / CBC / NoPadding")。
            SecretKeySpec keyspec =新しいSecretKeySpec(key.getBytes()、 "AES");
            IvParameterSpec ivspec =新しいIvParameterSpec(iv.getBytes());
            
            cipher.init(Cipher.DECRYPT_MODE、keyspec、ivspec)。
 
            バイト[] =オリジナルcipher.doFinal(encrypted1)。
            ストリングoriginalString =新しい文字列(オリジナル)。
            originalStringを返します。
        } 
        キャッチ(例外e){ 
            e.printStackTrace(); 
            ヌルを返します。
        } 
    } 
}

  Javaの塗りつぶしパターンは、独自のゼロで満たされたコンテンツにまで書かれた、NoPaddingです。

PHP:

<?PHPの
$ののPrivateKey = "1234567812345678"; 
$ IV = "1234567812345678"; 
$データ=「テスト文字列」; 

//加密
$ = MCRYPT_ENCRYPT(MCRYPT_RIJNDAEL_128、$のPrivateKey、$データ、MCRYPT_MODE_CBC、$ IV)で暗号化されました。
エコー(BASE64_ENCODE($)は、暗号化されました)。
エコー'<BR/>'; 

//解密
$はEncryptedData = BASE64_DECODE( "2fbwW9 + 8vPId2 / foafZq6Q =="); 
$解読= MCRYPT_DECRYPT(MCRYPT_RIJNDAEL_128、$のPrivateKey、$はEncryptedData、MCRYPT_MODE_CBC、$ IV)。
エコー($復号化されました)。
?>

 C#の好きな言語

システムを使用しました。
System.Collections.Genericを使用しました。
System.Linqのを使用しました。
System.Textのを使用しました。
System.Security.Cryptographyを使用しました。

名前空間pda_demo 
{ 
    クラスプログラム
    { 
        静的な無効メイン(文字列[] args)
        { 
            文字列encryptData = Program.Encrypt( "テスト文字列"、 "1234567812345678"、 "1234567812345678")。
            Console.WriteLineを(encryptData)。

            ストリングdecryptData = Program.Decrypt( "2fbwW9 + 8vPId2 / foafZq6Q =="、 "1234567812345678"、 "1234567812345678")。
            Console.WriteLineを(decryptData)。

            Console.Read(); 
        }

        パブリック静的文字列を暗号化(ストリングtoEncrypt、文字列キー、列IV)
        { 
            バイト[] keyArray = UTF8Encoding.UTF8.GetBytes(キー)。
            バイト[] ivArray = UTF8Encoding.UTF8.GetBytes(IV)。
            バイト[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt)。

            RijndaelManaged RDEL =新しいRijndaelManaged(); 
            rDel.Key = keyArray。
            rDel.IV = ivArray。
            rDel.Mode = CipherMode.CBC。
            rDel.Padding = PaddingMode.Zeros。

            ICryptoTransform cTransform = rDel.CreateEncryptor()。
            バイト[] resultArray = cTransform.TransformFinalBlock(toEncryptArray、0、toEncryptArray.Length)。

            Convert.ToBase64String(resultArray、0、resultArray.Length)を返します。
        } 

        パブリック静的文字列復号化(文字列toDecrypt、文字列キー、列IV)
        { 
            バイト[] keyArray = UTF8Encoding.UTF8.GetBytes(キー)。
            バイト[] ivArray = UTF8Encoding.UTF8.GetBytes(IV)。
            バイト[] toEncryptArray = Convert.FromBase64String(toDecrypt)。

            RijndaelManaged RDEL =新しいRijndaelManaged(); 
            rDel.Key = keyArray。
            rDel.IV = ivArray。
            rDel.Mode = CipherMode.CBC。
            rDel.Padding = PaddingMode.Zeros。

            ICryptoTransform cTransform = rDel.CreateDecryptor()。
            バイト[] resultArray = cTransform.TransformFinalBlock(toEncryptArray、0、toEncryptArray.Length)。

            UTF8Encoding.UTF8.GetString(resultArray)を返します。
        } 
    } 
}

  JS:

<スクリプトSRC = "aes.js"> </ SCRIPT> 
<スクリプトSRC = "パッドzeropadding.js"> </ SCRIPT> 
<SCRIPT> 
VARデータ= "テスト文字列"; 
VARキー= CryptoJS.enc.Latin1.parse( '1234567812345678'); 
VaRのIV = CryptoJS.enc.Latin1.parse( '1234567812345678'); 

//加密
VAR暗号化= CryptoJS.AES.encrypt(データ、キー、{IV:IV、モード:CryptoJS.mode.CBC、パディング:CryptoJS.pad.ZeroPadding})。

document.write(encrypted.ciphertext)。
document.write( '<BR/>'); 
document.write(encrypted.key)。
document.write( '<BR/>'); 
document.write(encrypted.iv)。
document.write( '<BR/>'); 
document.write(encrypted.salt)。
資料。書き込み( '<BR/>'); 
document.write(暗号化); 
document.write( '<BR/>');


//解密
= CryptoJS.AES.decrypt解読VAR(暗号化、鍵、{IV:IV、パディング:CryptoJS.pad.ZeroPadding})。
console.log(decrypted.toString(CryptoJS.enc.Utf8))。
</ SCRIPT>

  Javaのゼロパディング操作:

公共の静的な文字列paddingZero(INT NUM、INT長){ 
		文字列S = "" + NUM。
		一方、(s.length()<長さ){ 
			S = "0" + S。
		} 
		戻りS。		
	} 
プライベート最終INT ORDOR_LEN = 4。

  

 

おすすめ

転載: www.cnblogs.com/sunliyuan/p/11130446.html
おすすめ