How to encrypt all existing stored procedures of a database

前端 未结 8 1961
后悔当初
后悔当初 2021-02-10 10:13

Is there any possibility to encrypt all existing stored procedures of a SQL Server 2008 database AFTER they have been created via an SQLCMD script?

The reason I want to

相关标签:
8条回答
  • 2021-02-10 10:56

    Use This Query which Encrypt All Procedures in database

        CREATE TABLE #backup
    (
    id BIGINT IDENTITY(1, 1),
    sptext NVARCHAR(MAX) NOT NULL,
    spname NVARCHAR(100) NOT NULL,
    encrypttext NVARCHAR(MAX) NULL,
    encryptstatus BIT NOT NULL
    DEFAULT ( 0 )
    )
    DECLARE @sptexttable TABLE
    (
    id BIGINT IDENTITY(1, 1),
    sptext NVARCHAR(MAX),
    spname NVARCHAR(100)
    )
    INSERT INTO @sptexttable ( sptext, spname )
    SELECT [text],
    [name]
    FROM syscomments
    JOIN sysobjects ON syscomments.id = sysobjects.id
    AND sysobjects.xtype = 'p'
    DECLARE @sptext NVARCHAR(MAX)
    DECLARE @spname NVARCHAR(100)
    DECLARE @counter INT
    SET @counter = 1
    WHILE @counter <= ( SELECT MAX(id)
    FROM @sptexttable
    )
    BEGIN
    BEGIN TRY
    
    INSERT INTO #backup ( sptext, spname )
    SELECT sptext,
    spname
    FROM @sptexttable
    WHERE id = @counter
    END TRY
    BEGIN CATCH
    END CATCH
    
    IF NOT EXISTS ( SELECT [name]
    FROM sysobjects
    WHERE [name] = 'ce_LastIndexOf'
    AND xtype = 'FN' ) 
    BEGIN
    
    
    EXEC
    ( 'CREATE FUNCTION ce_LastIndexOf
    (
    @strValue VARCHAR(4000),
    @strChar VARCHAR(50)
    )
    RETURNS INT
    AS BEGIN
    DECLARE @index INT
    
    
    SET @index = 0
    
    
    WHILE CHARINDEX(@strChar, @strValue) > 0
    BEGIN
    SET @index = @index
    + CASE WHEN CHARINDEX(@strChar, @strValue) > 1
    THEN ( LEN(@strValue) - LEN(SUBSTRING(@strValue,
    CHARINDEX(@strChar, @strValue)
    + LEN(@strChar),
    LEN(@strValue))) )
    ELSE 1
    END
    SET @strValue = SUBSTRING(@strValue,
    CHARINDEX(@strChar, @strValue)
    + LEN(@strChar), LEN(@strValue))
    END
    RETURN @index
    END'
    )
    
    
    END
    DECLARE @tempproc NVARCHAR(MAX)
    DECLARE @procindex INT
    DECLARE @beginindex INT
    DECLARE @header NVARCHAR(MAX)
    DECLARE @asindex INT
    DECLARE @replacetext NVARCHAR(MAX)
    
    SET @tempproc = ( SELECT sptext
    FROM @sptexttable
    WHERE id = @counter
    )
    
    IF ( SELECT CHARINDEX('CREATE PROC', UPPER(@tempproc))
    ) > 0 
    BEGIN
    BEGIN TRY
    SELECT @procindex = CHARINDEX('PROC', UPPER(@tempproc))
    PRINT @procindex
    SELECT @beginindex = CHARINDEX('BEGIN', UPPER(@tempproc))
    PRINT @beginindex
    SELECT @header = SUBSTRING(@tempproc, @procindex,
    @beginindex - @procindex)
    SELECT @asindex = ( SELECT dbo.ce_lastindexof(@header, 'AS')
    - 2
    )
    SELECT @replacetext = STUFF(@header, @asindex, 10,
    CHAR(13) + 'WITH ENCRYPTION'
    + CHAR(13) + 'AS' + CHAR(13))
    SET @tempproc = REPLACE(@tempproc, @header, @replacetext)
    
    END TRY
    BEGIN CATCH
    END CATCH
    END
    
    UPDATE @sptexttable
    SET sptext = @tempproc
    WHERE id = @counter
    
    
    --PLAY HERE TO M AKE SURE ALL PROCS ARE ALTERED
    UPDATE @sptexttable
    SET sptext = ( SELECT REPLACE(sptext, 'CREATE PROC',
    'ALTER PROC')
    FROM @sptexttable
    WHERE id = @counter
    )
    WHERE id = @counter
    SELECT @sptext = sptext,
    @spname = spname
    FROM @sptexttable
    WHERE id = @counter
    BEGIN TRY
    EXEC ( @sptext
    )
    UPDATE #backup
    SET encrypttext = @sptext,
    encryptstatus = 1
    WHERE id = @counter
    END TRY
    BEGIN CATCH
    PRINT 'the stored procedure ' + @spname
    + ' cannot be encrypted automatically'
    END CATCH
    SET @counter = @counter + 1
    END
    SELECT *
    FROM #backup
    
    0 讨论(0)
  • 2021-02-10 11:01

    I wrote a cursor, steps through and encrypts most objects.

                                DECLARE cur_ENCRYPT_ANTHING CURSOR READ_ONLY
                                FOR
                                        SELECT  STUFF(src.definition,
                                                      CASE WHEN CHARINDEX('AS' + CHAR(13),src.definition,1) = 0
                                                           THEN CASE WHEN CHARINDEX('AS ' + CHAR(13),src.definition,1) = 0 THEN CHARINDEX('AS ',src.definition,1)
                                                                     ELSE CHARINDEX('AS ' + CHAR(13),src.definition,1)
                                                                END
                                                           ELSE CHARINDEX('AS' + CHAR(13),src.definition,1)
                                                      END,3,'WITH ENCRYPTION AS' + CHAR(13))
                                        FROM    (SELECT o.name
                                                 ,      STUFF(RIGHT(sm.definition,LEN(sm.definition) - CHARINDEX('CREATE ',sm.definition,1) + 1),1,6,'ALTER') AS definition
                                                 FROM   sys.sql_modules AS sm
                                                        JOIN sys.objects AS o ON sm.object_id = o.object_id
                                                 WHERE  CAST(CASE WHEN sm.definition IS NULL THEN 1
                                                                  ELSE 0
                                                             END AS BIT) = 0
                                                        AND type <> 'TR'
                                                ) AS src
    
    
    
    
    
    
    
    
                                DECLARE @VLS NVARCHAR(MAX)
                                OPEN cur_ENCRYPT_ANTHING
    
                                FETCH NEXT FROM cur_ENCRYPT_ANTHING INTO @VLS
                                WHILE (@@fetch_status <> -1)
                                      BEGIN
                                            IF (@@fetch_status <> -2)
                                               BEGIN
                                                     BEGIN TRY
                                                           EXEC (@VLS)
    
                                                     END TRY
                                                     BEGIN CATCH
                                                           PRINT ERROR_MESSAGE()
                                                           PRINT ''
    
                                                           PRINT @VLS
                                                     END CATCH
                                               END
                                            FETCH NEXT FROM cur_ENCRYPT_ANTHING INTO @VLS
                                      END
    
                                CLOSE cur_ENCRYPT_ANTHING
                                DEALLOCATE cur_ENCRYPT_ANTHING
    
    0 讨论(0)
提交回复
热议问题