• 資訊部
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hi guys,
I have big question. How to change this C# code to Apex. I tried but it didn't work. It always give me APEX_ERROR : System.InvalidParameterValueException: Invalid private key. Must be 16 bytes. Can somebody  help me, please. 
User-added image
@RestResource(urlMapping='/API_test/*')
global class API_test{

    global class clsrecords{   	
        global String endpoint{get;set;}
        global String decrypt{get;set;}
    }
    //Get
    @HttpGet
    global static List<clsrecords> getrecords(){
        
        Date dToday = System.Today();
        Datetime myDateTime = Datetime.now();
        Datetime datetimee = myDateTime.addDays(277);
        String datee = datetimee.format('yyyyMMdd');
        
        String d = EncodingUtil.urlDecode('8806165303', 'utf8');
        string key = EncodingUtil.urlDecode(datee,'utf8');
        string iv = EncodingUtil.urlDecode('WGXVRMNU', 'utf8');
        
        Blob data = blob.valueof(d);
        Blob token_key = blob.valueof(key);
        Blob token_iv = blob.valueof(iv);
        
        //Blob decrypt = Crypto.decryptWithManagedIV('AES128', token_key, token_key);
        //Blob decrypt = Crypto.decrypt('AES256',token_key,token_iv,data);
        //Blob encrypt = Crypto.encrypt('AES128',data2,token_key,token_iv); 
        Blob encrypt = Crypto.encryptWithManagedIV('AES128', token_key, token_iv ); 
        
        List<clsrecords> lstcls = new List<clsrecords>();
        clsrecords cls = new clsrecords();
        cls.decrypt = String.valueof(encrypt);
        //cls.decrypt = String.valueof(data.size())+String.valueof(token_key.size())+String.valueof(token_iv.size());
        lstcls.add(cls);
        return lstcls;
    }
}

 
Hi guy ,
this is my practice class and test class.I don't understand why my test class can not pass. Can someone help me? please...
public with sharing class ContactExtension {

    public Contact cont{get;set;}
    public String Phone{get;set;}
    
    public ContactExtension(ApexPages.StandardController controller){    
    
        Id ContactId = ApexPages.currentPage().getParameters().get('Id');
        this.cont = (Contact)controller.getrecord(); 
        Contact cont = new Contact();
    }   
    public PageReference save(){              
       
        PageReference pagereference;      
        if(cont.Phone != null){     
			Phone = cont.phone;            
            cont.Phone = '886'+cont.Phone;    
            cont.MobilePhone = '886'+ Phone;       
            insert cont; 
            pagereference = new PageReference('/'+cont.id);
        }      
        return pagereference;  
    }
	     
}
 
@isTest
public class testContactExtension {

     static testMethod void testm1(){
    
         test.startTest();         
         
         Contact  tcontact  = new Contact(LastName='TEST',Phone='0973768421',Email='Test@jzn.com.tw',JOB_STATUS__c ='在職');   
         insert tcontact; 
         
         PageReference pageRef=new PageReference ('/apex/PageTryInput');
         test.setCurrentPage(pageRef);
         pageRef.getParameters().put('Id',String.valueOf(tcontact.Id));
         ApexPages.StandardController thecontroller;
         ContactExtension cls = new ContactExtension(thecontroller);   
        
         cls.Phone ='00000';
         cls.cont = new Contact();
         cls.cont.Phone = '0000000';
         cls.cont.MobilePhone = cls.Phone + cls.cont.Phone;
         cls.save(); 

         
         test.stopTest();
     }
    
}