手机号解密代码示例

Java

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
import java.util.Arrays;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;

public class DecryptMobile{
    public static String decrypt(String algorithm, String cipherText, SecretKey key,IvParameterSpec iv)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
        return new String(plainText);
    }
    
    public static void main(String []args)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        
        String clientSecret = "0123456789abcdef0123456789abcdef";
        String encryptedMobile = "tyUWQwYuUmVFJtElAL+D7Q==";
        
        byte[] clientSecretBytes = clientSecret.getBytes();
        SecretKey secretKey = new SecretKeySpec(clientSecretBytes, 0, clientSecretBytes.length, "AES");
        
        byte[] iv = Arrays.copyOfRange(clientSecretBytes, 0, 16);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        
        String algorithm = "AES/CBC/PKCS5Padding";

        String mobile = decrypt(algorithm, encryptedMobile, secretKey, ivParameterSpec);
        System.out.println(mobile);
    }
}

PHP

<?php
    $client_secret = '0123456789abcdef0123456789abcdef';
    $encrypted_mobile = 'tyUWQwYuUmVFJtElAL+D7Q==';

    function decrypt($encrypted_mobile, $client_secret) {
        $iv = substr($client_secret, 0, 16);
        return openssl_decrypt($encrypted_mobile, 'aes-256-cbc', $client_secret, 0, $iv);
    }

    echo decrypt($encrypted_mobile, $client_secret);
?>

Golang

package main

import (
   "crypto/aes"
   "crypto/cipher"
   "encoding/base64"
   "errors"
   "fmt"
)

const (
   clientSecret    = "0123456789abcdef0123456789abcdef"
   encryptedMobile = "tyUWQwYuUmVFJtElAL+D7Q=="
)

func main() {
   fmt.Println(DecryptString(clientSecret, encryptedMobile))
}

func DecryptString(clientSecret, encryptStr string) string {
   origData := decodeBase64(encryptStr)
   key := []byte(clientSecret)
   iv := []byte(clientSecret)[:16]
   ret, _ := aesDecrypt(origData, key, iv)
   return string(ret)
}

func aesDecrypt(crypted, key, iv []byte) ([]byte, error) {
   block, err := aes.NewCipher(key)
   if err != nil {
      return nil, err
   }
   if len(iv) != block.BlockSize() {
      return nil, errors.New("ErrAesDecryptBlockSize")
   }
   encrypter := cipher.NewCBCDecrypter(block, iv)
   origData := make([]byte, len(crypted))
   encrypter.CryptBlocks(origData, crypted)
   return pkcs5UnPadding(origData)
}

func decodeBase64(in string) []byte {
   out := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
   n, err := base64.StdEncoding.Decode(out, []byte(in))
   if err != nil {
      return nil
   }
   return out[0:n]
}

func pkcs5UnPadding(origData []byte) ([]byte, error) {
   length := len(origData)
   unpadding := int(origData[length-1])
   end := length - unpadding
   if end > length || end < 0 {
      return []byte{}, errors.New("array bound")
   }
   return origData[:end], nil
}
诚邀您对本文档易读易用性进行评价
好用
不好用